Java Data Types

Java Data Types Examples

Data Types in Java

What are Java Data Types ?

Data Type Range Memory
(in bytes)
Default
Value
boolean true or false 1 bit false
byte -128 to 127 1 0
char any character or 0 to 65535 2 '\u0000'
short -32768 to 32767 2 0
int -2147483648 to 2147483647 4 0
float 1.40129846432481707e-45 to 3.40282346638528860e+38 4 0.0f
long -9223372036854775808 to 9223372036854775807 8 0L
double 4.94065645841246544e-324d to 1.79769313486231570e+308d 8 0.0d

Java boolean Data Type Example

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

public class BooleanDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF boolean IS false

		boolean b;

		System.out.println("Java boolean Data Type Example");

		//ASSIGN A VALUE

		b = true;

		System.out.println("Assigned boolean Value : " + b);
	}
}
				    

Java byte Data Type Example

/*  
    Java byte Data Type Example
    Save with file name ByteDataType.java
*/

public class ByteDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF byte IS  0

		byte b;

		System.out.println("Java byte Data Type Example");

		//ASSIGN A VALUE

		b = 77;

		System.out.println("Assigned byte Value : " + b);
	}
}
				    

Java char Data Type Example

/*  
    Java char Data Type Example
    Save with file name CharDataType.java
*/

public class CharDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF char IS '\u0000'

		char c;

		System.out.println("char Example");

		//ASSIGN A VALUE

		c = 'A';

		System.out.println("Assigned char Value : " + c);

		//ASSIGN small a ascii value

		c = 97;

		System.out.println("Assigned char ASCII Value : " + c);
	}
}
				    

Java short Data Type Example

/*  
    Java short Data Type Example
    Save with file name ShortDataType.java
*/

public class ShortDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF short IS 0

		short s;

		System.out.println("Java short Data Type Example");

		//ASSIGN A VALUE

		s = 123;

		System.out.println("Assigned short Value : " + s);
	}
}
				    

Java int Data Type Example

/*  
    Java int Data Type Example
    Save with file name IntDataType.java
*/

public class IntDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF int IS 0

		int i;

		System.out.println("Java int Data Type Example");

		//ASSIGN A VALUE

		i = 5;

		System.out.println("Assigned int Value : " + i);
	}
}
				    

Java float Data Type Example

/*  
    Java float Data Type Example
    Save with file name FloatDataType.java
*/

public class FloatDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF float IS 0f

		float a;

		System.out.println("Java float Data Type Example");

		//ASSIGN A VALUE LIKE THIS COMPILER SHOW ERROR
		//POSSIBLE LOSS OF PRECISION FOUND DOUBLE REQUIRED FLOAT
		//a = 5.25; //GET COMPILATION ERROR COMPILER ASSUMES double

		a = 5.25f; //ASSIGN A float VALUE

		System.out.println("Assigned float Value : " + a);
	}
}
				    

Java long Data Type Example

/* 
    Java long Data Type Example
    Save with file name LongDataType.java
*/

public class LongDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF long IS 0L

		long a;

		System.out.println("long Example");

		//ASSIGN A  VALUE

		a = 45000L;

		System.out.println("Assigned long Value : " + a);
	}
}
				    

Java double Data Type Example

/*
    Java double Data Type Example
    Save with file name DoubleDataType.java
*/

public class DoubleDataType
{
	public static void main(String args[])
	{
		//TYPE DECLARATION DEFAULT VALUE OF double IS 0d

		double a;

		System.out.println("double Example");

		//ASSIGN A double VALUE

		a = 5.25d;

		System.out.println("Assigned double Value : " + a);
	}
}