<< Chapter < Page Chapter >> Page >

The Java version of Hello World

The class file

Compiled Java programs are stored in "bytecode" form in a file with an extension of .class where the name of the file is the same as the name of the controlling class (or other class) in the program.

The main method is static

The main method in the controlling class of an application must be static , which results in main being a class method.

Class methods can be called without a requirement to instantiate an object of the class.

When a Java application is started, the Java Virtual Machine or JVM (an executable file named java.exe) finds and calls the main method in the class whose name matches the name of the class file specified on the command line.

Running an application

For example, to start the JVM and run a Java application named hello1 , a command such as the following must be executed at the operating system prompt:

null

This command instructs the operating system to start the JVM, and then instructs the JVM to find and execute the java application stored in the file named hello1.class . (Note that the .class extension is not included in the command .)

This sample program is a Java application named hello1.java .

When compiled, it produces a class file named hello1.class .

When the program is run, the JVM calls the main method defined in the controlling class .

The main method is a class method.

Class methods can be called without a requirement to instantiatean object of the class.

The program displays the following words on the screen:

Hello World

Interesting code fragments

I will explain this program code in fragments. A complete listing of the program is provided in Listing 5 .

The code fragment in Listing 1 shows the first line of the class definition for the controlling class named hello1 . (I will discuss class definitions in detail in a future module.)

Listing 1 . Beginning of the class named hello1.
class hello1 { //define the controlling class

The code fragment in Listing 2 begins the definition of the main method. I will also discuss method definitions in detail in a future module.

Listing 2 . Beginning of the main method.
public static void main(String[] args){

The fragment in Listing 3 causes the string Hello World to be displayed on the command-line screen.

The statement in Listing 3 is an extremely powerful statement from an object-oriented programming viewpoint. When you understand how itworks, you will be well on your way to understanding the Java version of Object-Oriented Programming (OOP).

I will discuss this statement in more detail later in a future module.

Listing 3 . Display the string Hello World.
System.out.println("Hello World");

Listing 4 ends the main method and also ends the class definition for the class named hello1 .

Listing 4 . End of the class named hello1.
}//end main }//End hello1 class

The complete program listing

As mentioned earlier, a complete listing of the program is provided in Listing 5 near the end of the module.

General information

This program illustrates several general aspects of Java programming.

Overall skeleton of java program

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask