Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

First Java Program : Hello World

"Hello, World!" program in Java along with a detailed explanation of the code:

HelloWorld.java:

public class HelloWorld {
    public static void main(String[] args) {
        // This is a comment. Comments are ignored by the compiler.
        // The next line prints "Hello, World!" to the console.
        System.out.println("Hello, World!");
    }
}

Explanation:

  • public class HelloWorld {: In Java, every application begins with a class definition. In this case, we define a class named HelloWorld.

  • public static void main(String[] args) {: This line defines the main method, which is the entry point of every Java application. When you run a Java program, it starts executing from the main method.

  • // This is a comment.: Comments in Java start with //. Comments are ignored by the compiler and are used for providing explanations within the code.

  • System.out.println("Hello, World!");: This line prints "Hello, World!" to the console. Let's break it down:

    • System.out: System is a pre-defined class in Java that provides access to the system, and out is an object of type PrintStream which is connected to the console.

    • println("Hello, World!");: println is a method of the PrintStream class used to print a line of text to the console. In this case, it prints "Hello, World!" followed by a new line.

When you run this Java program, it will output:

Hello, World!

Explanation of Execution:

  1. The program starts executing from the main method.

  2. The System.out.println("Hello, World!"); statement is executed, printing "Hello, World!" to the console.

  3. The program completes its execution, and the output is displayed.

This simple program demonstrates the basic structure of a Java application, including class definition, the main method, comments, and the System.out.println statement used for output.

PreviousSetting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)NextVariable

Last updated 1 year ago