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 Type | Description |
---|---|
int | 32 bit integer. Has constant size of 4 bytes on all platforms. |
short | 16 bit integer. Constant 2 byte size. Is not very popular in Java |
long | 64 bit integer. Same size on all OS. Is rarely used in real life Java. projects. |
float | 32 bit floating value. |
double | 64 bit floating value. |
byte | 1 byte of Java :-0 |
boolean | Stores either true or false. null value is not permitted. It’s size is implementation dependent but usually more than 1 bit. |
char | 16 bit Unicode character |
Java Primitive Data Types
What is an int Primitive Data Type?
- Uses 4 bytes
- Represents a signed integer. Java 8 onwards you can Integer class to represent an unsigned integer.
- Is in 2’s complement form
- 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.
- Minimum value of -2**31 for unsigned and 0 for unsigned.
- Has a default value of 0
- Example int x = 42;
What is short Primitive Data Type?
- Uses 2 bytes
- Since Java programmers are not overly obsessed with saving bits and bytes, short is not used much.
- Java programmers tend to use int even where short would do. Saving those 2 bytes is not preferred over culture shock of using short.
- Is a signed data type
- Is in 2’s complement form
- Maximum value of 32,767 which is 2**16 – 1
- Minimum value of -32,768 which is -2**16
- Like int has default value of 0
- Example short guy = 0 ; // ?
What is float Primitive Data Type?
- Uses 4 bytes – is referred to as single precision
- 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.
- Default value is 0.0
- Never use float values for money calculation. Use BigDecimal instead.
- If you have already read What Every Computer Scientist Should Know About Floats , you would not bother about its range.
- 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?
- Uses 2 bytes
- Minimum value is ‘\u0000’ which is 0
- Maximum value is ‘\uffff’ which is 65,535
- Can represent special characters
- Default value is 0
- Allows you to print supplement symbols such as ©, µ , Þ, ß, ½ and so on
- Allows you to support non Latin languages such as Hebrew – י ך כ ל ם מ ן נ ס ע ף , Hindi – ँ ं ः अ आ इ ई उ ऊ ऋ ऌ ऍ ऎ ए ऐ ऑ ऒ ओ औ क ख ग घ ङ च छ ज झ ञ ट ठ ड ढ ण त थ द ध न ऩ प फ ब भ म य र ऱ ल ळ ऴ व श ष स ह ़ , and so on.
What is boolean Primitive Data Type?
- Has a value true or false
- Is used in decision making structure
- Default value is false
- DOES NOT use just 1 bit
- Generally, the size is 1 byte. It is always more than 1 bit on all available JDKs.
- 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?
- Obviously uses 1 byte
- Is an integer
- Is signed
- Is in 2’s complement form
- Maximum value is 128
- Minimum value is -127
- Was intended to conserve space (much like short) when integer size is known to be small
- If you find yourself using Java byte and you are not in one of the few niche domains, ponder deep over your situation.