Java Primitive Data Type Values

The Java Programming Language supports eight primitive data types. All variables must first be declared before they can be used in Java. A variable’s data type determines the values it may hold and the operations that may be performed upon it. A primitive type is predefined by the language and is named by a reserved keyword.

Below is the list of each Java primitive data type that includes the size of each type, the minimum value and maximum value that the data type can hold.

byte

  • SIZE : 8 bit integer
  • MIN_VALUE : -128
  • MAX_VALUE : 127

short

  • SIZE : 16 bit integer
  • MIN_VALUE : -32768
  • MAX_VALUE : 32767

int

  • SIZE : 32 bit integer
  • MIN_VALUE : -2147483648
  • MAX_VALUE : 2147483647

long

  • SIZE : 64 bit integer
  • MIN_VALUE : -9223372036854775808L
  • MAX_VALUE : 9223372036854775807L

float

  • SIZE : 32 bit integer
  • MIN_VALUE : 1.401298464324817E-45f
  • MAX_VALUE : 3.4028234663852886E38f

double

  • SIZE : 64 bit integer
  • MIN_VALUE : 4.9E-324
  • MAX_VALUE : 1.7976931348623157E308

boolean

  • SIZE : 1 bit
  • VALUES : true,false

char

  • SIZE : 16 bit
  • MIN_VALUE : 0
  • MAX_VALUE : 65535

When you declare a variable in Java and do not initialize that variable depending on the compiler you may or may not get a warning or error. The compiler will sometimes assign default values to data types. The table below shows the default values for each data type. However, relying on the compiler to give a variable a default value may not be a good programming practice.

Default Values

Data Type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char ‘\u0000’
String null
boolean false


Let’s do some Java coding. The sample Java code below defines and initializes each primitive data type to the MAX_VALUE each can hold. The method called printPrimitive() will print out both the MIN_VALUE and MAX_VALUE of each data type.

Java Primitive Data Types Source Code:

package com.jcd.java.tutorials;

public class JavaDataTypes 
{

	public static void main(String[] args) 
	{
				
		//Define and initialize a byte with max value
		byte myByte = 127;
		System.out.println("myByte = "+myByte);
		
		//Define and initialize a short with max value
		short myShort = 32767;
		System.out.println("myShort = "+myShort);
		
		//Define and initialize an int with max value
		int myInt = 2147483647;
		System.out.println("myInt = "+myInt);
		
		//Define and initialize a long with max value
		long myLong = 9223372036854775807L;
		System.out.println("myLong = "+myLong);
		
		//Define and initialize a float with max value
		float myFLoat = 3.4028234663852886E38f;
		System.out.println("myFLoat = "+myFLoat);
		
		//Define and initialize a double with max value
		double myDouble = 1.7976931348623157E308;
		System.out.println("myDouble = "+myDouble);
		
		//Define and initialize a boolean with default value
		boolean myBoolean = false;
		System.out.println("myBoolean = "+myBoolean);

		
		//Print out the primitive min/max values
		printPrimitive();
		

	}
	
	//Method to print our minimum/maximum values of the primitive data types
	public static void printPrimitive() 
	{
		
		System.out.println("\nbyte MIN_VALUE  : " +Byte.MIN_VALUE);
        System.out.println("byte MAX_VALUE  : " +Byte.MAX_VALUE);
        System.out.println("short MIN_VALUE : " +Short.MIN_VALUE);
        System.out.println("short MAX_VALUE : " +Short.MAX_VALUE);
        System.out.println("int MIN_VALUE : " +Integer.MIN_VALUE);
        System.out.println("int MAX_VALUE : " +Integer.MAX_VALUE);
        System.out.println("long MIN_VALUE : " +Long.MIN_VALUE);
        System.out.println("long MAX_VALUE : " +Long.MAX_VALUE);
        System.out.println("float MIN_VALUEe  : " +Float.MIN_VALUE);
        System.out.println("float MAX_VALUE : " +Float.MAX_VALUE);
        System.out.println("double MIN_VALUE : " +Double.MIN_VALUE);
        System.out.println("double MAX_VALUE : " +Double.MAX_VALUE);

    }	

}


Java Primitive Data Types Output:

myByte = 127
myShort = 32767
myInt = 2147483647
myLong = 9223372036854775807
myFLoat = 3.4028235E38
myDouble = 1.7976931348623157E308
myBoolean = false

byte MIN_VALUE  : -128
byte MAX_VALUE  : 127
short MIN_VALUE : -32768
short MAX_VALUE : 32767
int MIN_VALUE : -2147483648
int MAX_VALUE : 2147483647
long MIN_VALUE : -9223372036854775808
long MAX_VALUE : 9223372036854775807
float MIN_VALUEe  : 1.4E-45
float MAX_VALUE : 3.4028235E38
double MIN_VALUE : 4.9E-324
double MAX_VALUE : 1.7976931348623157E308



Java References:

  1. Java Boolean Class
  2. Java String Class
  3. Java Long Class
  4. Java Integer Class
  5. Java Double Class
  6. Java Byte Class
  7. Java Short Class
  8. Java Float Class
  9. Java Convert Integer To String Example
  10. Java Convert Boolean To String Example
2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *