Java From Beginner To Advanced

0% completed

Previous
Next
Writing Your First Java Program

In this lesson, we will create a simple Java program that prints a message on the screen. We will look at the structure of a Java program, explain each part of the code, and learn how to run your program. This lesson is designed for beginners and covers every step in detail.

Understanding the Structure of a Java Program

Every Java program has a similar basic structure. The most important parts are:

  • Class Definition: Java code is written inside classes. A class is like a container for your code.
  • Main Method: The main method is the entry point. This is where the program starts running.
  • Statements: Each line of code ends with a semicolon (;), and they tell the computer what to do.

A Simple Example: "Hello World" Program

Below is a simple example of a Java program that prints "Hello, World!" to the screen:

Java
Java

. . . .

Let's break down this code step by step:

  • public class HelloWorld { ... }
    The keyword public means that the class is accessible by any other class. The name Solution is the name of our class, and it should match the file name (in this case, Solution.java).

  • public static void main(String[] args) { ... }
    This line defines the main method.

    • public means this method can be called from outside the class.
    • static means the method belongs to the class rather than an instance of the class.
    • void means this method does not return any value.
    • String[] args is a parameter that stores any command-line arguments.
  • System.out.println("Hello, World!");
    This command prints the text Hello, World! on the screen.

    • System.out is Java's standard output, and println is a function that prints a line of text.

Key Points to Remember

  • File Naming: Make sure the file name matches the class name exactly, including upper and lower case letters.
  • Syntax: Every statement ends with a semicolon (;).
  • Comments: Use comments (starting with //) to add notes in your code. They help you understand what your code does.

In this lesson, we learned how to write a simple Java program. We discussed the basic structure of a Java program, looked at each part of the code, and learned how to compile and run the program. By following these steps, you can write and run your very first Java program. In the next lessons, we will build on this foundation and explore more details about Java syntax and programming concepts.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next