Primitive Data Types In Java

Data Types in java represents the different values that needs to be stored in the variable.

There are two Types of Data Types available in java :

A.Primitive Data Types.
B.Non-Primitive Data Types.

A. Primitive Data Types in Java :
There are 8 primitive Data Types available in java. They are

  1. Boolean
  2. Char
  3. Byte
  4. Short
  5. Int
  6. Long
  7. Float
  8. Double

1.Boolean:Boolean Data Type has only two possible values. i.e True or false.
If we declare a data type and not initialized then Default value will be taken as false.

Example : boolean result = true

class booleanValue
//class name or java file name called booleanValue
{
public static void main(String args[])
//main method program execution will start from here
{
boolean result = true ;

//declaring the variable result as boolean type and assigning value as true
System.out.println(“The value of boolean is:”+result);
//printing the value of result value
}
}
Output :

Java-Boolean-Data-Type-Example

2.Char : Char Data Type is used to store any character.
Default value is ‘\u0000’

Example : char alphabet=’Z’

class charValue
//class name or java file name called charValue
{
public static void main(String args[])
//main method program execution will start from here
{
char alphabet=’Z’ ;

//declaring the variable alphabet as char type and assigning value as ‘Z’

System.out.println(“The value of alphabet is:”+alphabet);

//printing the value of alphabet
}
}
OUTPUT :

3.Byte : Byte Data Type is used to store numbers
Default value is 0
Example : byte a= 110, byte b = – 40

class byteExample
//class name or java file name called byteExample
{
public static void main(String args[])
//main method program execution will start from here
{
byte a = 110 ;
byte b =-40;

//declaring the variable as a and b as byte type and assigning value as 110 and -40

System.out.println(“The value of a is:”+a);

System.out.println(“The value of b is:”+b);

//printing the values of a and b
}
}
Output :

Java Data Type Byte Example

4.Short : Short Data Type is used to store numbers
Default Value is 0
Example : short s = 20000, short k = -19000

class shortExample
//class name or java file name called shortExample
{
public static void main(String args[])
//main method program execution will start from here
{
short s = 20000;
short k=-19000 ;

//declaring the variable as s,k of type “short” and assigning values sa 20000 and -19000

System.out.println(“The value of s is:”+s);

System.out.println(“The value of k is:”+k);

//printing the values of s and k
}
}
OUTPUT :

Java-short-Data-Type-Example

5.Int : Int Data Type is used to store numbers.
Default value is 0
Example : int a = 100000, int z = -200000

class intExample
//class name or java file name called intExample
{
public static void main(String args[])
//main method program execution will start from here
{
int a = 100000;
int z=-200000;

//declaring the variable a,b of type “int” and assigning a,z values

System.out.println(“The value of a is:”+a);

System.out.println(“The value of z is:”+z);

//printing the values of a and z
}
}
OUTPUT :

6.Long : Long Data Type is used to store numbers.
Default value is 0L
Example : long a=200000L, Long b = -300000L

class longExample
//class name or java file name called longExample
{
public static void main(String args[])
//main method program execution will start from here
{
long a=200000L;
Long b=-300000L;

//declaring the variable a,b of type “long” and assigning values of a and b

System.out.println(“The value of a is:”+a);

System.out.println(“The value of b is:”+b);

//printing the values of a and b
}
}
OUTPUT :

Java-Long-DataType-Example

7.Float : Float Data Type is used to store numbers.
Default value is 0.0f
Example : float f1 = 212.5f

class floatExample
//class name or java file name called floatExample
{
public static void main(String args[])
//main method program execution will start from here
{
float f1=212.5f;

//declaring the variable a,b of type “Float” and assigning value as 212.5f

System.out.println(“The value of f1 is:”+f1);

//printing the value of f1
}
}
OUTPUT:

  1. Double : Double Data Type is used to store numbers.
    Default value is 0.0d
    Example : double a1 = 132.5

class doubleExample
//class name or java file name called doubleExample
{
public static void main(String args[])
//main method program execution will start from here
{
double a=132.5;

//declaring the variable a of type “double” and assigning value as a

System.out.println(“The value of a is:”+a);

//printing the value of a
}
}
OUTPUT :

Java-Double-Data-Type-Example

For our easy remembering we can classify them into
Logical or conditional Data Type :

boolean

Text based Data Type :

char

Integer Java Data Types:

byte
short
int
long

Floating Data Type :

Float
Double

Leave a Comment

Your email address will not be published.