Java Tutorial
Java Introduction
Java is a compiled language: you write source, javac builds bytecode, and the JVM runs it on any machine with a Java runtime.
What is Java?
Java is a general-purpose language used for Android, servers, finance, and tools that need to run on more than one operating system. You write a .java file. javac compiles it to bytecode. The Java Virtual Machine runs that bytecode.
That compile step is the main difference from Python on StudyGrid. Python reads a file and executes it. Java must build first. If the types do not match, you get an error instead of a running program — that is a feature, not a hassle.
A first program
This is a complete Java program. Copy it into the Java editor and change the message.
Example
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}Click Try it in Java under the example. That opens /java/try — a compile-and-run editor.
Numbers and text in one print
System.out.println can print integers, strings, and results of expressions. The+ operator joins pieces into one line. Parentheses around the sum force the math to run before the join.
Example
public class Main {
public static void main(String[] args) {
int a = 7;
int b = 5;
System.out.println(a + " + " + b + " = " + (a + b));
}
}A third example: several lines
Each println ends the line. Use several calls when you want a short report instead of one long sentence.
Example
public class Main {
public static void main(String[] args) {
System.out.println("StudyGrid Java");
System.out.println("Compile, then run");
System.out.println(2026);
}
}What each line does
public class Mainnames the class. The file should match:Main.java.public static void main(String[] args)is where the program starts.System.out.println(...)writes a line to the console.
Compiled, not interpreted
- You save a
.javafile. javacchecks types and builds.classbytecode.javaruns that class. It prints output or reads input.
On StudyGrid the Java editor does steps 2 and 3 for you. Locally you would type javac Main.java and then java Main.
Java vs Python vs C++ on StudyGrid
| Python | C++ | Java | |
|---|---|---|---|
| Dashboard | /tutorial | /cpp | /java |
| Editor | /try — prints and plots | /cpp/try — g++ | /java/try — javac |
| Result | Output text and charts | Stdout and compiler messages | Stdout and compiler messages |
| File | .py | .cpp | .java |
Try It Yourself
Exercise: Print your name on one line and the year 2026 on the next. Then change the year and compile again.
Show solution
public class Main {
public static void main(String[] args) {
System.out.println("Ada");
System.out.println(2026);
}
}Two println calls make two lines. Numbers do not need quotes; text does.
Key Takeaways
- Java source is compiled to bytecode, then run by the JVM.
- Every program needs a class and a
mainmethod. System.out.printlnwrites a line to the console.- Type mismatches fail at compile time — before the program runs.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Current from an ammeter
Current is charge per second. The SI unit is the ampere: one coulomb of charge passing a point in one second. A first program does not have to compute that. It only has to print a reading a person already took.
Lab notebooks start the same way. Write the quantity, the number, and the unit on one line. If the unit is missing, the 0.45 is useless.
I = Q / t
Example
public class Main {
public static void main(String[] args) {
System.out.println("Current I = 0.45 A");
}
}Maths
A 3-4-5 triangle
A right triangle whose legs are 3 and 4 has hypotenuse 5. That is Pythagoras, not a coincidence: 9 + 16 = 25. Printers and carpenters still use the triple to square a corner.
Printing the three numbers is a Hello World with a reason to exist. Later chapters compute the 5 from the 3 and the 4. Here you only name them.
a² + b² = c²
Example
public class Main {
public static void main(String[] args) {
System.out.println("Right triangle sides: 3, 4, 5");
}
}