Primitive Data Types in Java

In the last lesson we learnt about Java variables. We also saw that Java is a static, strongly typed language. Each variable is associated with a data type. A data type tells the compiler and the runtime environment what kind of value is associated with the variable. Java has two kinds of data types – Primitive Data Types and Reference Types.

What is Primitive Data Type?

Primitive data types are the data types that have inbuilt support in Java and are stored in the Java stack of thread.

As a programmer you are expected to use them out of the box and not modify them. These primitive data types are used to represent the most elementary data types.

What is the Difference Between Primitive and Reference Data Types

The main characteristic difference between a primitive data type and reference data type is that a primitive data type variable directly stores the value. Another equally important difference is tha t primitive data types are much better at performance because they remain inside Java stack and are not part of the shared heap. Since a variable of primitive data type is known to exactly one thread, there are no concurrency issues.

A reference data type variable on the other hands stores the address of the value (which is object in this case). The value itself lies in the shared heap. Any Java thread that knows the address of object can go and modify the object. It is possible for multiple threads to simultaneously access an object and cause damage to the object. This is called race condition. A race condition can exist only for reference data types but never for primitive data type.

Another important distinction drawn is that primitive data types do not have methods but only value.

Eight Primitive Data Types in Java

These are the 8 primitive data types in Java –

Java Primitive Data Types

Primitive Data TypeDescription
int32 bit integer. Has constant size of 4 bytes on all platforms.
short16 bit integer. Constant 2 byte size. Is not very popular in Java
long64 bit integer. Same size on all OS. Is rarely used in real life Java. projects.
float32 bit floating value.
double64 bit floating value.
byte1 byte of Java :-0
booleanStores either true or false. null value is not permitted. It’s size is implementation dependent but usually more than 1 bit.
char16 bit Unicode character

Java Primitive Data Types

What is an int Primitive Data Type?

  1. Uses 4 bytes
  2. Represents a signed integer. Java 8 onwards you can Integer class to represent an unsigned integer.
  3. Is in 2’s complement form
  4. Maximum value that can be stored is 2**31 – 1 . This is the value when you use signed int. When using signed int, the max value is 2**32 – 1. Refer to this Javadoc API for a list of methods pertaining to unsigned int.
  5. Minimum value of -2**31 for unsigned and 0 for unsigned.
  6. Has a default value of 0
  7. Example int x = 42;

What is short Primitive Data Type?

  1. Uses 2 bytes
  2. Since Java programmers are not overly obsessed with saving bits and bytes, short is not used much.
  3. Java programmers tend to use int even where short would do. Saving those 2 bytes is not preferred over culture shock of using short.
  4. Is a signed data type
  5. Is in 2’s complement form
  6. Maximum value of 32,767 which is 2**16 – 1
  7. Minimum value of -32,768 which is -2**16
  8. Like int has default value of 0
  9. Example short guy = 0 ; // ?

What is float Primitive Data Type?

  1. Uses 4 bytes – is referred to as single precision
  2. Is of IEEE 754 float type.  If you intend to join banking, investments, trading, pricing or any such domain where measurements are involved, you must know this. It also makes a good interview question for weeding out fluffers and duffers from stuffers.
  3. Default value is 0.0
  4. Never use float values for money calculation. Use BigDecimal instead.
  5. If you have already read What Every Computer Scientist Should Know About Floats , you would not bother about its range.
  6. Example float onWater = 0.0 ; // ?

What is double Primitive Data Type?

A double data type is a float with double precision. All the points mentioned for float apply for double. One important distinction is that double uses 64 bits instead of 32. double is the preferred floating value data type in Java.

What is char Primitive Data Type?

  1. Uses 2 bytes
  2. Minimum value is ‘\u0000’ which is 0
  3. Maximum value is ‘\uffff’ which is 65,535
  4. Can represent special characters
  5. Default value is 0
  6. Allows you to print supplement symbols such as ©, µ , Þ, ß, ½ and so on
  7. Allows you to support non Latin languages such as Hebrew – י ך כ ל ם מ ן נ ס ע ף , Hindi – ँ ं ः अ आ इ ई उ ऊ ऋ ऌ ऍ ऎ ए ऐ ऑ ऒ ओ औ क ख ग घ ङ च छ ज झ ञ ट ठ ड ढ ण त थ द ध न ऩ प फ ब भ म य र ऱ ल ळ ऴ व श ष स ह ़ , and so on.

What is boolean Primitive Data Type?

  1. Has a value true or false
  2. Is used in decision making structure
  3. Default value is false
  4. DOES NOT use just 1 bit
  5. Generally, the size is 1 byte. It is always more than 1 bit on all available JDKs.
  6. Again, Java programmers don’t worry about conserving a few bits here and there. (There are exceptions, but they are not the norm).

What is byte Primitive Data Type?

  1. Obviously uses 1 byte
  2. Is an integer
  3. Is signed
  4. Is in 2’s complement form
  5. Maximum value is 128
  6. Minimum value is -127
  7. Was intended to conserve space (much like short) when integer size is known to be small
  8. If you find yourself using Java byte and you are not in one of the few niche domains, ponder deep over your situation.

Leave a comment

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