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-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.


Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!
Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

2 Comments

Add a Comment
  1. Easy explanation, thanks!

    1. Thanks, Ryan for your comment!

Leave a Reply

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

Sarmaroof © 2017