ComputersProgramming

The first Java program is Hello World

In the field of development in the study of the programming language, it is customary to greet the world with its first application. Regardless of the particular environment, the phrase derived from the work of the Hello World program will be the initial stage for mastering. The following is an example of such a simple Java application.

Preparing to create the first application

Before you create your Hello World in Java, you need to download and install the software. The first thing you need is JDK. This is a set of developer tools. Download it preferably from the official website of Oracle. The latest version of Java to date - 8. Developer toolkit is available for download and installation on almost all known platforms.

Further there is a choice to use any IDE, that is, the development environment, for example, Eclipse or NetBeans, or write the code in any text editor. For simplicity, the article will use an easier way - using a notebook.

Hello World in Java using a simple text editor

It is assumed that the system already has a JDK installed. Now you need to run the most common notepad in Windows or gedit or nano on Linux. You can immediately save it by naming a file, for example HelloWorld.java. Then in the text field you need to enter some simple lines of code:

Class HelloWorld {

}

This is where the class is naming. According to the requirements of the Java language, it should be named the same as the file itself, which is located. Next are curly braces, Java syntax elements showing where the class starts and ends. Inside it, you now need to define a method that will do something:

Class HelloWorld {

Public static void main (String [] args) {

}

}

It is with the main method that the beginning of any Java program begins. The public keyword is an access modifier that indicates that it is public and can be used anywhere. You can find out how to handle it in the official documentation for the language.

Next is static, which means a static method, that is, created not at the call, but at the beginning of the program. The keyword void says that main will not return any data, but will just do something.

String in parentheses and the characters [] mean that the method can take on input certain values, for example, for their subsequent processing. And the name of the variable args makes it possible to refer to it in the data inside the method. It is worth noting that main, like the class, is framed by curly braces, showing "its" territory.

Well, it remains to add a way to output the result of the program:

Class HelloWorld {

Public static void main (String [] args) {

System.out.println ("Hello World!");

}

}

This command simply displays the line in parentheses on the screen. At the end, a semicolon operator is used. Thus, Java indicates the end of an expression or a line of code. The value to print as a result is quotation marks.

Running and testing the first application

To run the resulting Hello World in Java, you need to go to the console. In Windows, it is called the command line, and in Linux it is called the terminal. Once in the console, you need to get to the previously saved file HelloWorld.java. For example, in Windows, it can be on the path C: \ Java \ Hello. Then just type in the console command cd C: \ Java \ Hello. Go to the folder with the program file, you must first translate it into a bytecode. The following construction is used for this:

Javac HelloWorld.java

After that, you need to run the resulting class for execution. There is also a separate command for this:

Java HelloWorld.

On the console screen, you should see the result of Hello World on Java - the greeting of the world around you.

Conclusion

Programming in Java is a very interesting and useful activity. This platform is capable of running on a variety of devices - from simple mobile phones to huge server stations. Many large Internet portals use Java as the main platform for processing server requests. Also on it is written code for the now popular Android operating system. And with such a development trend, like it, Java has a great future.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.