Java Reference
In-Depth Information
The closing brace of a block is tabulated exactly under the statement that contains
the opening brace. NetBeans formats the code automatically as you type. However, if
for some reason the code is not formatted, you can auto-format it. Simply go to the
menu Source and select Format .
Next, we will modify our code to print “Hello World!” To do so, simply type a statement
that does the printing in the main method.
package helloworld ;
public static void main(String args [])
public class Main
System. out . println ( "Hello World!" );
The statement: System.out.println("Hello World!") does the printing. The
println method stands for print and then print new line. Alternatively, the statement
System.out.print("Hello World!") will just print Hello World without moving to the
next line. In Java, the symbol “
” is used to display a special character. For example,
System.out.print("Hello World! \ n") will display Hello World and then a new line.
The reason is that the character “
n” displays a new line. For example, the statement
System.out.print("Hello \ n World \ n") will display Hello onthefirstlineand World on
the next line.
Note that NetBeans supports auto-complete. For example, we can write Sys and press
Ctrl+Space . System will automatically be displayed. We just need to press Enter to select
it. The semicolon at the end denotes the end of the statement. Note that the statement will
be underlined with a red line if we forget the semicolon. This is NetBeans's way of telling
us that something is wrong. If we hover over the red line, we will see the message: ';'
expected . Note that one needs to resolve all errors before the program can be executed.
In Java, spacing and new lines are not always important. For example, ten com-
mands can be put on the same line. This is why putting semicolon after every statement
is crucial.
A literal in Java is a constant that is specified in the code. For example, 2 is an example
of an integer literal and 2.33 is an example of a literal that is a real number. Note that in
the program the string "Hello world!" is surrounded by double quotes. All string literals
in Java are surrounded in double quotes. Conversely, as we will see later, single characters
are surrounded in single quotes. The ptintln line does the printing. Note that the line calls
the println method. As a parameter to the method, the string "Hello World!" is passed.
Note that every time we call a method, we need to specify in parentheses the parameters
of the method. If the method takes as input no parameters, then we should type only the
opening and closing parenthesis.
Thefirsttimethatweruntheprogram,weneedtoright-clickon in the
Projects view and select Run File . For sequential executions of the program, we can just
click on the green arrow above the code. Note that the first time that we click the green
arrow, it will ask us where the main method is. The reason is that there can potentially be
multiple main methods. Figure 2.4 shows the result of executing the program. Note that one
can run the program by clicking on the green arrow only if the correct project is selected
Search WWH ::

Custom Search