Clean • Professional
A Java program must contain a class and a main() method (entry point).
Example:
class Demo {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Because Java is object-oriented, and everything in Java belongs to a class.
public static void main(String[] args)
It stores command-line arguments.
A variable is a name given to a memory location where data is stored.
static keywordExample:
class Test {
int age = 25; // instance variable
static String name = "Java"; // static variable
void show() {
int x = 10; // local variable
System.out.println(x);
}
}
Because Java does not automatically initialize local variables.
Two categories:
Primitive (8 types)
Non-Primitive
Converting one data type to another.
1. Implicit / Widening (smaller → larger)
int a = 10;
double b = a; // auto conversion
2. Explicit / Narrowing (larger → smaller)
double x = 10.5;
int y = (int) x; // explicit cast
In expressions, smaller types automatically convert to larger types.
Example:
byte a = 10;
byte b = 20;
int c = a + b; // byte promoted to int
Perform mathematical calculations like addition, subtraction, multiplication, division, modulus, increment, and decrement.
+ - * / % ++ --
Example:
int a = 10, b = 3;
System.out.println(a % b); // 1
Compare two values and return true or false. (== != > < >= <=)
Example:
System.out.println(10 > 5); // true
Combine boolean expressions. && || !
Example:
boolean x = true, y = false;
System.out.println(x && y); // false
Perform operations on individual bits of integers. & | ^ ~ << >> >>>
Example:
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b); // 1
A shorthand if-else operator that returns one of two values based on a condition.
condition ? value_if_true : value_if_falseExample:
int age = 20;
String result = (age >= 18) ? "Adult" : "Minor";
///* *//** */Example:
// Single-line
/* Multi-line */
/** Documentation comment */
To explain logic and improve code readability.
import java.util.Scanner;
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println("Age: " + age);
}
}
import java.io.*;
class InputExample2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println("Name: " + name);
}
}
BufferedReader is faster because it reads data in larger chunks.
Examples:
class, int, static, public, return, if, else, extends, final
Names given to:
Example:
int age; // age is identifier
class Student{} // Student is identifier
_, $myVar, _count, $value1total, class, my-var