How to write our first java program with an example?

How to write our first java program with an example?

After Successfully installation of java now the question arises that how to write our first java program. So here we try to create a very basic java program. it is a tradition when we start learning a new programming language then it goes with a program called “Hello World.”

So to start writing we need this 2 software to create our first Java Program i.e. First the Java SE Development Kit and the Second is a text editor or IDE.

Here in this Java example, we use Notepad. It is a very simple editor that included various basic features for Windows Operating System. You can use a different text editor like NotePad++, Eclipse, or NetBeans, or use an online java converter also.

So the basic steps to create a Hello World java program are:

  • Write the program
  • Save the file.
  • Compile the code
  • Run the program
  • Get the output.

Step 1: Navigate to your Desktop folder.

Step 2: Now in the desktop window simply right-click and click on New over that and click on “Text Document” this will create a new text document, so we can simply rename it.  

Step 3: Now leave that document with the default file name because we will rename it later.

Step 4: So open our newly created text document.  


Step 5: Inside that document file simply type “class HelloWorld {“, and remember one thing that does not forget the curly brace{ because this indicates to the computer where the program is started.


Step 6: Then slightly press enter twice to create two new lines and then type an ending curly brace “}” this indicates to the computer that everything in our program is inside these two curly braces. 

Informatics:

Now if we tried to run the program it through an error message that says “Error: Main method not found in class HelloWorld, please define a method “. So to overcome this error we have to write a line of code in the next step since it is an important part of the program.

Here we are using a very specific and demanded programming language called Java, so among most languages, Java has a very specific and unique way of organizing and expressing.

Every Java program is a combination of Classes and Methods. Classes are more commonly known as the grouping of objects or collections of objects having identical properties and provide us with an organized way to write our code. So right now our HelloWorld program is just an empty class like you can say a keyboard with no keys.

Now after detecting the word class Java’s syntax dictates that we need to state the program’s name. An important thing is to note that most programming languages will not allow spaces between the names of classes and methods. It is because the use of empty space within code helps the compiler to get confused between differentiate.

After the class name, we use curly braces (“{” “}”) to denote where the program starts (“{“) and where it ends (“}”). So when we pass it through the compiler, the compiler will produce a program, but since now our class has no methods to use so an error will occur here.

Step 7: Now within the curly braces simply write “public static void main(String[] args) {” then slightly press enter twice to create two new lines and then type an ending curly brace “}”

Note: The main() method indicates that it is the entry point into the program.

Step 8: And now in between the body of the main() method that is in the middle of the program press the tab and type “System.out.println(“Hello World !!” );

Informatica:

The statement “System.out.println(“Hello World !!” );” will produce the output “Hello World !!” on the output screen. The actual meaning of this statement is what line is going to be displayed to the user that tells the computer to do something.

The line “System.out” is the first part of this instruction statement it means System is a class that is available at any time anywhere. And “.out” is allow to System to give us access to a class that gives the computer’s output. And calling the “println” method it will write or print the text passed which is passed to the method.

Step 9: After completion of the above steps now save the program with the same class name i.e.  “HelloWorld” with the “.java” extension. Example: “HelloWorld.java”.

Step 10: Open the Command Prompt by navigating to the Desktop directory.

Step 11: Compile the program. Type on the command prompt “javac HelloWorld.java”. then press enter.

Then it will generate a class file with the same name including the “.class” extension. Within the class file, the bytecode of the program will generate.

Step 12: Now type on the command prompt “java HelloWorld.java” to run the code.

Hurry Up! Successfully You run your first java program.

This is the output that you have passed within the “System.out.println():” statement.

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry