Data type In Java
Data type In Java
These are the most basic types and represent single values. Java has 8 primitive data types:
1. byte
o Size: 1 byte (8 bits)
o Range: -128 to 127
o Example: byte b = 100;
2. short
o Size: 2 bytes (16 bits)
o Range: -32,768 to 32,767
o Example: short s = 10000;
3. int
o Size: 4 bytes (32 bits)
o Range: -2^31 to 2^31 - 1
o Example: int i = 100000;
4. long
o Size: 8 bytes (64 bits)
o Range: -2^63 to 2^63 - 1
o Example: long l = 10000000000L;
5. float
o Size: 4 bytes (32 bits)
o Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits)
o Example: float f = 3.14f;
6. double
o Size: 8 bytes (64 bits)
o Range: Approximately ±1.7976931348623157E+308 (15 significant decimal
digits)
o Example: double d = 3.14159265358979;
7. char
Size: 2 bytes (16 bits)
o
Range: 0 to 65,535 (Unicode characters)
o
Example: char c = 'A';
o
8. boolean
o Size: 1 bit (not precisely specified, often 1 byte for practical reasons)
o Values: true or false
o Example: boolean flag = true;
Reference data types are used to refer to objects and arrays. These include:
1. Classes: A class is a blueprint for creating objects, which contain both data and
methods.
Example: String str = "Hello";
2. Interfaces: An interface defines a contract for classes to implement, specifying a set
of methods without providing the implementation.
Example: Runnable r = new MyRunnable();
3. Arrays: An array is a collection of elements of the same type.
Example: int[] arr = {1, 2, 3};
In Java, all reference data types store references (memory addresses) to the actual objects, not
the objects themselves.
Summary
Primitive Types: byte, short, int, long, float, double, char, boolean.
Reference Types: Objects, arrays, classes, and interfaces.