Java Data types Program (Primitive)
There are 8 primitive data types in java :
- Byte
- Int
- Float
- Double
- Short
- Long
- Char
- Boolean
Their Default Size and Range :
Data Types | Size | Range | Examples | Default |
---|---|---|---|---|
byte | 1 byte | Range of whole number starting from -128 to 127 | byte n = 20; | 0 |
Int | 4 bytes | Range of whole number starting from -2,147,483,648 to 2,147,483,647 | int b=10; | 0 |
float | 4 bytes | Fractional Number store 6 to 7 decimal digits/points | float d=10.00f; | 0.0 |
double | 8 bytes | Fractional Number store 15 decimal digits/points | double p = 12.34; | 0.0 |
short | 2 bytes | Range of whole number starting from -32,768 to 32,767 | short j = 1000; | 0 |
long | 8 bytes | Range of whole number starting from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | long l = 100000L; | 0 |
boolean | 1 bit | boolean data type store true or false | boolean h = true; | false |
char | 2 bytes | Char data types stores a single ASCII values/Character/Letter | char f ='f'; |
Program :
import java.util.*; class DT{ static int a; static float c; static char e; static boolean g; static short i; static long k; static byte m; static double o; public static void main(String args[]){ //........program to print the java data types , their size , and their default values....... int b=10; float d=10.00f; char f ='f'; boolean h = true; short j = 1000; long l = 100000L; byte n = 20; double p = 12.34; System.out.println(" Default Vaule of Int a is = ( "+a+" ) and Value of int b is = "+b); System.out.println(" Default Vaule of Float c is = ( "+c+" ) and Value of Float d is = "+d); System.out.println(" Default Vaule of Char e is = ( "+e+" ) and Value of Char f is = "+f); System.out.println(" Default Vaule of Boolean g is = ( "+g+" ) and Value of Boolean h is = "+h); System.out.println(" Default Vaule of Short i is = ( "+i+" ) and Value of Short j is = "+j); System.out.println(" Default Vaule of Long k is = ( "+k+" ) and Value of Long l is = "+l); System.out.println(" Default Vaule of Byte m is = ( "+m+" ) and Value of Byte n is = "+n); System.out.println(" Default Vaule of Double o is = ( "+o+" ) and Value of Double p is = "+p); }//....end of main function..... }//....end of DT class..............
Explanation of ( Java Data Types Program):
This program will execute the data types and their default values.
We use data types in java like int, float, char, etc with variables define inside the static main function and outside the main function with the static keyword.
First, we declare the variable with data types with static keyword inside the main function,
Second, we initialize new variables with the same data types having some values to print.
Third, we print the value by using “System.out.println” to display the result.
Learn More about the Java data types