Java programs follow a specific structure and syntax rules. Every Java application must contain a main method as the entry point.
public class Main {
public static void main(String[] args) {
// Your code here
System.out.println("Hello Java!");
}
}
Java has two categories of data types: primitive and reference types.
| Data Type | Size | Description |
|---|---|---|
| byte | 1 byte | Stores whole numbers |
| short | 2 bytes | Stores whole numbers |
| int | 4 bytes | Stores whole numbers |
| long | 8 bytes | Stores whole numbers |
| float | 4 bytes | Stores fractional numbers |
| double | 8 bytes | Stores fractional numbers |
| boolean | 1 bit | Stores true/false values |
| char | 2 bytes | Stores a single character |
// Declare variables
int age = 25;
double salary = 55000.75;
char grade = 'A';
boolean isJavaFun = true;
String name = "John Doe";
// Output variables
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: $" + salary);
System.out.println("Grade: " + grade);
System.out.println("Java is fun: " + isJavaFun);
Type casting is when you assign a value of one primitive data type to another type.
Automatically done when passing a smaller size type to a larger size type.
int myInt = 9; double myDouble = myInt; // Automatic casting: int to double System.out.println(myInt); // Output: 9 System.out.println(myDouble); // Output: 9.0
Must be done manually by placing the type in parentheses in front of the value.
double myDouble = 9.78; int myInt = (int) myDouble; // Manual casting: double to int System.out.println(myDouble); // Output: 9.78 System.out.println(myInt); // Output: 9
Java provides various operators for performing operations on variables and values.
int a = 10, b = 4;
System.out.println("a + b = " + (a + b)); // 14
System.out.println("a - b = " + (a - b)); // 6
System.out.println("a * b = " + (a * b)); // 40
System.out.println("a / b = " + (a / b)); // 2
System.out.println("a % b = " + (a % b)); // 2
int x = 5, y = 8;
System.out.println("x == y: " + (x == y)); // false
System.out.println("x != y: " + (x != y)); // true
System.out.println("x > y: " + (x > y)); // false
System.out.println("x < y: " + (x < y)); // true
System.out.println("x >= y: " + (x >= y)); // false
System.out.println("x <= y: " + (x <= y)); // true
boolean p = true, q = false;
System.out.println("p && q: " + (p && q)); // false
System.out.println("p || q: " + (p || q)); // true
System.out.println("!p: " + (!p)); // false
int m = 5; // Binary: 0101
int n = 3; // Binary: 0011
System.out.println("m & n: " + (m & n)); // 1 (0001)
System.out.println("m | n: " + (m | n)); // 7 (0111)
System.out.println("m ^ n: " + (m ^ n)); // 6 (0110)
System.out.println("~m: " + (~m)); // -6 (in two's complement)
System.out.println("m << 1: " + (m << 1)); // 10 (1010)
System.out.println("m >> 1: " + (m >> 1)); // 2 (0010)
Java provides different ways to get input and produce output.
System.out.print("Hello "); // Without newline
System.out.println("World!"); // With newline
System.out.printf("PI: %.2f", Math.PI); // Formatted output
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello " + name + ", you are " + age + " years old.");
scanner.close();
}
}
Comments are used to explain Java code and make it more readable.
// This is a single-line comment int x = 5; // You can also comment at the end of a line
/* This is a multi-line comment
It can span multiple lines */
/**
* This is a Javadoc comment
* Used for documentation generation
* @param args command-line arguments
*/
public static void main(String[] args) {
// Code here
}
Best Practice: Use comments to explain the "why" behind your code, not the "what". Well-written code should be self-explanatory for the "what".
Reserved words that have special meaning in Java. They cannot be used as identifiers.
Names given to classes, variables, methods, etc. They must follow these rules:
// Valid identifiers String firstName; int _count; double $balance; boolean isActive; // Invalid identifiers int 2ndValue; // Starts with digit String class; // Uses keyword double final-score; // Contains hyphen