This section contains some basic examples of java programs, specially for beginners to get some more idea about execution flow in java programs. If you don't know how to compile and execute java programs, then refer My first java program tutorial first.
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 observe 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 method in java ?
println("...")
method prints the
string inside it and moves the cursor to new line, while print("...")
method prints the string inside it and remains the cursor in same
line.
Let's see one more 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 caller of
method and if a method call is encountered, the execution moves
into the new method.
Command line arguments
A command line argument is an argument that is passed to
program while running the program. This argument get's stored in args
variable of main method which you can use in main method.
For example if you want to pass a command line argument to a program CommandLineArgument.java
given below, you need to write java
command like below at the time of execution :
java CommandLineArgument firstArgument
Here "firstArgument
" is an argument which is passed to CommandLineArgument
program and get's stored in args
variable of main
method. So anything that is passed after class name in java
command acts as command line argument.
Let's see the program of command line argument.
class
CommandLineArgument {public static void
main(String [] args) { System.out.println("args =
"+args[0]); } }
Compile the above program as javac CommandLineArgument.java
and run it using the command below.
java CommandLineArgument 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 program, you can do that by using space between arguments. For example command below passes multiple arguments to program.
java CommandLineArgument firstArgument secondArgument thirdArgument
These arguments will be stored in args
variable and can be accessed using
args[0], args[1], args[2]
and so on respectively.
Calling method using object
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. We will get to know more
about this in later sections.