Topic01 / 75
Introduction to Java
What & Why
Java is a statically-typed, object-oriented language famous for 'Write Once, Run Anywhere' via the JVM. It powers enterprise applications, Android apps, and large-scale backends — and is the most common language for placement interviews at top companies.
Syntax
java
1// HelloWorld.java
2public class HelloWorld {
3 public static void main(String[] args) {
4 System.out.println("Hello, World!");
5
6 // Variables
7 String name = "Priya";
8 int age = 22;
9 double price = 9.99;
10 boolean isActive = true;
11
12 System.out.printf("Name: %s, Age: %d%n", name, age);
13 }
14}Real-World Example
A simple sales calculator program:
example.ts
java
1public class SalesCalculator {
2 public static void main(String[] args) {
3 double[] dailySales = {12500.0, 23400.0, 18900.0, 31200.0, 27800.0};
4 double total = 0;
5
6 for (double sale : dailySales) {
7 total += sale;
8 }
9
10 double average = total / dailySales.length;
11 double tax = total * 0.18; // 18% GST
12
13 System.out.printf("Total Sales : ₹%.2f%n", total);
14 System.out.printf("Average/Day : ₹%.2f%n", average);
15 System.out.printf("GST (18%%) : ₹%.2f%n", tax);
16 System.out.printf("Net Revenue : ₹%.2f%n", total - tax);
17 }
18}Pitfalls & Interview Tips
- ⚠️Java is case-sensitive: String and string are different. Only String (capital S) is the class.
- ⚠️The filename MUST match the public class name — HelloWorld.java must contain public class HelloWorld.
- 💡Interview tip: Java is pass-by-value for primitives and pass-by-value-of-reference for objects. Not true pass-by-reference.