Simple Program of Java
In this page, we will learn how to write the simple program of java. We can write a simple
hello java program easily after installing the JDK.
To create a simple java program, you need to create a class that contains main method.
Let's understand the requirement first.
Requirement for Hello Java Example
For executing any java program, you need to
o install the JDK if you don't have installed it, download the JDK and install it.
o set path of the jdk/bin directory. http://www.javatpoint.com/how-to-set-path-in-
java
o create the java program
o compile and run the java program
Creating hello java example
Let's create the hello java program:
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
Test it Now
save this file as Simple.java
To compile: javac Simple.java
To execute: java Simple
Output:Hello Java
Understanding first java program
Let's see what is the meaning of class, public, static, void, main, String[],
System.out.println().
o class keyword is used to declare a class in java.
o public keyword is an access modifier which represents visibility, it means it is visible
to all.
o static is a keyword, if we declare any method as static, it is known as static method.
The core advantage of static method is that there is no need to create object to
invoke the static method. The main method is executed by the JVM, so it doesn't
require to create object to invoke the main method. So it saves memory.
o void is the return type of the method, it means it doesn't return any value.
o main represents startup of the program.
o String[] args is used for command line argument. We will learn it later.
o System.out.println() is used print statement. We will learn about the internal
working of System.out.println statement later.
To write the simple program, open notepad by start menu -> All Programs ->
Accessories -> notepad and write simple program as displayed below:
As displayed in the above diagram, write the simple program of java in notepad and
saved it as Simple.java. To compile and run this program, you need to open command
prompt by start menu -> All Programs -> Accessories -> command prompt.
To compile and run the above program, go to your current directory first; my current
directory is c:\new . Write here:
To compile: javac Simple.java
To execute: java Simple
How many ways can we write a java program
There are many ways to write a java program. The modifications that can be done in a java
program are given below:
1) By changing sequence of the modifiers, method prototype is not changed.
Let's see the simple code of main method.
1. static public void main(String args[])
2) subscript notation in java array can be used after type, before variable or after
variable.
Let's see the different codes to write the main method.
1. public static void main(String[] args)
2. public static void main(String []args)
3. public static void main(String args[])
3) You can provide var-args support to main method by passing 3 ellipses (dots)
Let's see the simple code of using var-args in main method. We will learn about var-args
later in Java New Features chapter.
1. public static void main(String... args)
4) Having semicolon at the end of class in java is optional.
Let's see the simple code.
1. class A{
2. static public void main(String... args){
3. System.out.println("hello java4");
4. }
5. };
Valid java main method signature
1. public static void main(String[] args)
2. public static void main(String []args)
3. public static void main(String args[])
4. public static void main(String... args)
5. static public void main(String[] args)
6. public static final void main(String[] args)
7. final public static void main(String[] args)
8. final strictfp public static void main(String[] args)
Invalid java main method signature
1. public void main(String[] args)
2. static void main(String[] args)
3. public void static main(String[] args)
4. abstract public static void main(String[] args)
Resolving an error "javac is not recognized as an internal or external
command" ?
If there occurs a problem like displayed in the below figure, you need to set path. Since
DOS doesn't know javac or java, we need to set path. Path is not required in such a case if
you save your program inside the jdk/bin folder. But its good approach to set path. Click
here for How to set path in java.
Internal Details of Hello Java Program
1. Internal Details of Hello Java
In the previous page, we have learned about the first program, how to compile and how to
run the first java program. Here, we are going to learn, what happens while compiling and
running the java program. Moreover, we will see some question based on the first program.
What happens at compile time?
At compile time, java file is compiled by Java Compiler (It does not interact with OS) and
converts the java code into bytecode.
What happens at runtime?
At runtime, following steps are performed:
Classloader: is the subsystem of JVM that is used to load class files.
Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.
Interpreter: read bytecode stream then execute the instructions.
Q)Can you save a java source file by other name than the class
name?
Yes, if the class is not public. It is explained in the figure given below:
To compile: javac Hard.java
To execute: java Simple
Q)Can you have multiple classes in a java source file?
Yes, like the figure given below illustrates:
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
Java Programs
Java programs are frequently asked in the interview. These programs can be asked from
control statements, array, string, oops etc. Let's see the list of java programs.
1) Fibonacci series
Write a java program to print fibonacci series without using recursion and using recursion.
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
2) Prime number
Write a java program to check prime number.
Input: 44
Output: not prime number
Input: 7
Output: prime number
3) Palindrome number
Write a java program to check palindrome number.
Input: 329
Output: not palindrome number
Input: 12321
Output: palindrome number
4) Factorial number
Write a java program to print factorial of a number.
Input: 5
Output: 120
Input: 6
Output: 720
5) Armstrong number
Write a java program to check Armstrong number.
Input: 153
Output: Armstrong number
Input: 22
Output: not Armstrong number
Java Sorting Programs
6) Bubble Sort
Write a java program to sort an array elements using bubble sort algorithm.
Input: 18 9 33 4 84 32
Output: 4 9 18 32 33 84
7) Selection Sort
Write a java program to sort an array elements using selection sort algorithm.
Input: 18 9 33 4 84 32
Output: 4 9 18 32 33 84
8) Insertion Sort
Write a java program to sort an array elements using insertion sort algorithm.
Input: 18 9 33 4 84 32
Output: 4 9 18 32 33 84
Java Searching Programs
9) Linear Search
Write a java program to perform linear search in java.
10) Binary Search
Write a java program to perform binary search in java.
Java Array Programs
11) Find 3rd Largest Number in an Array
Write a java program to find 3rd largest number in an array.
Input: 1,2,5,6,3,2
Output: 3
12) Find 2nd Largest Number in an Array
Write a java program to find 2nd largest number in an array.
Input: 1,2,5,6,3,2
Output: 5
13) Find Largest Number in an Array
Write a java program to find largest number in an array.
Input: 1,2,5,6,3,2
Output: 6
14) Find 2nd Smallest Number in an Array
Write a java program to find 2nd smallest number in an array.
Input: 1,2,5,6,3,2
Output: 2
15) Find Smallest Number in an Array
Write a java program to find smallest number in an array.
Input: 1,2,5,6,3,2
Output: 1
16) Remove Duplicate Element in an Array
Write a java program to remove duplicate element in an array.
Input: 10,20,20,30,30,40,50,50
Output: 10 20 30 40 50
17) Add Two Matrices
Write a java program to add two matrices.
Input:
First matrix elements:
1 1 1
2 2 2
3 3 3
Second matrix elements:
1 1 1
2 2 2
3 3 3
Output:
Addition of the matrix:
2 2 2
4 4 4
6 6 6
18) Multiply Two Matrices
Write a java program to multiply two matrices.
Input:
First matrix elements:
1 1 1
2 2 2
3 3 3
Second matrix elements:
1 1 1
2 2 2
3 3 3
Output:
Multiplication of the matrix:
6 6 6
12 12 12
18 18 18
19) Print Odd and Even Number from an Array
Write a java program to print odd and even number from an array.
Input: 1,2,5,6,3,2
Output:
Odd Numbers:
1
5
3
Even Numbers:
2
6
2
20) Transpose matrix
Write a java program to transpose a matrix.
Output:
Printing Matrix without transpose:
1 3 4
2 4 3
3 4 5
Printing Matrix After Transpose:
1 2 3
3 4 4
4 3 5