Java primitive data types

Java knows different variables depending on which data type you want to store. You can declare 8 primitive data types in Java namely: byte, short, int, long, float, double, char and boolean. The data types are divided into 4 categories as shown in the following table.

Data Type

Standard value

int 0
double 0.0
boolean false
String null

data-types

Data Type (bits)

Range, Description & Examples

Integer Type
byte (8 bits) -2⁷ to 2⁷ – 1
The byte type is small and can be used to save memory. Its default value is 0. Example: byte b = 20;
short (16 bits) -2¹⁵ to 2¹⁵ – 1
The short type can be also used to save memory. Its default value is 0.
Example: short s = 500;
int (32 bits) -2³¹ to 2³¹ – 1
The int type can be used for bigger values. Its default value is 0.
Example: int i = 2500;
long (64 bits)  -2⁶³ to 2⁶³ – 1
The long type can be used when you need a range of values wider than those of
int. Its default value is 0.
Example: long l = 23333333333;
Floating-point Type
float (32) ~ -3.4 x 10³⁸ to ~ 3.4 x 10³⁸
The float type can be used when floating-point types and values are needed.
Example: float f = 1.4f
double (64) ~ -1.8 x 10³⁰⁸ to ~1.8 x 10³⁰⁸
The double type can be used when floating-point types and values are needed.
double is a default choice for decimal values.
Example: double d = 22.3;
Character Type
 char (16)  0 to 65,535
The char type can be used by character types like a, b, c, d
Example: char letter = ‘d’;
Boolean Type
 boolean (1) The boolean type can only have two possible values either true or false.  It is false by default.
Example: boolean bool = true;

Example:

What is written to the standard output when the following program is compiled and run?

public class PrimitiveData {

	public static void main(String[] args){
		byte b = 47;
		short s = 77;
		int i = 455;
		long l = 1200;
		float f = 277.6f;
		double d = 120.93;
		boolean bool = true;
		char c = 'e';

		System.out.println(b);
		System.out.println(s);
		System.out.println(i);
		System.out.println(l);
		System.out.println(f);
		System.out.println(d);
		System.out.println(bool);
		System.out.println(c);
	}
}

47
77
455
1200
277.6
120.93
true
e

Exercise:

What happens when the following program is compiled and run?

public class PrimitiveData {

	public static void main(String[] args){
		int i = 20;
		int i2 = 3;
		int i3 = i * i2 - 5;
		System.out.println(i3);
	}
}

Select the correct answer.





Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

4 Comments

Add a Comment
  1. Thank you for your explanation. You are a best teacher.
    I have a question.

    How to write a symbols of{} and ; when I test my new code? So very difficult for me to arrange these two symbols clearly. Thank you.

    1. I don’t understand exactly what you mean. Could you make your point clear? So that I can answer your question!

  2. Easy explanation, thanks!

    1. Thanks, Ryan for your comment!

Leave a Reply

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