Data Types

Data Types

In java every variable and every expression should has some type and every type is strictly defined. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. There are two data types available in Java:

  1. Primitive Data Types
  2. Reference/Object Data Types
1. Primitive Data Types 

Primitive Data Types are again divided in to 2 types

  1. Numeric Data types ( Integral and Floating point)
  2. Non Numeric Data types (Char and boolean)

Integral – byte, short, int, long

Floating point – float, double

1. byte
  • Byte data type is an 8-bit signed two’s complement integer.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Range : – 128 to + 127
  • 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.
  • The most significant bit acts as sign bit. 0 means +ve number and 1 means -ve number.
  • Positive numbers will be represented directly in the memory where as negative numbers will be represented in 2’s compliment form.
byte b = 10;
byte b = 127;
byte b = 128; // Compile time error. Possible loss of precision found: int required: byte
byte b = 20.5 // Compile time error. Possible loss of precision found: double required: byte
byte b = true; // Compile time error. In compatible types found: boolean required: byte

Note

byte data type is best suitable to handle data in terms of streams either form the file or form the network.

2. short
  • Short data type is a 16-bit signed two’s complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
  • Default value is 0.
short s = 10;
short s = 127;
short s = 32768; // Compile time error. Possible loss of precision found: int required: short
short s = 20.5 // Compile time error. Possible loss of precision found: double required: short
short s = true; // Compile time error. Possible loss of precision found: boolean required: short

Note

short data type is best suitable for 16-bit processors like 8086 but these processors are out dated hence short data type also out dated data type

3. int
  • int data type is a 32-bit signed two’s complement integer.
  • Minimum value is – 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • int is generally used as the default data type for integral values unless there is a concern about memory.
  • Default value is 0.
int i = 10;
int i = 2147483648; // C.E :  integer number too large
int i = 2147483648l ;// C.E. Possible loss of precision found: long required: int
int i = true; // C.E. in compatible types found: boolean required: int

Note

  1. By default every number compiler will take as int if the range is beyond the int then we will get compile time error named integer number too large.
  2. In C language the size of int is varied from platform to platform. For 16-bit processors it is 2 bytes where as for 32-bit processors it is 4 bytes. The main advantage of this approach is read and write operations we can perform very efficiently and performance will be improved. But the main disadvantage of this approach is the chance of failing C program is very high if we are changing platform. Hence C language is not considered as Robust.
  3. But in Java language the size of int is always 4 bytes. The main advantage of this approach is chance of failing java program is very less if we are changing platform. Hence Java language is considered as Robust. But the main disadvantage of this approach is the read and write operations will become costly and performance will be reduced.
4. long

Some times int may not enough to hold big values then we should go for long datatype. For example to hold the amount of distance travelled by light in 1000 days int may not enough then we should go for long datatype.

long distance = 126000*60*60*24*1000 miles;
  • Long data type is a 64-bit signed two’s complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.

All the above four data types meant for representing integral values. If we want to represent floating point values then we should go for floating point data types.

5. float
  • If you want 5 to 6 decimal places of accuracy then we should go for float.
  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.
  • Range is -3.4 E38 to +3.4 E38
6. double
  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Range is -1.7 E308 to +1.7 E308
Non Numeric Data types
1. boolean
  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false.
  • Size : Not applicable (JVM dependent)
  • Range : Not applicable (Allowed values are true or false)
boolean b = true;
boolean b = 0; C.E: in compatible types found: int required: boolean
boolean b = True; C.E: Cannot find symbol symbol: variable True location: class Test
boolean b = "True"; C.E: in compatible types found: java.lang.String required: boolean

int x = 0;
if(x) { // C.E: in compatible types found: int required: boolean
   System.out.println("Hai");
}

int x = 1;
while(x) { // C.E: in compatible types found: int required: boolean
   System.out.println("Hai");
}

Note

Above two are executed in C++ fine. We didn’t get any C.E.

2. char

In old languages like C and C++ we can use only ASCII characters and to represent all ASCII characters 8-bits are enough. Hence char size is 1 byte.

But in Java we can use Unicode characters which covers world wide all alphabets sets. The number of Unicode characters is greater than 256 and hence 1 byte is not enough to represent all characters. Hence we should go for 2 bytes.

  • char data type is a single 16-bit Unicode character.
  • Minimum value is ‘\u0000’ (or 0).
  • Maximum value is ‘\uffff’ (or 65,535 inclusive).
  • Char data type is used to store any character.
Reference Data Types
  • Reference variables are created using constructors of the classes. They are used to access objects. 
  • These variables are declared to be of a specific type that cannot be changed. For example, Employee, Student etc.
  • Class objects, and various type of array variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or any compatible type.
Animal animal = new Animal("Tiger");
null

null is the default value for object reference and we won’t apply for primitive types. If we are trying to assign null value for primitive data types then we will get compile time error.

char ch = null; 
C.E: in compatible types found: <null type> required: char
Data Types
Scroll to top