Learn Java Basics
About Lesson

Introduction to Java

Java is a high-level, object-oriented programming language that is widely used for developing a wide range of applications, including desktop, mobile, web, and enterprise software. It was first released in 1995 by Sun Microsystems (now owned by Oracle Corporation) and has since become one of the most popular programming languages in the world.

One of the key features of Java is its “write once, run anywhere” principle, which means that once a Java program is written, it can run on any platform that supports the Java Virtual Machine (JVM), including Windows, Mac, Linux, and mobile devices.

Java also offers many other benefits, including its ability to provide robust security features, its support for multithreading, and its vast collection of libraries and frameworks that can simplify development and accelerate time-to-market.

First Java Program

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

Structure of a Java

  1. Package declaration (optional) – This is used to group related classes together and it is placed at the beginning of the source file.

  2. Import statements (optional) – These are used to import classes or packages that are used in the program.

  3. Class declaration – This is where the main class of the program is declared. It contains the class name and the main method.

  4. Main method – This is the entry point of the program and it is where the program execution begins.

  5. Statements and expressions – These are the instructions that make up the logic of the program. They can include variable declarations, assignments, method calls, conditional statements, loops, and more.

  6. Comments (optional) – These are used to add notes or explanations to the code for future reference or for other programmers who may need to work with the code.

Here is an example of a simple Java program that demonstrates the basic structure:


package com.example;

public class MyProgram {
    
    public static void main(String[] args) {
        // Print "Hello, world!" to the console
        System.out.println("Hello, world!");
    }
    
}

This program declares a class called MyProgram in the com.example package and defines a main method that prints "Hello, world!" to the console when the program is executed.



Structure of a Java

  1. Package declaration (optional) – This is used to group related classes together and it is placed at the beginning of the source file.

  2. Import statements (optional) – These are used to import classes or packages that are used in the program.

  3. Class declaration – This is where the main class of the program is declared. It contains the class name and the main method.

  4. Main method – This is the entry point of the program and it is where the program execution begins.

  5. Statements and expressions – These are the instructions that make up the logic of the program. They can include variable declarations, assignments, method calls, conditional statements, loops, and more.

  6. Comments (optional) – These are used to add notes or explanations to the code for future reference or for other programmers who may need to work with the code.

Here is an example of a simple Java program that demonstrates the basic structure:


package com.example;

public class MyProgram {
    
    public static void main(String[] args) {
        // Print "Hello, world!" to the console
        System.out.println("Hello, world!");
    }
    
}




Join the conversation