Primitive Data Types in Java

Google+
Google+
http://programmingloops.com/primitive-data-types-in-java/

There are 8 different primitive data types in Java, which can be categorize into 3 categories, namely Numeric, Textual and Boolean. Numeric can be further classify into two categories, integral and floating-point. byte, short, int and long fall under integral category, and float and double fall under floating-point category. char and boolean fall under Textual and Boolean primitive categories respectively. All these data types are fix in size, let us learn more about each one of them in details, starting with byte.

byte: byte is 8 bits in size, has a default value of 0, and can store value from -128 to 127. byte helps in saving memory, means programmers can use it for small integer value, instead of int data type.
Initialization examples: byte b1 = 4, and byte b2 = -6.

short: short is 16 bits in size, has a default value of 0, and can store value from -32,768 to 32767. Similar to byte data type, short can be use as a substitute to int data type for saving memory.
Initialization examples: short s1 = 12, and short s2 = -11.

int: int is 32 bits in size, has a default value of 0, and can store value from -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). int is the most commonly use numeric data type.
Initialization examples: int i1 = 100, and int i2 = -200.

long: long is 64 bits in size, has a default value of 0, and can store value from -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807). long is mainly use when a range wider than int data type is require.
Initialization examples: long l1 = -10000L, and long l2 = 20000L.
A thing to be note while initializing long, the letter “l” or “L” after the variable value is require as shown in the above examples.

float: float is 32 bits in size, has a default value of 0.0f, and range is beyond the discussion. float is use when there is a need to save memory in a large arrays of floating-point numbers.
Initialization examples: float f1 = -12.23f, and float f2 = 432.23f.
A thing to be note while initializing float, the letter “f”or “F” after the variable value is require as shown in the above examples.

double: double is 64 bits in size, has a default value of 0.0d, and like float, its range is beyond the discussion. This data type is use for decimal values.
Initialization examples: double d1 = -32.12d, and double d2 = 567.25d.
A thing to be note while initializing double, the letter “d”or “D” after the variable value is require as shown in the above examples.

char: char represents Unicode character that includes alphabets, numerals and special symbols. char is 16 bits in size, has a minimum value of ‘\u0000’ or 0, and maximum value of 65,535.
Initialization examples: char a = ‘A’, and char b = ‘B’.
boolean: boolean is 1 bit size data type, and has a default value of false. boolean is mainly use for flag value, which is either true or false.
Initialization examples: boolean b1 = true, and boolean b2 = false.

Here is a program example covering all the primitive data types:
public class primitivedts
{
        public static void main (String args[])
        {
               byte b = 7;
               short s = 129;
               int i = 1112;
               long l = 33333l;
               float f = 12.34f;
               double d = 567.12d;
               char c = ‘C’;
               boolean b1 = true;
               System.out.println(“The value of byte is : “+b);
               System.out.println(“The value of short is : “+s);
               System.out.println(“The value of integer is : “+i);
               System.out.println(“The value of long is : “+l);
               System.out.println(“The value of float is : “+f);
               System.out.println(“The value of double is : “+d);
               System.out.println(“The character is : “+c);
               System.out.println(“The boolean value is : “+b1);
        }
}

You can learn more about java primitive data types from here.