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 Main names 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

  1. You save a .java file.
  2. javac checks types and builds .class bytecode.
  3. java runs 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

PythonC++Java
Dashboard/tutorial/cpp/java
Editor/try — prints and plots/cpp/try — g++/java/try — javac
ResultOutput text and chartsStdout and compiler messagesStdout 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 main method.
  • System.out.println writes 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");
  }
}

FAQ: Java Introduction

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java introduction examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java introduction in this Java Java lesson (Java Introduction).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.