In the field of development, when learning a programming language, it is customary to greet the world with its first application. Regardless of the specific environment, the phrase displayed as a result 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 the JDK. This is a set of developer tools. It is advisable to download it from the official site of Oracle. The latest version of Java today is 8. The developer’s toolkit is available for download and installation on almost all known platforms.
Next, there is a choice to use any IDE, that is, a development environment, for example, Eclipse or NetBeans, or write code in any text editor. For simplicity, the article will use a simpler method - using notepad.
Hello World in Java using a simple text editor
It is assumed that the system already has an installed JDK. Now you need to run the most ordinary notepad on Windows or gedit or nano on Linux. You can save it right away by naming the file, for example HelloWorld.java. Then in the text box you need to enter a few simple lines of code:
class HelloWorld {
}
This is where the class is named. According to the requirements of the Java language, it should be named in the same way as the file in which it is located. Next come braces, Java syntax elements showing where the class begins 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 any Java program begins to work. 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 comes static, which means a static method, that is, created not when called, but at the beginning of the program. The void keyword means that main will not return any data, but just do something.
String in brackets and characters [] mean that the method can take some values as input, for example, for their subsequent processing. And the variable name args makes it possible to access data inside the method. It is worth paying attention to the fact that main, like the class, is framed with curly braces, showing "their" territory.
Well, now it remains to add a way to display 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 brackets on the screen. At the end, the semicolon operator is used. Thus, Java indicates the end of an expression or line of code. The value to be displayed as the result is enclosed in quotation marks.
Launch and test the first application
To run the resulting Hello World in Java, you need to go to the console. On Windows, it is called the command line, and on Linux, the terminal. Once in the console, you need to get to the previously saved HelloWorld.java file. For example, on Windows it may appear along the path C: \ Java \ Hello. Then just enter the cd C: \ Java \ Hello command into the console. Going 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.
The result of the work of Hello World in Java should appear on the console screen - a greeting from the outside world.
Conclusion
Java programming is a very interesting and useful activity. This platform can run 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. It also writes code for the now popular Android operating system. And with a development trend like hers, Java has a great future.