Java Features
Platform Independence
Object-Oriented
Robust
Secure
Portable
Multithreaded
Automatic Memory Management
Versatile Standard Libraries
Scalable
Java Platform
The platform is independent of any specific hardware or operating system due to the Java
Virtual Machine (JVM).
Components of the Java Platform
Java Development Kit (JDK)
A complete development environment for building Java applications.
Java Runtime Environment (JRE)
Provides the runtime environment to execute Java programs.
Java Virtual Machine (JVM)
The heart of the Java platform
Converts Java bytecode into machine-specific instructions
Provides platform independence
Data Types
1. Primitive Data Types
Integer Types
Byte
Short
Int
long
Floating-Point Types
Float
double
Character Type
char
Boolean Type
boolean
2. Non-Primitive Data Types
Strings
Arrays
Classes and Objects
Interfaces
Variables in Java
Local Variables
Declared inside methods, constructors, or blocks.
Accessible only within the block where declared.
Must be initialized before use.
Example:
public void display() {
int num = 10; // Local variable
System.out.println(num);
}
Instance Variables
Declared inside a class but outside any method.
Belong to an instance of the class (object).
Have default values (e.g., 0 for int, null for reference types).
Example:
public class Example {
int age; // Instance variable
}
Static Variables (Class Variables)
Declared with the static keyword.
Shared among all instances of the class.
Stored in a common memory area.
Example:
public class Example {
static int count; // Static variable
}
Arrays in Java
One-Dimensional Array
A single row of elements.
Example:
int[] numbers = {1, 2, 3, 4};
Multi-Dimensional Array
Arrays with two or more dimensions.
Example:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
Expressions in Java
Types of Expressions
1. Arithmetic Expressions: Perform mathematical calculations.
int result = 5 + 3; // result = 8
2. Relational Expressions: Evaluate to a boolean value based on comparisons.
boolean isGreater = 5 > 3; // true
3. Logical Expressions: Combine boolean values using logical operators.
boolean result = (5 > 3) && (4 < 6); // true
4. Assignment Expressions: Assign a value to a variable.
int x = 10;
5. Conditional Expressions: Use the ternary operator for compact decision-making.
int max = (a > b) ? a : b;
Operators in Java
Control Structures in Java
1. Decision-Making Statements
if Statement:
if (x > 0) {
System.out.println("Positive number");
}
if-else Statement:
if (x > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative or zero");
}
if-else-if Ladder:
if (x > 0) {
System.out.println("Positive");
} else if (x < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
switch Statement:
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
2. Looping Statements
for Loop:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Enhanced for Loop (for arrays or collections):
int[] numbers = {1, 2, 3};
for (int num : numbers) {
System.out.println(num);
}
while Loop:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
do-while Loop:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
3. Jump Statements
break: Exits a loop or a switch statement.
for (int i = 0; i < 5; i++) {
if (i == 3) break;
System.out.println(i);
}
continue: Skips the current iteration and moves to the next.
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
System.out.println(i);
}
return: Exits from the current method.
if (x < 0) return;