Simple Program of Java: Requirement For Hello Java Example
Simple Program of Java: Requirement For Hello Java Example
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.
1. class Simple{
2. public static void main(String args[]){
3. System.out.println("Hello Java");
4. }
5. }
Test it Now
o String[] args is used for command line argument. We will learn it 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:
1. static public void main(String args[])
2) subscript notation in java array can be used after type, before variable or after
variable.
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)
1. class A{
2. static public void main(String... args){
3. System.out.println("hello java4");
4. }
5. };
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.
Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.
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:
-------------------------------------------------------------------------------------------------------------------------------
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
Input: 7
Output: prime number
3) Palindrome number
Write a java program to check palindrome number.
Input: 329
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
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
Input: 1,2,5,6,3,2
Output: 3
Input: 1,2,5,6,3,2
Output: 5
Input: 1,2,5,6,3,2
Output: 6
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
Input: 10,20,20,30,30,40,50,50
Output: 10 20 30 40 50
Input:
Output:
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:
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: