This section contains some basic examples of java programs, specially for beginners to get some more idea about execution flow in java programs. It also shows you how to pass command line arguments to java programs and how to call method of a program using object. Refer My first java program tutorial first if you don't know how to compile and execute java programs.
Execution flow of java program
Both the programs given below demonstrates the execution flow of java programs.
class
FirstProgramFlow {public static void
main(String [] args) { System.out.println("first "
); System.out.print("second "
); System.out.println("third "
); } }
Output:
first
second third
As you can see from the output, the program executes from top to bottom starting from main
method. It executes first line then moves to second line, executes
second line then move to third line and so on.
What is the difference between print and println in java ?
println("...")
method prints the
string inside it and moves the execution pointer to new line, while print("...")
method prints the string inside it and keeps the pointer in same
line.
Let's see another program related to execution flow, this program calls method inside other method which makes it a bit more interesting for beginners to understand the execution flow. This program will give you more idea about execution flow in java.
class
SecondProgramFlow {public static void
main(String [] args) {// calling firstMethod
firstMethod(); System.out.println("main method"
); }public static void
firstMethod() { System.out.println("first method"
);// calling secondMethod and assigning return value in i
int
i = secondMethod(); System.out.println("After second method call, i = "
+i); }public static int
secondMethod() { System.out.println("second method"
);return
1; } }
Output:
first method
second method
After second method call,
i = 1
main method
Once a method is called, it finishes all it's statement until and
unless a return
or method call
statement is encountered. If a return
statement is encountered, the execution moves back to the line from where this method was called and if a method call is encountered, the execution moves
into the new method.
What is command line arguments in Java
A command line argument is an argument which is passed to
a program while running that program using java
command. This argument get's stored in args
(the name can be different) parameter of main
method.
You can access your argument using args
parameter inside main
method.
For an example if you want to pass a command line argument "firstArgument
" to a program MyFirstProgram.java
given below,
you need to run the program using java
command like below :
java MyFirstProgram firstArgument
Here firstArgument
is an argument which is passed to MyFirstProgram
and get's stored in args
variable of main
method. Anything which is passed after class name in java
command acts as command line argument and can be accessed
using args
variable.
Let's understand this by the program given below.
class
MyFirstProgram {public static void
main(String [] args) { System.out.println("args =
"+args[0]); } }
Compile the above program as javac MyFirstProgram.java
and run it using the command below.
java MyFirstProgram firstArgument
Output:
args = firstArgument
As you can see the "firstArgument
" is passed to program and get's printed in output. If you will not pass any argument to above program, it will
throw ArrayIndexOutOfBoundsException at runtime since you are accessing args[0]
in your program.
If you want to pass multiple arguments to your program, you can do that by using
space between arguments. For example command below passes three arguments to MyFirstProgram
.
java MyFirstProgram firstArgument secondArgument thirdArgument
These arguments will be stored in args
variable and can be accessed using
args[0], args[1] and args[2]
respectively.
How to call method using object in java
Since java is an object oriented programming language, you can create object in java and can call method using object. Program below illustrates how to create an object and call the method using that object.
class
MethodCallByObject {public static void
main(String [] args) {/* creating object obj of class MethodCallByObject */
MethodCallByObject obj =new
MethodCallByObject();/* calling firstMethod using object obj */
obj.firstMethod(); System.out.println("first"
); }void
firstMethod() { System.out.println("second"
); System.out.println("third"
); } }
Output:
second
third
first
In java
dot(.
) operator is used to access the member variable
and method of an object. In above example we are accessing firstMethod
using object obj
. The program above is used to give you a brief idea about how to create an object and call method using that. To get more detail about object
refer objects in java tutorial and for method refer methods in java tutorial.