The source file in Java language is a code that contains a description of one or more classes. Java compiler assumes that the source code of the programs is stored in files with extensions of Java. A code resulting in the translation process for each class is written in a separate output file – the name of which matches the name of the class, and extension “class.”
First of all, in this post, we will write and run the canonical program “Hello World.” By the end of the post, you will get enough information to orient yourself in Java language.
Hello World
So here’s your first Java program:
class HelloWorld {
public static void main (String args []) {
System. out. println (“Hello World”);
}
}
To work with the examples, you need to get through the network of Sun Microsystems and install the Java Developers Kit – package for the development of Java-based applications. Java language requires that all code is entered in named classes. The above example of a code should be written to a file HelloWorld.java. Be sure to check compliance with capital letters in the file name to the same name in the contained class. To translate this example, you must run the compiler Java – javac, specifying the name as a parameter of the file with source text:
javac HelloWorld.Java
Assembler will create a file HelloWorld.class independent of the processor byte-code of our example. In order to fulfill the resulting code, a Java expert needs to have a run-time environment of Java language (in this case, Java program), which is necessary to load a new class for execution. We emphasize that the name of the class is mentioned, not the name of the file where this class is contained.
java HelloWorld
Hello World
It was made with a few useful things, but we are sure that the installed Java-compiler and runtime work.
Step by Step
Hello World is a trivial example. However, even such a simple program in Java might seem intimidating to a newbie, because it introduces you to a lot of new concepts and details of language syntax. Let’s carefully go through each line of our first example by analyzing the elements that make up the Java-program.
- Line 1
class HelloWorld {
In this line, a Java expert should use the reserved word “class.” It tells the compiler that we’re going to describe the new class. Full description of the class is located between the opening brace on the first line and its pair – a closing brace on line 5. The braces in Java are used in the same way as in the languages C and C ++.
- Line 2
public static void main (String args []) {
This, at first glance, overly complex line of the example is a consequence of the important requirement laid down in the development of the language. There are no global functions in Java.
- Public. Breaking this line into individual tokens, we are immediately face with the keyword “public.” This is the access modifier which allows a Java expert to control the visibility of any method and any variable.
- Static. With this word, you declare methods and class variables, used to work with the class as a whole.
- void. If you just need to display the line and you don’t need to return the value of the “main” method, use the modifier “void.”
- Main. There is nothing unusual, just all the existing implementation of Java-interpreters, received the command to interpret the class, start their work with method “main.”
- Line 3
System. out. prlntln(“Hello World!”);
This line runs the “println” method of the object “out.” “Out” object is declared in the class OutputStream and is statically initialized in the class System.
The closing brace in line 4 ends with a method declaration “main,” and the same bracket in line 5 completes the declaration of the class HelloWorld.
If you need an expert who knows Java perfectly, and can provide Java homework help, you’ve found the right place. Experts at GetCodingHelp are knowledgeable not only in web programming, but in web design, IT administration, app development, and other spheres of web development. If you need our professional help, fill in an order form with your requirements and set the deadline. With our help, you can be sure in your future success.