Java is known for being verbose โ but also clear and strict. Its syntax borrows from C/C++, but adds safety, structure, and readability. To code effectively in Java, you must understand how files, classes, methods, statements, and variables come together to form a complete program.
This is the smallest valid Java program you can write:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Hello.java)main() is where the program starts execution{ } group code into blocks;// is a single-line comment, /* */ for multi-lineMyClass)main)Main โ mainJava is statically typed, so every variable must have a type. Examples:
int age = 25;
double pi = 3.14;
char grade = 'A';
boolean isJavaFun = true;
String name = "Aelify";
int โ Whole numbers (e.g., 42)double โ Decimal numbers (e.g., 3.14)char โ Single characters (e.g., 'A')boolean โ true / falseString โ Text (actually a class, not a primitive)// This is a single-line comment
/*
This is a
multi-line comment
*/
int x, prefer int itemCountHello.javajavac Hello.java โ creates Hello.classjava Hello (no .class or .java)Example output:
Hello, Java!
public class PersonalInfo {
public static void main(String[] args) {
String name = "Aelify";
int age = 21;
boolean learningJava = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Learning Java? " + learningJava);
}
}
๐ก Try modifying the values and adding a new variable for height!
Java is strict, but predictable. Once you understand its structure โ classes, main method, semicolons, and variable types โ everything starts to click. You've now written your first real Java code and understood how it flows from source file to output.
Every statement ends with a semicolon ;.
Java is case-sensitive: System โ system
{ } define blocksif (true) {
System.out.println("Inside block");
}
| Type | Description | Example |
|---|---|---|
int |
Whole numbers | int age = 25; |
double |
Decimal numbers | double pi = 3.14; |
char |
Single character | char grade = 'A'; |
boolean |
True/False | boolean alive = true; |
String |
Text (non-primitive) | String name = "Aelify"; |
int age = 30;
double height = 5.9;
boolean isStudent = true;
String language = "Java";
| Operator | Purpose | Example |
|---|---|---|
+ - * / % |
Arithmetic | a + b, x % y |
== != > < >= <= |
Comparison | a == b |
&& || ! |
Logical | a > 0 && b < 5 |
= |
Assignment | x = 5 |
int a = 10, b = 5;
System.out.println("Sum: " + (a + b));
System.out.println("Is A greater? " + (a > b));
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good Job");
break;
default:
System.out.println("Keep Trying");
}
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
int i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}
int j = 1;
do {
System.out.println("j = " + j);
j++;
} while (j <= 5);
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
}
}
import java.util.Scanner;
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Sum = " + sum);
}
}
public class TableOf7 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("7 x " + i + " = " + (7 * i));
}
}
}
import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num % 2 == 0)
System.out.println(num + " is Even");
else
System.out.println(num + " is Odd");
}
}
import java.util.Scanner;
public class AgeClassifier {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if (age < 13)
System.out.println("Child");
else if (age < 20)
System.out.println("Teen");
else if (age < 60)
System.out.println("Adult");
else
System.out.println("Senior");
}
}
for loop.import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
}
}
import java.util.Scanner;
public class SplitSentence {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
for (String word : words) {
System.out.println(word);
}
}
}
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0)
System.out.println("FizzBuzz");
else if (i % 3 == 0)
System.out.println("Fizz");
else if (i % 5 == 0)
System.out.println("Buzz");
else
System.out.println(i);
}
}
}
You've covered the most essential building blocks of Java:
;) โ Every Java statement must end with a semicolon.
System โ
system, Main โ main.
=) instead of comparison (==) in conditions.
{ } โ leads to "illegal start of type" or
logical errors.class or main method โ program won't run without:
public static void main(String[] args)
HelloWorld.java for public class HelloWorld).
String to int or
double to int without casting.
char declaration โ use single quotes: char c = 'a';
(not "a").java.util.Scanner before using it.nextLine() after nextInt() or nextDouble() without
consuming the newline.i++ in
while/for).
i >= 0 with i++, causing endless
loop.= with == in if conditions.else if without a preceding if.break; โ leads to fall-through bugs."Sum: " + a + b gives string
like "Sum: 105" instead of 15.javac FileName.java before java FileName.
If you use nextInt() followed by nextLine(), always clear the newline buffer:
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
sc.nextLine(); // โ ๏ธ This clears the \n left behind by nextInt()
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hi " + name + ", you are " + age + " years old.");
}
}
You're now ready to build interactive, logical Java programs. Up next, we'll explore variables in detail โ primitive vs reference, memory, type casting, and more!
In our next post, we'll dive into ๐งฎ [4] Data Types & Variables โ The Core Foundation of Java!
โ Blog by Aelify (ML2AI.com)
๐ Documentation Index