Java primitive data types program

Java Data types Program (Primitive)


There are 8 primitive data types in java :

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

Their Default Size and Range :

Data TypesSizeRangeExamplesDefault
byte1 byteRange of whole number starting from -128 to 127byte n = 20;0
Int4 bytesRange of whole number starting from -2,147,483,648 to 2,147,483,647int b=10;0
float4 bytesFractional Number store 6 to 7 decimal digits/pointsfloat d=10.00f;0.0
double8 bytesFractional Number store 15 decimal digits/pointsdouble p = 12.34;0.0
short2 bytesRange of whole number starting from -32,768 to 32,767short j = 1000;0
long8 bytesRange of whole number starting from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807long l = 100000L;0
boolean1 bitboolean data type store true or falseboolean h = true;false
char2 bytesChar data types stores a single ASCII values/Character/Letterchar 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