freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Data Types in Java

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

There are two data types available in Java:
1.Primitive Data Types
2.Reference/Object Data Types

Java Images

Primitive data types:

   There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.

Boolean datatype:

Boolean datatype: Boolean data types represent any of the two values, true or false. JVM uses 1 bit to represent a Boolean value internally.
For example: boolean response=true;
We should not enclose the boolean value true (or false) in any quotation marks.

Char datatype:

       In Java, the data type used to store characters is char. Character is 16 bits wide in Java. Java uses Unicode to represent characters. Unicode is a computer industry standard for representing text related data. This includes alphabets, symbols ($, &, *, #, @, !, etc.), and special figures such as ...etc. Java support lot of Unicode symbols from many more human languages for this purpose, it requires 16 bits. The range of a char is 0 to 65,536.
Syntax: char = ;
For Example: char ch='a';

Integral datatypes:

       These datatypes represent integer numbers, i.e. numbers without any fractional parts or decimal points. For example, 125, -225678, 0, 1022, etc. The integer datatypes are again sub devided into byte, short, int, and long types.
Integer datatype: Int data type is a 32-bit signed two's complement integer. It is one of the most commonly used data types in Java. Int is generally used as the default data type for integral values unless there is a concern about memory.
Syntax: int = ;
Example: int n=1110;

Byte datatype: Byte data type is an 8-bit signed two's complement integer. byte is smallest Java integer type. byte is mostly used when dealing with raw data like reading a binary file. Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
Syntax: byte = < value>;
Example: byte b=10;

Short datatype: A short is twice the size of a byte, Short data type is a 16-bit signed two's complement integer. Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int.
Syntax: short = < value>;
Example: short s=1000;

Long datatype: Long data type is a 64-bit signed two's complement integer and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. This makes it useful when big, whole numbers are needed. The default value of long datatype is 0 
Syntax: long = < value>;
Example: long x=100000L;

Floating point datatypes

       These datatypes are useful to represent numbers with decimal point. For Example, 3.14, 0.0012, -123.11, etc. are called floating point numbers. These are again classified as float (Single precision floating point number) and double (Double precision floating point number).
Float datatype: In programming, any decimal or fractional value is called a float .If there is a decimal after the number, it will be classified as a float. In Java, a float is made up of 32-bits IEEE floating points*. If we have to assign float value then we must use  or  literal to specify that current value is Float.
Example: float x=2.321f; 

Double datatype: Double is a data type that is twice the size of a float. I.e. it is made up of 64-bit IEEE floating points. In Java any value declared with decimal point is by default of type double. Default value is 0.0d. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values.
Example: double y=2.321;

Non primitive datatypes:

    The non-primitive data types in Java are objects and arrays. These non-primitive types are often called "reference types" because they are handled "by reference"--in other words, the address of the object or array is stored in a variable, passed to methods, and so on.
Examples: Object of a class Car, String, etc.