Java Basics
(Write Once, Run Anywhere)
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
1. Sample Code
Let's look at the most basic Java program: Hello World.
public class Main { public static void main(String[] args) { System.out.println("Hello World"); }}Understanding the Parts:
- class Main: Everything in Java happens inside a class. We define a class named "Main". Ideally, the file
name should also be
Main.java. - public static void main(String[] args): This is the entry point.
- public: Access modifier, means it can be accessed from anywhere.
- static: It can be run without creating an object of the class.
- void: It does not return any value.
- main: The name of the method.
- String[] args: Command line arguments. We can pass inputs to the program when running it from the command line.
- System.out.println: The command to print output to the screen.
printlnmeans "print line", so it moves to a new line after printing.
2. Comments
Comments are ignored by the computer. They are for humans to read.
// This is a single line comment/* This is a multi-line comment */3. Data Types
Java has 8 primitive data types to store different values.
- byte: 1 byte, small integers (-128 to 127)
- short: 2 bytes, integers
- int: 4 bytes, integers (Most common)
- long: 8 bytes, large integers
- float: 4 bytes, decimals (needs 'f' suffix, e.g., 3.14f)
- double: 8 bytes, decimals (Most common for fractions)
- char: 2 bytes, single character (e.g., 'A')
- boolean: 1 bit, true or false
4. Operators
Arithmetic Operators
+(Addition): Adds two values.-(Subtraction): Subtracts the right operand from the left.*(Multiplication): Multiplies two values./(Division): Divides the left operand by the right.%(Modulo): Returns the remainder of a division operation.
Unary Operators
Operators that require only one operand.
++(Increment): Increases a value by 1.--(Decrement): Decreases a value by 1.!(Logical NOT): Inverts the boolean value.
Relational Operators
Used to compare two values. They return a boolean result (true or false).
==(Equal to): Checks if two values are equal.!=(Not equal to): Checks if two values are not equal.>(Greater than): Checks if the left value is greater than the right.<(Less than): Checks if the left value is less than the right.>=(Greater than or equal to): Checks if the left value is greater than or equal to the right.<=(Less than or equal to): Checks if the left value is less than or equal to the right.
Logical Operators
Used to determine the logic between variables or values.
&&(Logical AND): Returns true if both statements are true.||(Logical OR): Returns true if at least one of the statements is true.
Assignment Operators
Used to assign values to variables.
=(Assignment): Assigns the value on the right to the variable on the left.+=(Add and Assign): Adds a value to the variable and assigns the result.-=(Subtract and Assign): Subtracts a value from the variable and assigns the result.*=(Multiply and Assign): Multiplies the variable by a value and assigns the result./=(Divide and Assign): Divides the variable by a value and assigns the result.%=(Modulo and Assign): Assigns the remainder of the division to the variable.
5. Strings
Strings are objects in Java, not primitives. They store text.
- Immutable: Once created, a String object cannot be changed. Modifying it creates a new object.
String s1 = "Hello";char[] arr = {'W', 'o', 'r', 'l', 'd'};String s2 = new String(arr); // char array to stringSystem.out.println(s1 + " " + s2); // Concatenate: Hello WorldSystem.out.println(s1.charAt(1)); // Char at index 1: 'e'System.out.println(s1.length()); // Length: 5System.out.println(s1.substring(0, 2)); // Substring: "He"System.out.println(s1.equals("Hello")); // Check content equality: true6. Input Output
For input, we use the Scanner class.
import java.util.Scanner;public class InputExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int age = sc.nextInt(); String name = sc.next(); System.out.println(name + " is " + age); sc.close(); }}What about BufferedReader?
BufferedReader is another way to read input. It is faster but harder to use (requires parsing strings to numbers manually). Scanner is easier and preferred for beginners.
7. Type Casting
Converting one data type to another.
- Implicit (Widening): Small type to large type (e.g.,
inttodouble). This happens automatically by the compiler. - Explicit (Narrowing): Large type to small type (e.g.,
doubletoint). This must be done manually by the programmer.
int myInt = 9;double myDouble = myInt; // Automatic casting: 9.0int heavyInt = (int) 9.78; // Manual casting: 9 (fraction lost)8. Constants
Use the final keyword to create constants. These values cannot be changed.
final float PI = 3.14f;// PI = 3.15f; // This will cause an error9. Arrays
Storing multiple values of the same type.
int[] scores = {90, 80, 70};System.out.println(scores.length); // 3System.out.println(scores[0]); // 90// For-Each Loopfor (int i : scores) { System.out.println(i);}// 2D Arrayint[][] matrix = { {1, 2}, {3, 4} };10. Conditional Statements
If, Else If, Else
int marks = 85;if (marks > 90) { System.out.println("A");} else if (marks > 80) { System.out.println("B");} else { System.out.println("C");}Explanation: The program checks the condition marks > 90. Since 85 is not greater than 90, it
moves to the next condition marks > 80. This is true, so it prints "B". The rest of the chain is
skipped.
Switch
int day = 2;switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; default: System.out.println("Invalid");}Explanation: The computer checks the value of day. It matches with case 2 and
executes the code inside it.
- break: This keyword stops the code from running into the next case automatically (no "fall-through").
- default: This runs if no case matches the value (like an
else).
11. Loops
For Loop
for (int i = 0; i < 5; i++) { System.out.println(i);}While Loop
int i = 0;while (i < 5) { System.out.println(i); i++;}Do While Loop
int i = 0;do { System.out.println(i); // Runs at least once i++;} while (i < 5);12. Exception Handling
Handling errors so the program doesn’t crash.
try { int[] myNumbers = {1, 2, 3}; System.out.println(myNumbers[10]); // Error} catch (Exception e) { System.out.println("Something went wrong.");} finally { System.out.println("The 'try catch' is finished.");}Summary
We covered the fundamental building blocks of Java:
- Structure: Class based, main method
- Data: Types, Variables, Arrays, Strings
- Logic: Operators, If-Else, Switch
- Control: Loops (for, while)
- Safety: Exception Handling
Practice writing these snippets to get comfortable with the syntax!