Java Data Types

Java Data Types Examples

boolean Data Type
/*
    boolean Data Type Example
    Save with file name BooleanDataType.java
*/

public class BooleanDataType
{
	public static void main(String args[])
	{
		boolean b; //TYPE DECLARATION DEFAULT VALUE OF boolean IS false
		System.out.println();
		System.out.println("boolean Example");
		System.out.println("===============");
		System.out.println();
		b = true; //ASSIGN A VALUE
		System.out.println("Assigned boolean Value : " + b);
	}
}
                        
byte Data Type
/*  
    byte Data Type Example
    Save with file name ByteDataType.java
*/

public class ByteDataType
{
	public static void main(String args[])
	{
		byte b; //TYPE DECLARATION DEFAULT VALUE OF byte IS  0
		System.out.println();
		System.out.println("byte Example");
		System.out.println("============");
		System.out.println();
		b = 77; //ASSIGN A VALUE
		System.out.println("Assigned byte Value : " + b);
	}
}
                        
char Data Type
/*  
    char Data Type Example
    Save with file name CharDataType.java
*/

public class CharDataType
{
	public static void main(String args[])
	{
		char c; //TYPE DECLARATION DEFAULT VALUE OF char IS '\u0000'
		System.out.println();
		System.out.println("char Example");
		System.out.println("============");
		System.out.println();
		c = 'A'; //ASSIGN A VALUE
		System.out.println("Assigned char Value : " + c);
		c = 97; //ASSIGN small a ascii value
		System.out.println("Assigned char ASCII Value : " + c);
	}
}
                        
short Data Type
/*  
    short Data Type Example
    Save with file name ShortDataType.java
*/

public class ShortDataType
{
	public static void main(String args[])
	{
		short s; //TYPE DECLARATION DEFAULT VALUE OF short IS 0
		System.out.println();
		System.out.println("short Example");
		System.out.println("=============");
		System.out.println();
		s = 123; //ASSIGN A VALUE
		System.out.println("Assigned short Value : " + s);
	}
}
                        
int Data Type
/*  
    int Data Type Example
    Save with file name IntDataType.java
*/

public class IntDataType
{
	public static void main(String args[])
	{
		int i; //TYPE DECLARATION DEFAULT VALUE OF int IS 0
		System.out.println();
		System.out.println("int Example");
		System.out.println("=============");
		System.out.println();
		i = 5; //ASSIGN A VALUE
		System.out.println("Assigned int Value : " + i);
	}
}
                        
float Data Type
/*  
    float Data Type Example
    Save with file name FloatDataType.java
*/

public class FloatDataType
{
	public static void main(String args[])
	{
		float a; //TYPE DECLARATION DEFAULT VALUE OF float IS 0f
		System.out.println();
		System.out.println("float Example");
		System.out.println("=============");
		System.out.println();
		//ASSIGN A VALUE LIKE THIS COMPILER SHOW ERROR
		//POSSIBLE LOSS OF PRECISION FOUND DOUBLE REQUIRED FLOAT
		//a = 5.25; //GET ERROR COMPILER ASSUMES double

		a = 5.25f; //ASSIGN A float VALUE
		System.out.println("Assigned float Value : " + a);
	}
}
                        
long Data Type
/* 
    long Data Type Example
    Save with file name LongDataType.java
*/

public class LongDataType
{
	public static void main(String args[])
	{
		long a; //TYPE DECLARATION DEFAULT VALUE OF long IS 0L
		System.out.println();
		System.out.println("long Example");
		System.out.println("=============");
		System.out.println();
		a = 45000L; //ASSIGN A  VALUE
		System.out.println("Assigned long Value : " + a);
	}
}
                        
double Data Type
/*
    double Data Type Example
    Save with file name DoubleDataType.java
*/

public class DoubleDataType
{
	public static void main(String args[])
	{
		double a; //TYPE DECLARATION DEFAULT VALUE OF double IS 0d
		System.out.println();
		System.out.println("double Example");
		System.out.println("==============");
		System.out.println();
		a = 5.25d; //ASSIGN A double VALUE
		System.out.println("Assigned double Value : " + a);
	}
}