in Core Java

Data types in Java

Data Types in Java

 

Primitive data types in Java

Primitive Data types form the back bone to represent the properties/attributes of the objects. Any form of the data can be stored by using the primitive data types in Java.

Data types in Java are actually derived from the programming language C. This is the reason why, the primitive data types representation in Java looks like the structured programming.  This is also one of the reasons why some people quote that Java is only 99% Object oriented programming and the rest one percent – they are mentioning about the primitive data types representation.

In fact, java designers have made a wise decision by choosing this kind of representation as i believe this reduces the memory consumption and also reduces the creation of newer objects which is crucial to maintain the applications performance.

 

Primitive Data Types:

There are totally eight primitive data types in Java. They can be categorized as given below:

  • Integer types (Does not allow decimal places)
  • Rational Numbers(Numbers with decimal places)
  • characters
    • char
  • conditional
    • boolean

Please notice that all the data type keywords are in small letters. These are part of the java keywords and every keyword in java is in small letters.

In the integer data types, we have four different data types. But, why do we need four different types when one can do the job. Yes, it is extremely important to understand that each and every data type has limitations to the amount of numbers it can represent. There is a memory constraint defined for every data type. 

Understanding the memory limitations is extremely important in deciding which data type should be used. For example, when you are representing the age of a person, for sure it will not cross 120, so, using short data type is enough instead of long which has very big memory foot print.

The following should be understood for every data type:

  1. Memory size allocated.
  2. Default value
  3. Range of values it can represent.

Memory size for data types

Every data type has some memory size defined. This enables that when ever a variable is declared, the memory size defined will be blocked irrespective of the value that is going to get assigned.  

Default value:

Every primitive data type has default values defined. When the programmer does not declare to assign any values to the variables, these default values will be assigned by the Virtual machine during the object instantiation. 

Range of values the data types can represent

It is extremely important to understand what are the min and max range of values a data type can be able to hold. Without this, when ever a value that cannot fit the data type is assigned, the applications crash with fatal errors or exceptions.

Please note that all the data types that can represent numbers can hold both positive and negative numbers.

In programming zero is considered to be a positive number. 

byte :

byte data type is the first data type with least memory size allocated which can only be used for numbers of small ranges. 

  • The memory size allocated to byte is 8 bits.
  • It can represent a total of 256  (28) numbers. That means 128 numbers on the positive side and 128 on the negative side.
  • byte can represent from 0 to 127 on positive side( as zero is positive number per programming).
  • On the negative side it can represent the numbers from -1 to -128.
  • The default value for byte data type is zero -'0'.

Declaration example:

byte a =20;

short :

  • Memory size allocated to short is 16 bits.
  • It can represent a total of 65536(216) numbers.
  • On the positive side it represents from 0 to 32767 – a total of 32768 numbers.
  • on the negative side it represent from -1 to -32768.
  • default value for short is zero -'0'.

 

Declaration example:

short  a =15000;

 

int 

This data type is the most used data type in java. 

  • Memory size allocated is 32 bits
  • Represents a total of 4,294,967,296 numbers.
  • Default value for int is zero -'0'.

 

Declaration example:

int  a =33000;

long 

This data type is mostly used for huge values like astronomical distances.

  • Memory size allocated to long is 64 bits.
  • Default value is zero-'0'.

float 

This data type is used to represent the numbers with decimals.

  • Memory size allocated to float is 32 bits.
  • Default value of float is '0.0f'.

Declaration example:

float a = 2.45f;

Please note that the declaration of float is slightly different from others. Any value assigned to the float should be appended with either 'f' or 'F'. Without explicitly mentioning the 'f', compiler throws the syntax errors. 

The reason for enforcing to use 'f' might be to tell the JVM to only utilize 32 bits of memory. 

double 

This is also used to represent the decimal type of numbers.

  • Memory size allocated is 64 bits.
  • Default value is 0.0.

Declaration example:

double a = 30.444;

double a = 34.33d;

double a =345.98D;

During the declaration of double data type, when the value is being assigned, a 'd' or 'D' can be appended. But this is optional.

 

char 

The char data type is used to represent the characters. 

  • Memory size allocated to char is 16 bits.
  • It can represent around 65536 characters and due to which it can represent the characters from unicode which supports for different languages like chinese, hindi, german etc.,
  • default value for char is '\u0000' which is nothing by blank space.
  • '\u' infront of the hex codes represents that the character is a unicode.

Declaring char:

char b ='z';

Please note that a char can only hold one character at a time and should be quoted in single inverted commas.

 

boolean 

This is used as a result of conditional statements.

  • Memory size- JVM dependent – Note that the memory size allocated to boolean varies from platform to platform and this is the reason why it is said a JVM Dependent.
  • Default value for boolean is 'false'.

Declaration:

boolean a =true;

 

Data types in Java details
Data Type Memory Size Default value Declaration
byte 8 bits 0 byte a=9;
short 16 bits 0 short b=89;
int 32 bits 0 int c=8789;
long 64 bits 0 long=9878688;
float 32 bits 0.0f float b=89.8f;
double 64 bits 0.0 double c =87.098
char 16 bits '\u0000' char a ='e';
boolean JVM Dependent false boolean a =true;

 

 

Importance of Understanding limitations of data types:

 

 

 

importance of data types and memory sizes -Ariane 5

It is extremely important to understand the limitations of the data type respective to their memory sizes.  An very good example to sight the importance is the catastrophic failure of the Ariane 5 space shuttle. Ariane 5 shuttle is designed to fly higher altitudes than earlier shuttles. But the team has used the old software package which uses a variable like say short. But the shuttle is capable of flying higher heights which the variable cannot hold. 

When the shuttle took of and with in seconds when it reached to certain height, the variable failed to hold the higher values and on board computer generated fatal errors and the shuttle blasted off in the air which lead to loss of billions of dollars.

This is the example i sight that never under estimate the power of usage of data types when you are working on mission critical projects. 

Summary:

Primitive data types in java are derived from the C language and are programmed in structural syntax. Data types in java are designed to support all forms of numbers and is capable of representing multiple languages which has very good support for Internationalization. 

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...