Java is a statically typed programming language, this means that variables must be declared before its use. So, in Java when a variable is created we have to specify its data type as well. This will define what type of values it can take and what type of operations it can perform.
For example, in this statement
Primitive data types Object data types
In Java, Primitive data types are predefined and named using reserved keywords by the language. There are several advantages with primitive data types over object data types.
- Better performance, since there is no need to instantiate an object
-
There is no need to check for
null values, since there is no reference to an object - There is no
GC (garbage collection) process on primitive data types, as the variable goes out of scope it gets freed immediately.
Below are the pritive data types available in Java programming language.
short
The
Example:
- We can use
short data type to save memory in large arrays, in situations where the memory savings really matters and we know the numbers are within short data type range.
int
The
Example:
-
Starting from Java 8, we can use
int to represent an unsigned 32-bit integer as well, which has range from 0 to 232-1, useInteger class object data type to useint data type as an unsigned integer. -
int data type takes 4 bytes of memory.
long
The
Example:
-
Starting from Java 8, we can use
long to represent an unsigned 64-bit long, which has range from 0 to 264-1, useLong class object data type to uselong data type as an unsigned long. -
long data type takes 8 bytes of memory.
When you declare a variable value using a suffix, you can also declare the value with underscore "_" symbol.
For example long a = 1_00_123L;
(if this make it more readable, you can use this format.)
float
The
-
If you need to save memory in large arrays of floating point numbers,
float data type can be used. -
Never use
float data type for precise values such as currency, use eitherdouble orjava.math.BigDecimal for that purpose. -
float data type takes 4 bytes of memory.
double
The
- This data type is the default choice for any decimal values.
-
It has accuracy up to 15 decimals, consider using
java.math.BigDecimal if you need more accuracy. -
double data type takes 8 bytes of memory.
char
The
-
It has a minimum value of
'\u0000' (or 0) and a maximum value of'\uffff' (or 65,535 inclusive) char takes 2 bytes of memory.
boolean
The
- Use this data type for simple flags that tracks
true orfalse conditions. boolean takes 1 bit of memory.
byte
The
- The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters.
- They can also be used in place of int where their limits are in the range of byte, this can help to clarify the code.
byte takes 1 byte of memory.
Object data types are the ones created by user, they refer to a particular object. These are also called reference data types. These includes Strings, arrays or any Classes created by user.
For example
Example:
- Objects give greater flexibility to model real-world things.
- You can alter the behavior of an Object using the reference of that object.
- Once the scope of references object is over, it will get marked for GC collection and eventually gets cleared out from memory.