Let us write our first program in Java. We’ll print “Hello World” on the console.
1. Write Hello World program in Java
Write in following peace of code and save as file HelloWorldApplication
and extension as java
in your favorite code editor.
public class HelloWorldApplication { public static void main(String[] args) { System.out.println("Hello World"); } }
Output
Hello World
2. javac – Compile java program
javac HelloWorldApplication.java
The code which you have written will not work until and unless it is compiled and run. So, first we have to compile the code.
javac
command is used to compile the Java code. The syntax of javac
is:
javac [options] [source files]
Note: Both options and source files are optional in the javac
command. You can provide multiple entries for both options and source files.
There are many options which you can provide with javac
command.
When we compile the java file, .class
file is generated if there is no compilation error.
2.1 Get help about javac command
javac -help
2.2 Compiling multiple files
You can provide multiple files with javac
command separated by spaces.
javac FirstFile.java SecondFile.java
3. Run Java application
java HelloWorldApplication
java
command is used to launch JVM which then executes the program. The basic syntax of java
command is:
java [options] class [arguments]
Here options and arguments are optional and both can have multiple values.
Note: You need not to provide any extension with the class name. It is the .class
file which will be executed. You can not use .java
file with java command to run the program.
3.1 Passing arguments to java command
java HelloWorldApplication 1 99
In this command 1 and 99 are arguments provided to the java
command. These will be received in the args parameter in our code:
public static void main(String[] args)
args[]
is an array. If you want to access these arguments, you can access these arguments as args[0]
and args[1]
.
public class HelloWorldApplication { public static void main(String[] args) { System.out.println(args[0]); System.out.println(args[1]); } }
Output
1
99
4. Compiling Java file with package
When your class file is defined in a package, you need to provide some extra information to compile and run Java program.
package com.learnitweb; public class ArgumentsApplication { public static void main(String[] args) { System.out.println("Hello World"); } }
Note that we have defined here that HelloWorldApplication
is in com.learnitweb
package.
4.1 How to compile a class defined in a package
javac -d . HelloWorldApplication.java
- Here -d defines the directory where generated files should be placed.
- . (dot) means place the generated class files in the current directory.
HelloWorldApplication.java
is the java program to be compiled.
On successful compilation, one folder will be created for every level of package. So our HelloWorldApplication.class
file will be placed in learnitweb
folder inside com
folder.
4.2 How to run the Java program defined in a package
Since we have defined our java program inside a package, and class is generated inside the com\learnitweb
folder. We have to provide fully classified name to run the program.
java com.learnitweb.HelloWorldApplication
5. Let’s discuss our Hello World Application
Let us understand the program we have written.
- public class HelloWorldApplication
We are declaring a class here with name HelloWorldApplication. Class name should be preceded with word class
. public
here is the access modifier which defines class as public which means the class can be accessed within the package and outside the package.
2. curly braces ({ })
Curly braces define the starting and ending point of the block of code.
3. public static void main(String[] args)
Here we are defining a method with name main
.
void
means the method does not return any value.
The main
method accepts one argument args
which is of type String
array. This is public
and static
method.
static
means this method is associated with the class and not the object. We have declared main
method as static
because we run it without an object.
4. System.out.println(“Hello World”);
Prints Hello World
in new line.
System
is a class. out
is a member variable in class System
of type PrintStream
. println
is a method in class PrintStream
.