package my_proj;
public class my_first {
public static void main(String[] args) {
System.out.println("Welcome to plus2net");
}
}Output:
Welcome to plus2net
Declaring two int variables (v1 = 5 and v2 = 7), storing their sum in res, and combining a label string with the numeric result using the + operator:
package my_proj;
public class my_first {
public static void main(String[] args) {
System.out.println("Welcome to plus2net");
int v1 = 5;
int v2 = 7;
int res = v1 + v2;
System.out.println("Output : " + res);
}
}Terminal Output:
Welcome to plus2net
Output : 12
| Topic / Statement | Key Syntax | What You Will Learn |
|---|---|---|
| Variables & Data Types | int, double, String |
Declaring primitive variables, type casting, and scope rules. |
| User Inputs (Scanner) | new Scanner(System.in) |
Reading strings, integers, and decimals from keyboard input. |
| if-else Statement | if (cond) ... else |
Conditional branching, else-if ladders, and nested if logic. |
| switch Statement | switch (x) { case 1: } |
Multi-way constant dispatch with break, default, and arrow syntax. |
| for Loop | for (int i=0; i<n; i++) |
Counter-controlled loops, step increments, and enhanced for-each. |
| while Loop | while (cond) { ... } |
Pre-test condition loops with break and continue. |
| do-while Loop | do { ... } while (cond); |
Post-test loops guaranteed to execute at least once. |
| Component | Type / Role | Meaning in Java |
|---|---|---|
public static void main(String[] args) |
Entry point method | The JVM starts program execution here without needing a class instance. |
System |
final class in java.lang |
Provides access to standard input (in), output (out), and error (err). |
out |
static PrintStream field |
Standard output stream connected to the terminal console. |
println(...) |
Overloaded method | Converts any primitive, String, or Object to text and appends a newline. |
System.out.print() keeps the cursor on the same line after printing, whereas System.out.println() moves the cursor to the beginning of the next line:
// print() stays on the same line; println() appends a newline at the end:
System.out.print("Java ");
System.out.print("Programming ");
System.out.println("Tutorial");
System.out.println("Next Line Starts Here");Terminal Output:
Java Programming Tutorial
Next Line Starts Here
Java evaluates + from left to right. If the left operand is a String, "Result: " + v1 + v2 converts v1 and v2 into strings and prints "57" instead of 12! Wrap numeric addition in parentheses (v1 + v2):
int v1 = 5, v2 = 7;
// Trap: Without parentheses, "+" concatenates left-to-right as strings ("5" + "7" = "57")!
System.out.println("Without parentheses: " + v1 + v2);
// Fix: Wrap arithmetic in parentheses so (v1 + v2) evaluates to 12 first:
System.out.println("With parentheses: " + (v1 + v2));Terminal Output:
Without parentheses: 57
With parentheses: 12
Instead of chaining many + operators, use System.out.printf() with format specifiers (%s for strings, %d for integers, %.2f for 2-decimal floats, and %n for a platform-safe newline):
String item = "Notebook";
int qty = 3;
double price = 49.5;
// %s = String, %d = integer, %.2f = floating-point rounded to 2 decimal places, %n = newline
System.out.printf("Item: %s | Qty: %d | Unit Price: %.2f%n", item, qty, price);Terminal Output:
Item: Notebook | Qty: 3 | Unit Price: 49.50
Use backslash escape sequences to embed tabs (\t), line breaks (\n), or double quotes (\") inside a string literal:
System.out.println("Col1\tCol2\tCol3");
System.out.println("Line 1\nLine 2");
System.out.println("She said, \"Hello Java!\"");Terminal Output:
Col1 Col2 Col3
Line 1
Line 2
She said, "Hello Java!"
Using width specifiers like %-10s (left-aligned in 10 characters) and %8.2f (right-aligned in 8 characters with 2 decimals) produces clean console tables:
public class FormattedScoreReport {
public static void main(String[] args) {
System.out.printf("%-10s %6s %8s%n", "Student", "Score", "Average");
System.out.printf("%-10s %6d %8.2f%n", "Alex", 92, 91.75);
System.out.printf("%-10s %6d %8.2f%n", "Rama", 85, 84.50);
}
}Terminal Output:
Student Score Average
Alex 92 91.75
Rama 85 84.50
Java is strictly case-sensitive! Writing system.out.println(...) with a lowercase s or string[] args with a lowercase s causes a compile-time error (package system does not exist). Class names like System and String always start with an uppercase letter.
If your class is declared public class my_first, the source file must be saved as my_first.java (exact case match) or javac will refuse to compile it.
public class BasicPrintlnFullDemo {
public static void main(String[] args) {
int v1 = 5, v2 = 7, res = v1 + v2;
if (res == 12) {
System.out.println("Check 1 Passed: 5 + 7 = " + res);
}
String formatted = String.format("Sum=%d", res);
if ("Sum=12".equals(formatted)) {
System.out.println("Check 2 Passed: Format verified (" + formatted + ").");
}
System.out.println("All Java basic & System.out.println() checks passed!");
}
}Terminal Output:
Check 1 Passed: 5 + 7 = 12
Check 2 Passed: Format verified (Sum=12).
All Java basic & System.out.println() checks passed!
Calling System.out.println(); with empty parentheses prints a blank newline (moving the cursor down one line).
System.out writes to the standard output stream (stdout), whereas System.err writes error messages to the standard error stream (stderr, often highlighted in red in IDEs).
Both System and String belong to the java.lang package, which the Java compiler imports automatically into every source file.
println(Object x) automatically calls String.valueOf(x), which invokes x.toString() (or prints "null" if x is null).
Compile with javac my_first.java (or javac -d . my_first.java if using package my_proj;) and run with java my_proj.my_first (or java my_first.java directly in Java 11+).
System.in.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.