Data types in Java
In Java, data types are classified into two main categories: primitive data types and reference data types.
Primitive Data Types
Primitive data types are the most basic data types available in Java. They are predefined by the language and named by a reserved keyword. Java has eight primitive data types:
byte
Description: 8-bit signed integer.
Range: -128 to 127.
Example:- byte b = 100;
System.out.println("Byte value: " + b);
short
Description: 16-bit signed integer.
Range: -32,768 to 32,767.
Example:
short s = 10000;
System.out.println("Short value: " + s);
int
Description: 32-bit signed integer.
Range: -2^31 to 2^31 - 1.
Example:
int i = 100000;
System.out.println("Int value: " + i);
long
Description: 64-bit signed integer.
Range: -2^63 to 2^63 - 1.
Example:
long l = 10000000000L;
System.out.println("Long value: " + l);
float
Description: 32-bit floating-point number.
Range: Approximately ±3.40282347E+38F (6-7 significant decimal digits).
Example:
float f = 234.5f;
System.out.println("Float value: " + f);
double
Description: 64-bit floating-point number.
Range: Approximately ±1.79769313486231570E+308 (15 significant decimal digits).
Example:
double d = 123.456;
System.out.println("Double value: " + d);
char
Description: 16-bit Unicode character.
Range: '\u0000' (or 0) to '\uffff' (or 65,535).
Example:
java
Copy code
char c = 'A';
System.out.println("Char value: " + c);
boolean
Description: Represents one bit of information, but its "size" isn't precisely defined.
Range: true or false.
Example:
boolean flag = true;
System.out.println("Boolean value: " + flag);
Reference Data Types
Reference data types are based on classes, and they include objects, arrays, and interfaces. They store the reference (memory address) of the variable's value rather than the data itself.
String
Description: Represents a sequence of characters.
Example:
String str = "Hello, World!";
System.out.println("String value: " + str);
Arrays
Description: Container objects that hold a fixed number of values of a single type.
Example:
int[] arr = {1, 2, 3, 4, 5};
System.out.println("Array value: " + arr[2]);
Classes
Description: User-defined blueprints from which objects are created.
Example:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("John", 30);
p.display();
}
}
Interfaces
Description: Abstract types used to specify behaviors that classes must implement.
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}
Key Differences between Primitive and Reference Data Types
Memory Allocation:
Primitive types are allocated on the stack.
Reference types are allocated on the heap.
Default Values:
Primitive types have default values (e.g., 0 for int, false for boolean).
Reference types have a default value of null.
Methods and Operations:
Primitive types do not have methods. Operations on primitive types are carried out using operators.
Reference types can have methods that can be called to perform operations.
Understanding these data types and their characteristics is fundamental to programming in Java. They form the basis of how data is stored, manipulated, and accessed in your applications.
Comments
Post a Comment