Today we will discuss about primitive data types used in java.
“Is Java a pure object-oriented language?”
Let’s start discussion with a sensitive question, “Is Java a pure object-oriented language?”. I also have been stuck around different views of various authors. When I deeply studied, I got that we cannot say Java a pure object-oriented programming language. I have seen phrase ‘everything in java is object’.
But There are some primitive data types in java due to which I cannot say java as pure object-oriented language. I don’t force you to agree me because java is more object oriented than other previously studied language like c++ and simula. I just focused in word “pure”. No matter java is a best example of Object-oriented language, but I can’t say it as Pure object-oriented programming language.
Leave this topic and we briefly discuss about Java data types.
Data Types in Java
Data type is essential while declaring and defining variables. Data type defines type and nature of data, also the operations that can be performed in that data. There are total 8 data types in java.
Let’s divide data types in Java into categories.
- Numeric data types.
- Integral data types
- Floating point data types
- Integral data types
- Non numeric data types
Numeric data types are also called signed data type because these types can hold either positive or negative numbers.
Let’s start discussion all Java data types from first one.
Numeric Data Types in Java
Byte occupy 1 byte of memory in computer so it may be named as Byte. In the byte most significant bit is used to represent sign of the number and remaining 7 bits represent value of number. It can hold numbers ranging from -128 to +127. It is declared using keyword byte. Special thing about byte is that it is network and file supported form and best suitable when you are receiving or transmitting data over streams.
It is most rarely used data type in java because people are comfortable with Integer since there is no lack of memory in modern systems. It occupies 2 bytes (16 bits) of memory in computer. As like Byte MSB is used to represent sign of number and remaining 15 bits represent numerical value. So, its range is -215 to 215-1 i.e.(-32768 to 32767). The keyword short to used declare short type variables. It is suitable in case of 16 bit processors and systems with smaller memory like home appliances and imbedded systems.
It is most used data type in java. It occupies 4 bytes (32 bits) of memory. It can hold numbers between 231 to 231 – 1 i.e.(-2,147,483,648 to 2,147,483,647). We can declare integer variable by using keyword int. It is the data type that you will use most while programming with java.
Sometimes int is not enough to hold the value that we want. For example, we want to represent the Light year (distance travelled by light in one year) in meters. In such case java provide another data type that can hold much larger value than that of int data type. The size of long data type is 8 bytes. So, it can represent numbers from -263 to 263-1. Keyword long is used to define long type variables.
long dist = 46734l;
These all are Integral data types.
But if we want to represent floating point numbers, we have float and double data types which are floating point data types.
Foat type values occupy 4 bytes of memory in computer. It is single precision floating point data type. Default vale of float type value is 0.0f. Any float variable can hold value between 3.402,823,5 E+38 and 1.4 E-45. If you need precision up to 6-7 decimal points, then float data type can be best choice.
Double data type represents double precision floating point numbers. These occupy 8 bytes of memory in computer. Its range is from 1.797,693,134,862,315,7 E+308 to 4.9 E-324. When you need to store a value, whose precision is more than 7 decimal point you can use double data type.
Non-Numeric Data Types in Java
Char in java can store any Unicode character whose size is 16 bits. In java characters must be enclosed in single quotes. That is any character enclosed in single quote is treated as character in java. Default value for char data type is ‘\0000’
char choice ='A';
Boolean represent either true or false. Here true and false are reserved literals in java. Default value of Boolean data type is false.
You must remember a thing that if you assign any invalid value to a variable java produce error at compile time. That’s why it is called strongly typed language. For example, you can’t assign a float value or integer that out of range (less than -128 and greater than 127).