While working with programming languages, you will often come across something called “data types”. These are like labels that tell the computer what kind of data the computer memory will be holding. In this article, we will talk about understanding `Java Data Types’ and their categories.
We all know Java is a statically typed language, meaning Java Data Type of variable should be defined
in advance. This way, the computer gets to know how much space to allocate.
In Java, Data types are mainly classified into two types:
1. Primitive Data types
2. Non-Primitive (Reference) Data types
Primitive Data Types: These are a basic data type that stores the actual value. Primitive Data types are allocated Stack memory.
Non-Primitive (Reference) Data Types: Reference data type stores references to objects meaning the point to addresses of actual data rather than storing them directly. E.g. – objects, array, enums etc. In this discussion, we will explore 8 Primitive Java Data Types
In this discussion, we will explore 8 Primitive Java Data Types.
If you face any difficulties learning about the below data types then worry not, you can take Java homework assistance from CodingZap experts and clear your confusion.
1. byte
The `byte` is a primitive data type used to represent small signed integer value using 8 bits (1 byte).
Size: A `byte` occupies 8 bits in memory.
Range: It can hold value from -128 to 127
Use: Byte is typically used when memory saving is more important. Often used in cases like read/write
binary files, networking and other low-level operations.
Default Value: 0
Example Code for Byte data type:
public class ByteExample {
public static void main(String[] args) {
byte temperature = -10;
System.out.println(temperature);
}
}
In this example, the `temperature` variable is declared as a byte and assigned the value -10. Since -10
falls within the range of a byte, this code is valid and demonstrates the use of the` byte` data type.
2. short
Size: A `short` takes up 16 bits (2 bytes) in memory.
Range: minimum value of -32,768 to maximum value of 32,767
Use : Due to its larger size compared to `byte` , it is used to store the value that exceeds -128 to -127
range but still within limited range of numbers.
Default Value: 0
Example Java Code short data type:
public class ShortExample {
public static void main(String[] args) {
short studentsCount = 20000;
System.out.println("student count: " + studentsCount);
}
}
Output:
student count: 20000
In this example, `studentsCount` variable is declared as `short` as it will hold value 20000 as it falls
within its range.
3. int
`int` data type is used to store integer number.
Size: int data type takes up 32 bits (4 bytes) in memory.
Range: -2^31 to 2^31 + 1
Use: It is most used data type in programming due to its extended range and suitable for mathematical calculations.
Default Value: 0
Example Java Code int data type:
public class IntExample {
public static void main(String[] args) {
int bankBalance = 8000000;
System.out.println("Balance in Bank: " + bankBalance);
}
}
Output:
Balance in Bank: 8000000
In this code, an integer variable `bankBalance` is declared and assigned the value 8000000 (8 million)
since 8 million falls between approximate range of 2.1 billion. Therefore, `int` is suitable data type to
hold such values.
4. long
Size: long data type takes up 64 bits (8 bytes) in memory.
Range: -2^63 to 2^63 + 1
Use: long is used when dealing with larger numbers. Remember long occupies more memory and it is
slower than int, short and byte in terms of processing.
Default Value: 0L (L is suffix)
Example Java Code long data type:
public class LongExample {
public static void main(String[] args) {
long worldPopulation = 7800000000L;
System.out.println("World Population: " + worldPopulation);
}
}
Output:
World Population: 7800000000
In this code, `worldPopulation` is declared as long and assigned the value `7800000000L`. The use of
`L` at the end of 7800000000 indicates it’s a long literal.
**Starting from Java Se 8 or later version, long has unsigned 64 bit range where minimum is 0 and
maximum is 2^64-1.
5. float
The `float` data type is a 32-bit single-precision data type. As the name suggests, it is used to store
floating-point numbers. However, it has limitations on precision. The float data type internally rounds
off values, making it unsuitable for scenarios where precision is crucial. There’s difference between double and float datatypes in Java.
Size: long data type takes up 64 bits (8 bytes) in memory.
Range: range of approximately ±1.4e-45 to ±3.4e+38.
Use: Use the float data type when you need to store decimal values and are willing to trade off some
precision for memory efficiency.
Default Value: 0.0f (f is suffix)
Example Java Code float data type:
public class FloatExample {
public static void main(String[] args) {
float distance = 150.25f;
float time = -42.3e3f; // Scientific Notation -42.3 * 10 ^3
System.out.println("Distance: " + distance);
System.out.println("Time: " + time);
}
}
Output:
Distance: 150.25
Time: -42300.0
In above code and output, while `distance` variable declared demonstrate the use of float with suffix
f, `time` variable demonstrate the scientific way of declaring float value.
6. double
The `double` data type is a 64-bit double-precision data type. `double` can stored much larger decimal
value and provides more accuracy as compared to double.
By Default, If Suffix `L` is not present then floating–point literals are considered as `double`.
Size: 64 bit
Range: ±4.9e-324 to ±1.8e+308.
Use: Helpful in financial calculations when we need to store decimal values with higher precision.
However, minimal rounding errors will still be there.
Default Value: 0.0
Example Java Code double data type:
public class DoubleExample {
public static void main(String[] args) {
double gravity = 27.5;
double pi = 3.141592653589793;
System.out.println("Gravity: " + gravity);
System.out.println("Pi: " + pi);
}
}
Output:
Gravity: 27.5
Pi: 3.141592653589793
In the code, we can observe two `double` variable each having different precisions which demonstrate
the use and range of `double` data type
7. char
`char` data type is used to represent a single character. It occupies 16 bits of space in memory.
It represent this characters using Unicode encoding which allows itto handle single characters, letters,
symbols, punctuation, escape sequences etc.
Use: Helpful in processing strings, handling text input and performing character based operations.
Default Value: ‘\u0000’ (0)
Example Java Code char data type:
public class CharExample {
public static void main(String[] args) {
char letter = 'A';
char digit = '5';
char percent = '%';
char e = '\u0065';
System.out.println("Letter: " + letter);
System.out.println("Digit: " + digit);
System.out.println("Symbol: " + percent);
System.out.println("Small E: " + e);
}
}
Output:
Letter: A
Digit: 5
Symbol: %
Small E: e
The output displays the characters for ‘A’, ‘5’, and ‘%’ as expected, and it shows the lowercase letter
‘e’ for the e variable, which was assigned the Unicode code point \u0065.
8. boolean
`boolean` data type has only two values `true` and `false`.
Default value: false
Use: It is mainly used in conditional statements and expressions to determine whether certain
conditions are met or not. Often used with ( >= , <= , ! , == , || , && ) operators.
Example Java Code boolean data type:
public class BooleanExample {
public static void main(String[] args) {
boolean isRaining = true;
boolean hasUmbrella = false;
boolean office = (isRaining && !hasUmbrella) ;
System.out.println("Should Go to Office ? : " + office);
// Consider true = Yes and False = No
}
}
Output:
Should Go to Office ? : true
In this example, the code uses boolean variables to represent whether it’s raining and whether the
person has an umbrella. The program then uses logical operators to check the conditions and prints
whether person should go to office or not.
Conclusion:
In Summary, Primitive Data types stored the actual value and are more memory efficient. In Contrast,
Reference Data types store the reference or we can say it points to the address of actual data.
Understanding which data type to use in application is crucial as it helps to avoid errors and saves
memory. If you’re interested to know about data types in JavaScript , you can check out our article