public class Add{
public static void main(String args[])
{
int n1=5,n2=10,sum=0;
sum=n1+n2;
System.out.println("Sum of the numbers:" + sum);
}
}
import java.util.Scanner;
public class Addition{
public static void main (String args[]){
Scanner n1=new scanner(System.in);
int num1,num2,sum=0;
System.out.println("Enter a numbers:");
num1=n1.next Int();
num2=n1.next Int();
sum=num1+num2;
System.out.println("The sum of the numbers:"+ sum);
}
}
class Area{
public static void main(String args[]){
double pi, r, a;
r=10.8;
pi=3.1416;
a=pi*r*r;
System.out.println("Area of circle is "+a);
}
}
class AreaOfRect{
public static void main(String args[]){
float area, length, breadth;
length=15.9f;
breadth=12.7f;
area=length*breadth;
System.out.println("Area of the rectangle is "+area);
}
}
class ExDoWhile{
public static void main(String args[]){
int n=10;
do{
System.out.println("Tick "+n);
n--;
}while(n>0)
}
}
class ExWhile{
public static void main(String args[]){
int n=10;
while(n>0){
System.out.println("Tick "+n);
n--;
}
}
}
import java.util.Scanner;
class FloatAdd{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
float num1,num2,sum;
System.out.println("Enter the 2 numbers");
num1=input.nextFloat();
num2=input.nextFloat();
sum=num1+num2;
System.out.println("The sum is "+sum);
}
}
class ForEach{
public static void main(String args[]){
int nums[] = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for(int x : nums){
System.out.println("Value is "+x);
sum=sum+x;
}
System.out.println("Summation :"+sum);
}
}
class getArg{
public static void main(String args[]){
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
}
}
}
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
//Java program to initialize the values from one object to another object.
class Student6{
int id;
String name;
//constructor to initialize integer and string
Student6(int i,String n){
id = i;
name = n;
//constructor to initialize another object
Student6(Student6 s){
id = s.id;
name =s.name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
//Java program to overload constructors in java
class Student2
int id;
String name;
int age;
//creating two arg constructor
Student2(int id,String name)
this.id = id;
this.name = name;
//creating three arg constructor
Student2(int id,String name,int age)
this.id = id;
this.name = name;
this.age=age;
void show()
System.out.println(id+" "+name+" "+age);
public static void main(String args[])
Student2 s1 = new Student2(100,"Ram");
Student2 s2 = new Student2(200,"Rahim",18);
s1.show();
s2.show();
Example2: We can copy the values of one object into another by assigning
the values of the object to another object. In this case, there is no need to
create the Constructor.
See the following code.
// Student4.java
class Student4
int id;
String name;
Student4(int id,String name)
{
this.id = id;
this.name = name;
Student4()
void show()
System.out.println(id+" "+name);
public static void main(String args[])
Student4 s1 = new Student4(100,"Ram");
Student4 s2 = new Student4();
s2.id=s1.id;
s2.name=s1.name;
s1.show();
s2.show();