In Java, data types specify the different sizes and values that variables can hold. Java is a strongly typed language, which means every variable must be declared with a data type.
Java data types can be categorized into two types:
- Primitive Data Types
- Non-Primitive Data Types
Primitive data types are predefined by the Java language and named by a reserved keyword.
- Size: 8-bit
- Range: -128 to 127
- Default Value: 0
- Usage: To save memory in large arrays where the memory savings actually matters.
byte age = 30;
- Size: 16-bit
- Range: -32,768 to 32,767
- Default Value: 0
- Usage: To save memory in large arrays, slightly more efficient than
byte
.
short temperature = 250;
- Size: 32-bit
- Range: -2^31 to 2^31-1
- Default Value: 0
- Usage: Default data type for integral values unless there is a concern about memory.
int phoneNumber = 1234567890;
- Size: 64-bit
- Range: -2^63 to 2^63-1
- Default Value: 0L
- Usage: When a wider range than
int
is needed.
long bigNumber = 12345678990L;
- Size: 32-bit
- Range: Varies
- Default Value: 0.0f
- Usage: Use this when you need a fractional number and memory savings are important.
float pi = 3.14F;
- Size: 64-bit
- Range: Varies
- Default Value: 0.0d
- Usage: Default data type for decimal values.
double decimalValue = 3.123;
- Size: 16-bit
- Range: '\u0000' (0) to '\uffff' (65,535)
- Default Value: '\u0000'
- Usage: Used to store any character.
char letter = '@';
- Size: Depends on the JVM
- Values: true or false
- Default Value: false
- Usage: For simple flags that track true/false conditions.
boolean isAdult = true;
Non-primitive data types are also called reference types because they refer to objects. They include classes, interfaces, and arrays.
- Usage: To store a sequence of characters.
- Special Note: Strings are immutable in Java.
String name = "Anubhav";
System.out.println("Name: " + name);
- Concatenation: Using
+
operator orconcat()
method. - charAt(): Returns the character at a specified index.
- length(): Returns the length of the string.
- replace(): Replaces each occurrence of a character with another character.
- substring(): Extracts a substring from the string.
String firstName = "Anubhav";
String lastName = "Raj";
String fullName = firstName + " " + lastName; // Using + operator
String fullName2 = firstName.concat(" ").concat(lastName); // Using concat() method
System.out.println("Full Name: " + fullName);
System.out.println("Full Name (using concat): " + fullName2);
System.out.println("First character of the name: " + name.charAt(0));
System.out.println("Length of the name: " + name.length());
System.out.println("New String: " + name.replace('A', 'B'));
System.out.println("Substring: " + name.substring(1, 5));
- Usage: To store multiple values of the same type in a single variable.
- Special Note: Arrays in Java are objects and are dynamically allocated.
int[] numbers = {90, 85, 88, 92, 78};
int[] marks = new int[6];
marks[0] = 90;
marks[1] = 85;
marks[2] = 88;
marks[3] = 92;
marks[4] = 78;
System.out.println("Marks array: " + Arrays.toString(marks));
int[][] marks = {
{90, 85, 88, 92, 78},
{92, 88, 78, 85, 90}
};
System.out.println("Element at marks[1][0]: " + marks[1][0]);
System.out.println("First row: " + Arrays.toString(marks[0]));
System.out.println("Second row: " + Arrays.toString(marks[1]));
for (int i = 0; i < marks.length; i++) {
for (int j = 0; j < marks[i].length; j++) {
System.out.print(marks[i][j] + " ");
}
System.out.println();
}
System.out.println("Full marks array using Arrays.deepToString(): " + Arrays.deepToString(marks));
- Declaration:
- Syntax:
int[][] arrayName = new int[rows][columns];
- Alternative:
int[][] arrayName = { {row1}, {row2}, ... };
- Syntax:
- Accessing Elements:
- Use
arrayName[rowIndex][colIndex]
, where indices start from 0.
- Use
- Iterating Through 2D Arrays:
- Use nested loops to iterate through each element.
- Printing 2D Arrays:
- Use
Arrays.toString(arrayName[row])
to print a specific row. - Use
Arrays.deepToString(arrayName)
to print the entire 2D array.
- Use
- Common Operations:
- Transposing, rotating, and summing elements in rows or columns are common operations.
- Memory Considerations:
- 2D arrays in Java are arrays of arrays, meaning each "row" is actually a reference to another array.
- Initialization:
- If not explicitly initialized, 2D arrays of primitive types will be filled with their default values (e.g., 0 for int).
Conditional statements are used to perform different actions based on different conditions.
int age = 30;
if (age >= 18) {
System.out.println("You are eligible to vote");
}
boolean day = true;
if (day) {
System.out.println("It's a good day");
} else {
System.out.println("It's a bad day");
}
int marks = 85;
if (marks < 50) {
System.out.println("Fail");
} else if (marks >= 50 && marks < 60) {
System.out.println("D Grade");
} else if (marks >= 60 && marks < 70) {
System.out.println("C Grade");
} else if (marks >= 70 && marks < 80) {
System.out.println("B Grade");
} else if (marks >= 80 && marks < 90) {
System.out.println("A Grade");
} else {
System.out.println("A+ Grade");
}
int day = 4;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid day");
}
int age = 30;
String eligibility = (age >= 18) ? "You are eligible to vote" : "You are not eligible to vote";
System.out.println(eligibility);
Understanding Java data types is fundamental for programming in Java. This guide covers both primitive and non-primitive data types, their usage, and common operations. With this knowledge, you can efficiently handle data in your Java programs.