Table of Contents
simple Java language programs
In this tutorial, we will learn some of simple Java language programs
We are going to learn some simple Java programs and how to write those programs for beginners.
Java program has three segments
- Class declaration
- pre define method(main method) definition
- Print statements
Example
{
{
}
my first java program
How to write my first java program
Let,s look at how to save the file, compile and run this program please follow these steps
- Open the Notepad++ and add the above code
- Save the file,
- file name My_First_Java_Program.java
- the file name must be the same as the class name
- Open the command prompt
- go to the directory
- fine where you saved the file.
The explanation is given below
- Type ‘ javac My_First_Java_Program.java’ and press enter to compile your code. If there are no error in your code, the commend prompt Will take you to the next line
- Then, type ‘ java My_First_Java_Program’ to run your program and enter the enter key
- You will be able to see ‘my first java program’ printed on the window
Explanation of main method in Java
We use public static void main String args[]. Each word has different purpose and meaning
public : the public is an Access Modifier, it defines who can access this method. public means that this method will be accessable by any class. that means if other classs are able to access this class
static : it is a keyword in java, which is used several ways in java.
void: it is used to define the return type of the method. it defines what the method can returm. Viod means the Method will not return any value. (one of the return type int, which returns any value)
main: it means it is a strating point for an application in java. It is named in method
String args[]: it is parametter to the main method.
Example programs
2 nd program
This is second and simple java program this program display a byte value
first, assign a variable mynewmarks in byte then assign a value 10 to mynewmarks
then call display mynewmarks – 10
my first java program public class Mynewmarks //class declaration { public static void main(String args[]) //main method { byte mynewmarks; //variable declaration mynewmarks=10; //value initialization to variable System.out.print(mynewmarks);// display statement } }
When the above code is executed it produces the following result
out put - 10
Some program Examples given below
public class alphaphat{ public static void main(String args[]){ char ch='A'; System.out.print(ch); } }
output - A
public class char1{ public static void main(String args[]){ char ch=67; System.out.print(ch); } }
output - c
public class octal{
public static void main(String args[]){
char ch=067;
System.out.print(ch);
}
}
out put – 7
out put – my charecter is A1
8
public class alphapat2{
public static void main(String args[]){
char ch=0x67;
System.out.print(ch);
}
}
output – g
9
public class alphapat3{
public static void main(String args[]){
char ch=’A’;
System.out.print((char)(ch+1));
}
}
output – B
print bio data using Java program
class biodata{ public static void main(String args[]){ String fname; String lname; String fullname; int age; char bloodgroup; double salary; double height; String game; char divition; int grade; fname="karmehavannan"; lname="sathasivam"; age=20; bloodgroup='A'; salary=20000; height=163; grade=12; divition='B'; game="football"; System.out.println("my first name is"+" "+fname); System.out.println("my last name is" +" " +lname); System.out.println("my full name is" +" " +fname+" "+lname); System.out.println("my age is"+" "+age); System.out.println("my salary is"+" "+salary); System.out.println("my blood group is"+" "+bloodgroup); System.out.println("my height is"+" "+height); System.out.println("my fevorite game is"+" "+game); System.out.println("my Grade is"+" "+grade+divition); } }
When the above code is executed , it produces the following result
boolean
1. public class vanbuolean{
public static void main(String args[]){
boolean bul;
bul =true;
System.out.print(bul);
}
}
out put – true
Funny program
public class face1{
public class doublealphapat{
public static void main(String args[]){
char ch=2;
System.out.print(ch);
}
}
out put – face
Aarethmatic oparation
program 1
class add{ public static void main(String args[]){ int x=10; double y=3; System.out.println(x+y); System.out.println(x-y); System.out.println(x*y); System.out.println(x/y); System.out.println(x%y); } }
output
13.0 7.0 30.0 3.33333333333335 1.0
program 2
class oper{ public static void main (String arg[]){ int a =3; int b = 5; int c = 1; a= a+b+c; System.out.println("__________________________"); System.out.println(a); System.out.println(b); System.out.println(c); b+=a; System.out.println("__________________________"); System.out.println(a); System.out.println(b); System.out.println(c); c=b+++a--+--c; System.out.println("__________________________"); System.out.println(a); System.out.println(b); System.out.println(c); a=--b+--c+a++; System.out.println("__________________________"); System.out.println(a); System.out.println(b); System.out.println(c); } }
When the above code is executed it produces the following result
................................................. 9 5 1 .................................................. 9 14 1 .................................................... 8 15 23 ..................................................... 44 14 22 ......................................................
Unary operation
class add1{ public static void main(String args[]){ int x=10; int y=3; System.out.println(x+y); System.out.println(x-y); System.out.println(x*y); System.out.println(x/y); System.out.println(x%y); System.out.println(x-=1); System.out.println(x+1); System.out.println(x-1); System.out.println(x+=1); System.out.println(x-=1); System.out.println(++x); System.out.println(y); System.out.println(++y); System.out.println(y++); System.out.println(y); System.out.println(++y); System.out.println(++y+y); System.out.println(++y + ++y); System.out.println(++y + ++y+y++); } }
When the above code is executed it produces the following result
13 7 30 3 1 9 10 8 10 9 10 3 4 4 5 6 14 17 32