Java Tutorial
🔍
What is OOP?Classes & ObjectsConstructorsAccess Modifiersthis Keywordstatic KeywordEncapsulationInheritancesuper KeywordMethod OverridingOverloading vs OverridingPolymorphismUpcasting & Downcastinginstanceof OperatorAbstractionAbstract ClassesInterfacesMarker InterfacesAbstract Class vs InterfaceObject ClasstoString() Methodequals() & hashCode()
Collections OverviewCollections HierarchyIterable InterfaceCollection InterfaceMap Interfaceequals() and hashCode()IteratorListIteratorFail-fast vs Fail-safe IteratorConcurrentModificationExceptionArrayListLinkedListHashSetLinkedHashSetTreeSetQueuePriorityQueueDequeArrayDequeHashMapLinkedHashMapTreeMapConcurrentHashMapCopyOnWriteArrayListList vs Set vs MapChoosing the Right CollectionComparableComparatorComparable vs ComparatorCollections Utility ClassArrays Utility ClassImmutable CollectionsCollection vs CollectionsVectorHashtableStackArrayList Internal WorkingLinkedList Internal WorkingHashMap Internal WorkingTreeMap Internal Working
Hello, World in Java
beginner· Basics & I/O · Fundamentals
Problem
Hello World is the traditional first program written in any programming language, used to confirm the basic setup works.
Print "Hello, World!" to the console.
Java Program
Java
public class HelloWorld {
public static void main(String[] args) {
// main() is the entry point the JVM looks for and calls first
System.out.println("Hello, World!");
}
}Output
Hello, World!
Core Logic
It's the classic first program in any language — define the method the JVM looks for on startup, then print a line of text.
How It Works
- 1
public class HelloWorlddeclares a class matching the file name, which Java requires for the public class in a file. - 2
public static void main(String[] args)is the exact method signature the JVM searches for and invokes first. - 3Inside
main,System.out.println(...)writes the given string to standard output. - 4
println(as opposed toprint) appends a newline after the text.
Running the program prints exactly one line:
Hello, World!.💡
Key Point: Every Java program needs this exact main signature — the JVM won't find an entry point without it.
Key Concepts
main methodSystem.out.println()
Approach 2: Using printf()
Java
public class HelloWorldPrintf {
public static void main(String[] args) {
System.out.printf("Hello, World!%n"); // %n adds the newline printf doesn't add automatically
}
}
Output
Hello, World!
Core Logic
printf() writes formatted output using a template string, which becomes useful the moment a program needs to mix in variable values.
How It Works
- 1
System.out.printf("Hello, World!%n")takes a format string as its argument, the same wayprintlntakes plain text. - 2
%nis printf's placeholder for a platform-correct newline — printf doesn't add one automatically the wayprintlndoes. - 3With no other
%placeholders in the string, this call behaves just likeprintln, just written a different way.
System.out.printf("Hello, World!%n") prints the same single line as println("Hello, World!").💡
Key Point: printf only pays off once a format string has real placeholders — System.out.printf("Hello, %s!%n", name) substitutes name into %s, something plain println can't do without manual string concatenation.
Key Concepts
System.out.printf()format string%n