Download Java Data Types in Formats
QR Code for this Page
Java Data Types

Java Data Types - Data Types in Java, Primitive data types

Java Data Types Examples

Data Types in Java

Data type specifies the size and type of values that can be stored in an identifier. There are two types of data types in Java:

  1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
  2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

What are the 8 Primitive Data Types in Java ?

There are 8 Primitive data types in Java. The eight primitive data types in Java are:

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

What is a boolean Data Type in Java ?

boolean is a primitive data type in Java. boolean means true or false i.e. logical true or false. It occupies 1 bit in memory. These values do not correspond to 1 or 0 as in C or C++. The following example shows how to use boolean data type in Java.

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);
	}
}

What is a byte Data Type in Java ?

byte is a primitive data type in Java. byte data type holds numeric value between -128 and 127. The following example shows how to use byte data type in Java.

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);
	}
}

What is a char Data Type in Java ?

char is a primitive data type in Java. char data type holds any character or 0 to 65535 in it. The following example shows how to use char data type in Java.

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);
	}
}

What is a short Data Type in Java ?

short is a primitive data type in Java. short data type holds numeric value between -32768 and 32767. The following example shows how to use short data type in Java.

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);
	}
}

What is a int Data Type in Java ?

int is a primitive data type in Java. int data type holds numeric value between -2147483648 and 2147483647. The following example shows how to use int data type in Java.

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);
	}
}

What is a float Data Type in Java ?

float is a primitive data type in Java. float data type holds numeric value between 1.40129846432481707e-45 and 3.40282346638528860e+38. The following example shows how to use float data type in Java.

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);
	}
}

What is a long Data Type in Java ?

long is a primitive data type in Java. long data type holds numeric value between -9223372036854775808 and 9223372036854775807. The following example shows how to use long data type in Java.

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);
	}
}

What is a double Data Type in Java ?

double is a primitive data type in Java. double data type holds numeric value between 4.94065645841246544e-324d and 1.79769313486231570e+308d. The following example shows how to use double data type in Java.

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);
	}
}
Download Java Data Types in Formats