What are data types in java?
In Java, data types specify the type of data that a variable can hold. Java is a strongly-typed language, meaning that each variable must be declared with a specific data type. This helps in ensuring that operations on the data are performed correctly. Java has two main categories of data types:
- Primitive Data Types
- Reference/Object Data Type
1. Primitive Data Types #
Primitive data types are the most basic data types built into the language. There are eight primitive data types in Java:
1. byte
- Size: 1 byte (8 bits)
- Range: -128 to 127
- Usage: Useful for saving memory in large arrays where the memory savings matter.
Example:
byte b = 100;
System.out.println("Byte value: " + b); // Output: Byte value: 100
2. short
- Size: 2 bytes (16 bits)
- Range: -32,768 to 32,767
- Usage: Can be used to save memory in large arrays when the range of values is known to be limited.
Example:
short s = 1000;
System.out.println("Short value: " + s); // Output: Short value: 1000
3. int
- Size: 4 bytes (32 bits)
- Range: -2^31 to 2^31-1
- Usage: Default data type for integral values unless there is a concern about memory.
Example:
int i = 100000;
System.out.println("Int value: " + i); // Output: Int value: 100000
4. long
- Size: 8 bytes (64 bits)
- Range: -2^63 to 2^63-1
- Usage: Used when a wider range than int is needed.
Example:
long l = 100000L;
System.out.println("Long value: " + l); // Output: Long value: 100000
5. float
- Size: 4 bytes (32 bits)
- Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits)
- Usage: Used when fractional precision is needed. Suffix f or F is mandatory.
Example:
float f = 10.5f;
System.out.println("Float value: " + f); // Output: Float value: 10.5
6. double
- Size: 8 bytes (64 bits)
- Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits)
- Usage: Default data type for decimal values. Use double for precise calculations.
Example:
double d = 10.5;
System.out.println("Double value: " + d); // Output: Double value: 10.5
7. boolean
- Size: 1 bit (Java uses 1 byte internally)
- Values: true or false
- Usage: Used for simple flags that track true/false conditions.
Example:
boolean flag = true;
System.out.println("Boolean value: " + flag); // Output: Boolean value: true
8. char
- Size: 2 bytes (16 bits)
- Range: 0 to 65,535 (unsigned)
- Usage: Used to store any character.
Example:
char c = 'A';
System.out.println("Char value: " + c); // Output: Char value: A
Summary #
Here is a consolidated example that uses all the primitive data types:
public class Main {
public static void main(String[] args) {
// byte example
byte b = 100;
System.out.println("Byte value: " + b);
// short example
short s = 1000;
System.out.println("Short value: " + s);
// int example
int i = 100000;
System.out.println("Int value: " + i);
// long example
long l = 100000L;
System.out.println("Long value: " + l);
// float example
float f = 10.5f;
System.out.println("Float value: " + f);
// double example
double d = 10.5;
System.out.println("Double value: " + d);
// boolean example
boolean flag = true;
System.out.println("Boolean value: " + flag);
// char example
char c = 'A';
System.out.println("Char value: " + c);
}
}
Output:
Byte value: 100
Short value: 1000
Int value: 100000
Long value: 100000
Float value: 10.5
Double value: 10.5
Boolean value: true
Char value: A