Java is a general-purpose, object-oriented language used for Android apps, banking systems, large websites and university courses around the world. Java code is compiled once into bytecode that runs on any computer with a Java Virtual Machine, which is where the slogan “write once, run anywhere” comes from.
In this lesson you will learn
- What Java is used for and how it runs
- How to install the JDK and run your first program
- The parts of a Java program: class,
mainand statements - Variables, types, methods and loops in a small example
- How Java compares with C and Python
How Java Runs Your Code
- You write source code in a file ending in
.java. - The compiler,
javac, turns it into bytecode in a.classfile. - The Java Virtual Machine (JVM), started with the
javacommand, runs that bytecode on Windows, macOS or Linux alike.
To start, install a JDK (Java Development Kit), such as the free Eclipse Temurin or Oracle OpenJDK builds, choosing a long-term-support version. Check it with java -version and javac -version.
Your First Java Program
Save this as Main.java. In Java the file name must match the public class name.
public class Main {
public static void main(String[] args) {
String name = "Core Tech ED";
int lessons = 3;
double hours = 1.5;
System.out.println("Welcome to Java on " + name);
System.out.println("Lessons today: " + lessons + ", hours: " + hours);
}
}
javac Main.java
java Main
Output
Welcome to Java on Core Tech ED
Lessons today: 3, hours: 1.5
- All code lives inside a class, here
Main. - The program starts at
public static void main(String[] args), just as a C program starts atmain(). - Every variable has a declared type:
String,int,double. - Statements end with a semicolon, and blocks use curly braces.
+joins text with other values when building output.
Methods, Arrays and Loops
This program estimates rickshaw fares for several trips using a method, an array and a for-each loop.
public class Main {
static int fare(int km) {
if (km <= 0) return 0;
return 30 + (km - 1) * 15; // 30 taka for the first km, 15 for each extra km
}
public static void main(String[] args) {
int[] trips = {1, 4, 7};
int total = 0;
for (int km : trips) {
int f = fare(km);
System.out.printf("%d km -> %d taka%n", km, f);
total += f;
}
System.out.println("Total: " + total + " taka");
System.out.println(7 / 2 + " " + 7 / 2.0);
}
}
Output
1 km -> 30 taka
4 km -> 75 taka
7 km -> 120 taka
Total: 225 taka
3 3.5
static int fare(int km) is a method: it takes an int and returns an int. for (int km : trips) visits each item in the array. printf works like C’s, with %n for a new line. The last line shows a trap Java shares with C: 7 / 2 divides two integers and gives 3, while 7 / 2.0 gives 3.5.
Java, C and Python Compared
| Point | C | Java | Python |
|---|---|---|---|
| Runs as | Native machine code | Bytecode on the JVM | Bytecode on the Python interpreter |
| Types | Static | Static | Dynamic |
| Memory | Manual (malloc/free) | Garbage collected | Garbage collected |
| Style | Procedural | Object-oriented (everything in classes) | Multi-paradigm |
| Common uses | Systems, embedded | Android, enterprise back ends | Data, AI, scripting |
If you know C, Java’s syntax will feel familiar; see the C introduction. If you know Python, expect to declare types and use semicolons and braces.
Common Beginner Mistakes
- File name does not match the class.
public class Mainmust be inMain.java. - Running
java Main.class. Use the class name only:java Main. - Comparing strings with
==. That checks if they are the same object; usea.equals(b). - Missing semicolons or braces. The compiler error usually points to the line just after the real mistake.
- Integer division. Make one operand a
doubleto keep decimals.
Practice Exercises
Exercise 1. Write a program that prints your name and age on two lines.
Show answer
public class Main {
public static void main(String[] args) {
System.out.println("Nadia");
System.out.println(19);
}
}
Exercise 2. What does System.out.println(10 / 4); print?
Show answer
2. Both numbers are int, so the decimals are dropped.
Exercise 3. In the fare program, what would fare(10) return?
Show answer
30 + 9 × 15 = 165.
Frequently Asked Questions
Is Java the same as JavaScript?
No. They are different languages with different purposes. JavaScript mainly runs in web browsers; Java runs on the JVM. Only the names are similar.
Is Java still worth learning?
Yes. It remains one of the most used languages for Android, banking and large back-end systems, and many universities teach object-oriented programming with it.
Which editor should I use for Java?
IntelliJ IDEA Community Edition, Eclipse or VS Code with the Java extensions are all free and popular.
Summary
- Java compiles to bytecode that runs on the JVM on any operating system.
- Every program has a class and starts at
main. - Variables need declared types; statements end with semicolons.
- Compile with
javac, run withjava.
