



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




What Java is
Syntax and variables
int score = 85; boolean passed = true; String studentName = "Maya"; Primitive and reference types
Methods and parameters
static String getGrade(int marks) { return marks >= 60? "Pass" : "Needs improvement"; } public static void main(String[] args) { System.out.println(getGrade(59)); }
Control Flow and Arrays Four loop forms
int[] scores = {10, 20, 30, 40}; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); // Use the index. } for (int score : scores) { System.out.println(score); // Read each value. } int i = 0; while (i < scores.length) { // Check before each iteration. System.out.println(scores[i++]); } int attempts = 0; do { // Run the body at least once. attempts++; } while (attempts < 3); The switch statement
int priority = 2; switch (priority) { case 1: System.out.println("High"); break; case 2: System.out.println("Normal"); break; default: System.out.println("Unknown"); } Array indexing and assignment
Inheritance and Abstraction Inheritance
Abstraction
abstract class Shape { abstract double area(); } class Rectangle extends Shape { private final double width, height; Rectangle(double width, double height) { this.width = width; this.height = height; } @Override double area() { return width * height; } } Interfaces
interface Printable { void print(); } class Report implements Printable { @Override public void print() { System.out.println("Report ready"); } }
Object Behavior and Exception Handling Encapsulation and accessors
class Settings { private int volume; int getVolume() { return volume; } void setVolume(int volume) { if (volume < 0 || volume > 100) { throw new IllegalArgumentException("Volume must be 0 to 100"); } this.volume = volume; } }
Polymorphism
Shape shape = new Rectangle(3, 4); System.out.println(shape.area()); // Prints 12. Handling exceptions
String input = "abc"; try { int quantity = Integer.parseInt(input); System.out.println("Quantity: " + quantity); } catch (NumberFormatException e) { System.out.println("Enter a whole number."); } References Dev.java language tutorials · https://dev.java/learn/language/ Oracle Java Language Specification · https://docs.oracle.com/javase/specs/