Writing your first Java program helps you learn how to tell the computer what to do using Java language. It shows you the basic structure of a Java program.
0
0
Writing first Java program
Introduction
When you want to learn how Java programs are built.
When you want to test if your Java setup is working correctly.
When you want to print a simple message on the screen.
When you want to understand how to run Java code step-by-step.
Syntax
Java
public class ClassName { public static void main(String[] args) { System.out.println("Your message here"); } }
public class ClassName defines the program's name and container.
main method is where the program starts running.
Examples
This prints the message "Hello, world!" on the screen.
Java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }
This prints "Good morning!" as a greeting.
Java
public class Greeting { public static void main(String[] args) { System.out.println("Good morning!"); } }
Sample Program
This program prints a welcome message to the screen. It shows the basic parts of a Java program: class, main method, and printing text.
Java
public class FirstProgram { public static void main(String[] args) { System.out.println("Welcome to Java programming!"); } }
OutputSuccess
Important Notes
Make sure the file name matches the class name exactly (FirstProgram.java for class FirstProgram).
Use a Java compiler like javac to turn your code into something the computer can run.
Run the program using java ClassName command after compiling.
Summary
Java programs start with a class and a main method.
Use System.out.println() to print messages on the screen.
File name must match the class name for the program to run correctly.