Your First Java Program: Hello World

Tagged: ,

  • Your First Java Program: Hello World

  • StudentMia 

    August 24, 2021 at 2:15 pm

    1.1 Your First Java Program: Hello World

    In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. As with any application, you need to be sure that Java is properly installed on your computer. You also need an editor and a terminal application. Here are system-specific instructions for three popular home operating systems. [ Mac OS X · Windows · Linux ]

    Programming in Java. We break the process of programming in Java into three steps: Create the program by typing it into a text editor and saving it to a file named, say, <tt></tt>.Compile it by typing “<tt>javac</tt>” in the terminal window. Execute (or run) it by typing “<tt>java MyProgram</tt>” in the terminal window.

    The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named <tt>MyProgram.class</tt>); the third actually runs the program.

    Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for email. is an example program. Type these character into your text editor and save it into a file named <tt></tt>.

    Compiling a Java program. A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the <tt>.java</tt> extension as input (your program) and produces a file with a <tt>.class</tt> extension (the computer-language version). To compile <tt></tt> type the boldfaced text below at the terminal. (We use the <tt>%</tt> symbol to denote the command prompt, but it may appear different depending on your system.)

    If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.Executing (or running) a Java program. Once you compile your program, you can execute it. This is the exciting part, where the computer follows your instructions. To run the <tt>HelloWorld</tt> program, type the following in the terminal window:

    If all goes well, you should see the following response

    Understanding a Java program. The key line with <tt>System.out.println()</tt> prints the text “Hello, World” in the terminal window. When we begin to write more complicated programs, we will discuss the meaning of <tt>public</tt>, <tt>class</tt>, <tt>main</tt>, <tt>String[]</tt>, <tt>args</tt>, <tt>System.out</tt>, and so on.Creating your own Java program. For the time being, all of our programs will be just like <tt></tt>, except with a different sequence of statements in <tt>main()</tt>. The easiest way to write such a program is to:Copy <tt></tt> into a new file whose name is the program name followed by <tt>.java</tt>.Replace <tt>HelloWorld</tt> with the program name everywhere.Replace the print statement by a sequence of statements.Errors. Most errors are easily fixed by carefully examining the program as we create it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message.Compile-time errors. These errors are caught by the system when we compile the program, because they prevent the compiler from doing the translation (so it issues an error message that tries to explain why).Run-time errors. These errors are caught by the system when we execute the program, because the program tries to perform an invalid operation (e.g., division by zero).Logical errors. These errors are (hopefully) caught by the programmer when we execute the program and it produces the wrong answer. Bugs are the bane of a programmer’s existence. They can be subtle and very hard to find.One of the very first skills that you will learn is to identify errors; one of the next will be to be sufficiently careful when coding to avoid many of them.Input and output. Typically, we want to provide input to our programs: data that they can process to produce a result. The simplest way to provide input data is illustrated in Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message.

    ExercisesWrite a program that prints “<tt>Hello, World</tt>” ten times.Modify to make a program that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, “<tt>java UseThree Alice Bob Carol</tt>” gives “<tt>Hi Carol, Bob, and Alice.</tt>”.Web ExercisesWrite a program that prints your initials using nine rows of asterisks like the one below.

    Describe what happens if, in, you omit<tt>main</tt><tt>String</tt><tt>HelloWorld</tt><tt>System.out</tt><tt>println</tt>Describe what happens if, in, you omitthe <tt>;</tt>the first <tt>”</tt>the second <tt>”</tt>the first <tt>{</tt>the second <tt>{</tt>the first <tt>}</tt>the second <tt>}</tt>Describe what happens if, in, you misspell (by, say, omitting the second letter)<tt>main</tt><tt>String</tt><tt>HelloWorld</tt><tt>System.out</tt><tt>println</tt>I typed in the following program. It compiles fine, but when I execute it, I get the error <tt>java. lang.NoSuchMethodError: main</tt>. What am I doing wrong?

    Answer: you forgot the <tt>String[] args</tt>. It is required.

    For more visible code visit

Viewing 1 of 1 replies
Reply to: StudentMia<span class="bp-verified-badge"></span>
Your information:

Original Post
0 of 0 posts June 2018