Table of Contents
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
Example
}
my first java program
Let,s look at how to save the file, compile and run this program please follow these steps
The explanation is given below
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.
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
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
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 ......................................................
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.