Writing the first java program

Step 1: Install Java

Before you can write and run Java programs, you need to have Java installed on your computer. You can download and install Java from the official Oracle website.

Step 2: Set up a Text Editor

You can write Java code using any text editor, like Notepad on Windows or TextEdit on Mac. Just make sure to save your file with a ".java" extension. For example, you can save it as "MyFirstProgram.java".

Step 3: Write Your Java Program

Open your text editor and start writing your Java program. Here's a simple example of a "Hello, World!" program:



Explanation of code

public class MyFirstProgram: This line declares a class named "MyFirstProgram." In Java, every program starts with a class.

public static void main(String[] args): This is the main method of your program. It's the entry point, where your program begins executing. The public static part is necessary for the program to run.System.out.println("Hello, World!"); This line prints "Hello, World!" to the console. It's a simple way to display text on your screen.

Step 4: Save Your Program
After writing your program, save the file with a ".java" extension, like "MyFirstProgram.java" This is important so that Java knows it's a source code file.

Step 5: Compile Your Program
Open your command prompt or terminal and navigate to the folder where you saved your Java file. Then, compile your program by typing:
javac MyFirstProgram.java
This command tells Java to compile your code into a format that the computer can understand.

Step 6: Run Your Program
Once the compilation is successful, you can run your program by typing:
java MyFirstProgram
Your program will execute, and you'll see the output "Hello, World!" displayed in the console.





Post a Comment

0 Comments