Java’s platform independence comes from its "Write Once, Run Anywhere" (WORA) nature. Java code is compiled into platform-neutral bytecode which is executed by a Java Virtual Machine (JVM).
Compile your code:
javac HelloWorld.javaRun on Windows, Linux, or macOS (provided a JVM is installed):
java HelloWorldVariables store data values. They must be declared with a specific type before use.
type variableName = value;- Declaring an integer variable:
int age = 25;
- Declaring a String variable:
String name = "John";
- Declaring multiple variables:
double salary = 50000.50; char grade = 'A';
Java has two main categories of data types:
These include:
byte,short,int,longfloat,doublecharboolean
int number = 10; // integer type
double price = 19.99; // floating point type
char letter = 'J'; // character type
boolean flag = true; // boolean typeThese refer to objects and include:
- Classes
- Interfaces
- Arrays
String text = "Hello, World!"; // Class instance
int[] numbers = {1, 2, 3, 4}; // Array instanceVariable names must start with a letter, $, or _ and are case-sensitive.
int studentAge = 20; // Correct
int _counter = 100; // Correct
int $amount = 50; // Correct
int 1stNumber = 5; // Incorrect: begins with a digitDifferent naming conventions help distinguish the purpose of identifiers in Java.
Used for variables and methods. The first letter is lowercase, and each subsequent word starts with an uppercase letter. Example:
int studentAge = 20;
String firstName = "Jane";Used for class names and constructors. Each word starts with an uppercase letter. Example:
public class StudentGrade {
public StudentGrade() { }
}Less common in Java, snake_case separates words with underscores. Example:
int student_age = 20;Used for constants. All letters are uppercase with words separated by underscores. Example:
public static final int MAX_SCORE = 100;| Feature | Primitive Data Types | Reference Data Types |
|---|---|---|
| Definition | Store simple values directly. | Store references to objects. |
| Memory Allocation | Fixed size in memory. | Dynamic memory allocation. |
| Examples | int, float, boolean |
String, Array, Object |
int x = 10; // primitive variable
String str = "Java"; // reference variablePrimitive types are stored in stack memory.
int x = 10; // Directly stored in stack as [10]
float f = 3.14F; // Directly stored in stack as [3.14]
boolean b = true; // Directly stored in stack as [true]
char c = 'A'; // Directly stored in stack as ['A']Reference types store memory addresses in the stack with the object in heap memory.
String str = "Java"; // 'str' holds a reference in stack, "Java" in heap
Person person = new Person(); // 'person' holds a reference in stack, Person object in heap
int[] arr = new int[5]; // 'arr' holds a reference in stack, array of integers in heap+-----------------+ +------------------------+
| Stack Memory | | Heap Memory |
|-----------------| |------------------------|
| int x = 10 | | |
| float f = 3.14 | | |
| boolean b = true| | |
| String str = Ref| ---> Ref ->| "Java" |
+-----------------+ +------------------------+
+-----------------+ +------------------------+
| Stack Memory | | Heap Memory |
|-----------------| |------------------------|
| String listRef | ---> Ref ->| ["Java", "Python"] |
| Person pRef | ---> Ref ->| Person object (fields) |
+-----------------+ +------------------------+
A String in Java is a sequence of characters. Strings are immutable, meaning their values cannot be changed once created. They are widely used for storing and manipulating text.
-
Using string literals:
String greeting = "Hello, World!";
-
Using the
newkeyword:String message = new String("Welcome to Java!");
-
Concatenating strings:
String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName; System.out.println(fullName); // Output: John Doe
-
Comparing strings:
String str1 = "Java"; String str2 = "Java"; System.out.println(str1.equals(str2)); // Output: true
-
String immutability:
String original = "Hello"; String modified = original.concat(", World!"); System.out.println(original); // Output: Hello System.out.println(modified); // Output: Hello, World!
-
length(): Returns the length of the string.String text = "Java"; System.out.println(text.length()); // Output: 4
-
charAt(int index): Returns the character at the specified index.String text = "Java"; System.out.println(text.charAt(2)); // Output: v
-
substring(int beginIndex, int endIndex): Extracts a portion of the string.String text = "Java Programming"; System.out.println(text.substring(0, 4)); // Output: Java
-
toUpperCase()andtoLowerCase(): Converts the string to uppercase or lowercase.String text = "Java"; System.out.println(text.toUpperCase()); // Output: JAVA System.out.println(text.toLowerCase()); // Output: java
-
trim(): Removes leading and trailing spaces.String text = " Java "; System.out.println(text.trim()); // Output: Java
-
replace(char oldChar, char newChar): Replaces occurrences of a character.String text = "Java"; System.out.println(text.replace('a', 'o')); // Output: Jovo
-
contains(CharSequence sequence): Checks if the string contains a sequence of characters.String text = "Java Programming"; System.out.println(text.contains("Program")); // Output: true
-
split(String regex): Splits the string into an array based on a delimiter.String text = "Java,Python,C++"; String[] languages = text.split(","); for (String lang : languages) { System.out.println(lang); } // Output: // Java // Python // C++
-
equals(String anotherString): Compares two strings for equality.String str1 = "Java"; String str2 = "java"; System.out.println(str1.equals(str2)); // Output: false
-
isEmpty(): Checks if the string is empty.String text = ""; System.out.println(text.isEmpty()); // Output: true
In Java, strings can be created in two ways: using string literals or the new keyword. Both approaches have distinct behaviors.
When a string is created using a literal, it is stored in the String Pool. If a string with the same value already exists in the pool, the reference to the existing string is returned instead of creating a new object.
String str1 = "Java";
String str2 = "Java";
System.out.println(str1 == str2); // Output: true (both refer to the same object in the pool)When a string is created using the new keyword, a new object is always created in the Heap Memory, even if an identical string exists in the String Pool.
String str1 = new String("Java");
String str2 = new String("Java");
System.out.println(str1 == str2); // Output: false (different objects in heap memory)| Feature | String Literals | new Keyword |
|---|---|---|
| Memory Location | Stored in the String Pool. | Stored in the Heap Memory. |
| Object Creation | Reuses existing objects if possible. | Always creates a new object. |
| Performance | Faster due to reuse. | Slower due to new object creation. |
Use string literals when possible to improve performance and reduce memory usage. Use the new keyword only when explicitly required to create a new object.
Packages in Java are used to group related classes and interfaces together. They help organize code and avoid naming conflicts.
To create a package, use the package keyword at the top of your Java file.
package com.example.myapp;
public class MyClass {
public void displayMessage() {
System.out.println("Hello from MyClass!");
}
}To use a class from another package, you need to import it using the import keyword.
import com.example.myapp.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayMessage();
}
}-
Single Class Import: Imports a specific class.
import java.util.ArrayList;
-
Wildcard Import: Imports all classes from a package.
import java.util.*;
If no package is specified, the class belongs to the default package. However, it is not recommended for large projects.
- Code organization
- Avoids class name conflicts
- Easier to locate and maintain code
- Provides access control
java.lang: Contains fundamental classes likeString,Math,Object, etc. (Imported by default).java.util: Includes utility classes likeArrayList,HashMap,Date, etc.java.io: Provides classes for input and output operations.java.net: Contains classes for networking operations.java.sql: Used for database connectivity.
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println(list);
}
}Java has a set of reserved keywords that have predefined meanings and cannot be used as identifiers (e.g., variable names, method names, class names).
| Category | Keywords |
|---|---|
| Access Control | private, protected, public |
| Class/Interface | class, interface, enum, extends, implements |
| Control Flow | if, else, switch, case, default, for, while, do, break, continue, return |
| Exception Handling | try, catch, finally, throw, throws |
| Modifiers | abstract, final, static, transient, volatile, synchronized |
| Primitive Types | byte, short, int, long, float, double, char, boolean |
| Others | void, null, true, false, this, super, new, import, package, instanceof, assert, native, strictfp, goto, const |
public class Example {
private int number = 10; // 'private' is a reserved keyword
public static void main(String[] args) {
if (true) { // 'if' is a reserved keyword
System.out.println("Hello, Java!");
}
}
}- Reserved keywords are case-sensitive (
publicis valid, butPublicis not a keyword). - Avoid using these keywords for naming variables, methods, or classes to prevent compilation errors.
- Some keywords like
gotoandconstare reserved but not used in Java.
For a complete list of keywords, refer to the official Java documentation.
Operators in Java are special symbols or keywords used to perform operations on variables and values.
-
Arithmetic Operators Perform basic mathematical operations.
Operator Description Example +Addition int sum = a + b;-Subtraction int diff = a - b;*Multiplication int prod = a * b;/Division int div = a / b;%Modulus (remainder) int rem = a % b;int a = 10, b = 3; System.out.println(a + b); // Output: 13 System.out.println(a % b); // Output: 1
-
Relational (Comparison) Operators Compare two values and return a boolean result.
Operator Description Example ==Equal to a == b!=Not equal to a != b>Greater than a > b<Less than a < b>=Greater than or equal to a >= b<=Less than or equal to a <= bint a = 5, b = 10; System.out.println(a > b); // Output: false System.out.println(a <= b); // Output: true
-
Logical Operators Used to combine multiple boolean expressions.
Operator Description Example &&Logical AND a > 5 && b < 10` ` !Logical NOT !(a > 5)boolean a = true, b = false; System.out.println(a && b); // Output: false System.out.println(a || b); // Output: true
-
Assignment Operators Assign values to variables.
Operator Description Example =Assign a = 10;+=Add and assign a += 5;-=Subtract and assign a -= 5;*=Multiply and assign a *= 5;/=Divide and assign a /= 5;%=Modulus and assign a %= 5;int a = 10; a += 5; // a = a + 5 System.out.println(a); // Output: 15
-
Unary Operators Operate on a single operand.
Operator Description Example +Unary plus +a-Unary minus -a++Increment a++or++a--Decrement a--or--a!Logical NOT !aint a = 5; System.out.println(++a); // Output: 6 System.out.println(a--); // Output: 4
-
Bitwise Operators Perform operations on bits.
Operator Description Example &Bitwise AND a & b` ` Bitwise OR ^Bitwise XOR a ^ b~Bitwise Complement ~a<<Left shift a << 2>>Right shift a >> 2Bitwise operators operate on binary representations of integers. Below are the truth tables for common bitwise operators:
The result is
1if both bits are1, otherwise0.A B A & B 0 0 0 0 1 0 1 0 0 1 1 1 The result is
1if at least one bit is1.A B A & B 0 0 0 0 1 1 1 0 1 1 1 1 The result is
1if the bits are different, otherwise0.A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 The result is the complement of the bit (flips
0to1and1to0).A ~A 0 1 1 0 Shifts bits to the left, filling with
0on the right.A (Binary) A << 1 (Binary) A << 2 (Binary) 0001 0010 0100 0010 0100 1000 Shifts bits to the right, filling with the sign bit (for signed numbers).
A (Binary) A >> 1 (Binary) A >> 2 (Binary) 0100 0010 0001 1000 1100 (signed) 1110 (signed) These truth tables help visualize how bitwise operations work at the binary level.
-
ifStatement Executes a block of code if the condition is true.if (condition) { // Code to execute }
int age = 18; if (age >= 18) { System.out.println("You are an adult."); }
-
if-elseStatement Executes one block of code if the condition is true, otherwise executes another block.if (condition) { // Code if true } else { // Code if false }
int age = 16; if (age >= 18) { System.out.println("You are an adult."); } else { System.out.println("You are a minor."); }
-
else ifLadder Tests multiple conditions sequentially.if (condition1) { // Code if condition1 is true } else if (condition2) { // Code if condition2 is true } else { // Code if none of the conditions are true }
int marks = 85; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else { System.out.println("Grade: C"); }
-
switchStatement Tests a variable against multiple cases.switch (variable) { case value1: // Code for value1 break; case value2: // Code for value2 break; default: // Code if no case matches }
int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day"); }
The Math class in Java provides methods for performing mathematical operations.
-
Math.abs()Returns the absolute value of a number.System.out.println(Math.abs(-10)); // Output: 10
-
Math.max()andMath.min()Returns the maximum or minimum of two numbers.System.out.println(Math.max(10, 20)); // Output: 20 System.out.println(Math.min(10, 20)); // Output: 10
-
Math.pow()Returns the value of the first argument raised to the power of the second argument.System.out.println(Math.pow(2, 3)); // Output: 8.0
-
Math.sqrt()Returns the square root of a number.System.out.println(Math.sqrt(16)); // Output: 4.0
-
Math.random()Returns a random number between 0.0 and 1.0.System.out.println(Math.random());
-
Math.round()Rounds a floating-point number to the nearest integer.System.out.println(Math.round(4.6)); // Output: 5
-
Math.ceil()andMath.floor()ceil(): Rounds up to the nearest integer.floor(): Rounds down to the nearest integer.
System.out.println(Math.ceil(4.2)); // Output: 5.0 System.out.println(Math.floor(4.8)); // Output: 4.0
-
Math.log()Returns the natural logarithm of a number.System.out.println(Math.log(10)); // Output: 2.302585...
-
Math.sin(),Math.cos(),Math.tan()Returns the trigonometric sine, cosine, or tangent of an angle (in radians).System.out.println(Math.sin(Math.PI / 2)); // Output: 1.0
-
Math.toRadians()andMath.toDegrees()Converts angles between degrees and radians.System.out.println(Math.toRadians(180)); // Output: 3.14159... System.out.println(Math.toDegrees(Math.PI)); // Output: 180.0
These operators, conditional statements, and the Math class are essential tools for solving problems in Java programming.
An array is a collection of elements of the same type, stored in a contiguous block of memory. Arrays are used to store multiple values in a single variable.
-
Declaration: Specify the type of elements and use square brackets
[].int[] numbers;
-
Initialization: Allocate memory and assign values.
numbers = new int[5]; // Array of size 5
-
Combined Declaration and Initialization:
int[] numbers = {10, 20, 30, 40, 50};
Array elements are accessed using indexes. Indexes in Java start from 0.
int[] numbers = {10, 20, 30, 40, 50};
// Accessing elements
System.out.println(numbers[0]); // Output: 10
System.out.println(numbers[2]); // Output: 30
System.out.println(numbers[4]); // Output: 50You can update the value of an element by assigning a new value to its index.
int[] numbers = {10, 20, 30, 40, 50};
// Modifying elements
numbers[1] = 25; // Update the second element
System.out.println(numbers[1]); // Output: 25The length property gives the total number of elements in the array.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length); // Output: 5-
Accessing Out-of-Bounds Index:
int[] numbers = {10, 20, 30}; System.out.println(numbers[3]); // Error: Index 3 is out of bounds
-
Using Negative Index:
int[] numbers = {10, 20, 30}; System.out.println(numbers[-1]); // Error: Negative index is not allowed
String[] names = {"Alice", "Bob", "Charlie"};
// Accessing names
System.out.println(names[0]); // Output: Alice
System.out.println(names[2]); // Output: Charlie
// Modifying a name
names[1] = "David";
System.out.println(names[1]); // Output: David- Arrays are fixed in size once created.
- Indexes start from
0and go up tolength - 1. - Use the
lengthproperty to avoid accessing out-of-bounds indexes.
Arrays are a fundamental concept in Java and are widely used for storing and managing collections of data.
The for loop is one of the most commonly used control structures in Java. It is used to execute a block of code repeatedly for a fixed number of iterations.
for (initialization; condition; update) {
// Code to be executed
}- Initialization: This is executed once at the beginning of the loop. It is used to initialize the loop control variable.
- Condition: This is evaluated before each iteration. If the condition is
true, the loop body is executed. Iffalse, the loop terminates. - Update: This is executed after each iteration. It is typically used to modify the loop control variable.
public class NaturalForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}1
2
3
4
5
6
7
8
9
10
public class SumOfNaturalNumbers {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i; // Add the current number to the sum
}
System.out.println("The sum of the first 10 natural numbers is: " + sum);
}
}The sum of the first 10 natural numbers is: 55
public class ArrayIteration {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
A for loop can be nested inside another for loop to handle multi-dimensional data or perform complex iterations.
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i * j + "\t");
}
System.out.println();
}
}
}1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
If the condition in a for loop is omitted or always evaluates to true, the loop will run indefinitely.
public class InfiniteLoop {
public static void main(String[] args) {
for (;;) { // No initialization, condition, or update
System.out.println("This is an infinite loop.");
}
}
}Note: Use infinite loops cautiously and ensure there is a way to break out of them.
Java also provides an enhanced for loop, which is specifically designed for iterating over arrays or collections.
public class ForEachExample {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}Apple
Banana
Cherry
- The
forloop is ideal when the number of iterations is known beforehand. - Avoid modifying the loop control variable inside the loop body to prevent unexpected behavior.
- Use the enhanced
forloop for simpler and more readable code when working with arrays or collections.
The for loop is a powerful tool in Java that allows developers to write concise and efficient code for repetitive tasks.
The while loop is used to execute a block of code repeatedly as long as the given condition is true.
while (condition) {
// Code to execute
}public class WhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}1
2
3
4
5
The do-while loop is similar to the while loop, but it guarantees that the code block will execute at least once, even if the condition is false.
do {
// Code to execute
} while (condition);public class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}1
2
3
4
5
| Feature | while Loop |
do-while Loop |
|---|---|---|
| Execution | Executes only if the condition is true. |
Executes at least once, even if the condition is false. |
| Use Case | Use when the condition needs to be checked before execution. | Use when the code must execute at least once. |
Java provides the Scanner class to take input from the user. It is part of the java.util package.
To use the Scanner class, you need to import it:
import java.util.Scanner;import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
}
}Enter a number: 42
You entered: 42
import java.util.Scanner;
public class StringInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
}
}Enter your name: Alice
Hello, Alice!
import java.util.Scanner;
public class MultipleInputsExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
System.out.println("Enter words (type 'exit' to stop):");
while (true) {
input = scanner.nextLine();
if (input.equalsIgnoreCase("exit")) {
break;
}
System.out.println("You entered: " + input);
}
}
}Enter words (type 'exit' to stop):
Hello
You entered: Hello
Java
You entered: Java
exit
import java.util.Scanner;
public class DoWhileInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a positive number: ");
number = scanner.nextInt();
} while (number <= 0);
System.out.println("You entered: " + number);
}
}Enter a positive number: -5
Enter a positive number: 0
Enter a positive number: 10
You entered: 10
- Use the
whileloop when the condition needs to be checked before execution. - Use the
do-whileloop when the code must execute at least once. - Use the
Scannerclass to take user input for various data types likeint,double,String, etc. - Always close the
Scannerobject usingscanner.close()to release resources.
These loops and input methods are essential for creating interactive Java programs.