Java Tutorial

Java Output

System.out.println sends text to the console. print stays on the same line. This is how every first program talks back.

println writes a line

System.out.println prints its argument and then starts a new line. Strings use double quotes. Numbers print without quotes.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, Java");
    System.out.println(2026);
  }
}

print stays on the line

print does not add a newline. Use it when several pieces should sit on one line, then finish with println.

Example

public class Main {
  public static void main(String[] args) {
    System.out.print("Ada");
    System.out.print(" ");
    System.out.println(2026);
  }
}

Concatenation

The + operator glues a string to a number or another string. Parentheses around an expression 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));
  }
}

Run this in /java/try. Change 7 and compile again.

printf for decimals

System.out.printf formats numbers. %.2f means two digits after the point. %n is a newline.

Example

public class Main {
  public static void main(String[] args) {
    System.out.printf("pi is %.2f%n", 3.14159);
  }
}

println and print are different: print leaves the cursor on the same line. Forgetting the final println is why several pieces sometimes end up jammed together with no line break.

Try It Yourself

Exercise: Print exactly this line, computing the total yourself with concatenation: 3 coffees cost 10.5 (3 at 3.5 each).

Show solution
int cups = 3;
double each = 3.5;
System.out.println(cups + " coffees cost " + (cups * each));

The parentheses around cups * each matter: without them, Java joins left to right and you get text, not the product.

Key Takeaways

  • println prints and moves to a new line; print stays on the line.
  • + joins strings and numbers — parenthesize math so it runs before the join.
  • printf formats numbers: %.2f for two decimals, %n for a newline.

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

Print a voltmeter reading

A digital meter shows a number and a unit. println concatenates both so the line stays readable.

Example

public class Main {
  public static void main(String[] args) {
    System.out.println("V = " + 4.92 + " V");
  }
}

FAQ: Java Output

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 println 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 println in this Java Java lesson (Java Output).

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.