Data types in Java
Java supports several types of data , you can use these types to declare variables and to create arrays. Data types in Java are also referred as simple types or primitive types. Java has divided the types into two categories predefined types and referenced types. Predefined types are nothing but already existing types in Java like int , float , char etc. Referenced types are classes , interfaces and array types , for example ‘class’ Creating a ‘class’ is like creating a new data type and you can use this type to create object. Well this can be clearly explained in the classes and methods chapter.
Primitive types are predefined type in Java programming language. Under primitive type you can observe several data types to declare a variable. Primitive type are put into four groups.
Integers : byte , short , int and long.
Floating-point numbers : float and double , which represent numbers with fractional precision
Characters : This group includes char which represents symbols in a character set, like letters and symbols.
Boolean : This group includes boolean , which is a special type for representing true/false values.
All the data types under primitive had strictly range , for example int is always 32 bits regardless of the platform.
- Java defines four types of integers : byte , short , int and long. All of these are signed , positive and negative values.
- The width and ranges of these integers types vary widely , as shown in the below table
- Byte variables are declared by the used byte keyword. For example byte b,c;
- The smallest integer type is byte , is a signed 8-bit type that has a range from -128 to +128.
- Variables of type byte are especially used when you’re working with network values.
- Short variables are declared by the use of short keyword for example short s;
- Short is 16-bit signed type , it is probably the least used Java type
- The most commonly used integer type is int , it is signed 32-bit type has a range from -2,147,483,648 to +2,147,483,648.
- Variables of type int are commonly used in loops and to index an array.
- Although you might think that using a byte or short would be more efficient than using an int in situations. But Java always prefers an int type for portability and type casting which is explained down the line.
- long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.
- The range of long is quite large , this makes it useful when whole and large numbers are needed to store in a variable.
- For example int cannot hold the speed of the light in number , where long can hold. Below is the program which explains where long type.
- As you can see the output ,the value “16070400000000” holding by the distance variable of type long , where int cannot hold such a large number.
- Type casting is required to hold by the int type .
- Floating-point types are also known are real numbers , which are used when evaluating an expression which contains decimal at the end of the number.
- For example calculating the value of sine or cosine , might require the floating-point type in order hold the value , because the values of sine or cosine may contain fractional precision(decimal).
- There are two kinds of floating-point type one is float and the other is double .
- Width and range of these two types are shown below
- The type float specifies single decimal value that uses 32-bit of storage.
- float uses half of double type , which helps in processing fast results.
- Variables of type float are useful when you need a fractional component , but don’t require a large degree of decimals.
- Type double uses 64-bit to store a double decimal value.
- double precision is actually faster than single precision on some modern processors that have been optimized for high-sped mathematical calculations.
- All transcendental math functions such as sin() , cos() returns double values.
- when you need accuracy for any calculation in a program it’s better to use double type
- Example : double pi =3.1416 which is the value of pi .
- In Java , the data type used for characters is char . The char in C/C++ is different from char in Java.
- In C/C++ the char is 8 bits where as in Java the storage space is 16-bits.
- Java uses unicode to represent characters. For example the value of character ‘x’ is 88
- Unicode defines a fully international character set that can represent all of the characters found in the human language. For this purpose it requires 16-bits of space.
- Unicode ranges from 0 to 127, unicode which is also represented as ASCII. The ASCII value of ‘x’ is 88.
- The range of char is from 0 to 65,536 , there are no negative chars.
- Although char is used to hold ASCII values , it can also be used as an integer type on which you can perform arithmetic operations.
- In Java it is possible to store logical values true/false in a variable .
- It is done through using the primitive data type called boolean. Boolean can have only two possible values that is true or false.
- Boolean will be mostly used in the control statements like ‘if’ and ‘for’.
- To control a statement boolean type is mostly used , for example below is the program that demonstrates the boolean type.
- As you can observe in the above program , there are two control ‘if’ statements.
- In Java if(b==true) is equal to if(b) , because b is already a boolean variable.
- we will look into the output how it shows the values of b .
We have to notice three points in the above program.
- The first ‘if’ statement displays true because , the last value stored in b is true. ‘if’ statements only displays when the condition is true. here the condition is ‘b==true‘
- The second ‘if‘ statement failed to execute because , we changed the b value to false, ‘if’ statement only executes when the condition is true.
- Third , the outcome of the relational operator statement “System.out.println(” 10>9 is “ + (10>9)); ” , this statement returned ‘true‘ because the relational operator we have used in the statement is “<” is a boolean value which returns either true or false depending on the operation.
- In our case we stated (10<9) which is true , so it displayed true in the output.