Statements in JAVA
Decision Making & Branching
if statement
//A java program to find if statement
import [Link].*;
class iftest
public static void main() throws IOException
int age=25;
if (age>=18)
[Link]("enterd age is major");
Output:
Entered age is major
ifelse statement
/*a java program to find if else statement*/
import [Link].*;
class ifelsetest
public static void main() throws IOException
{
int age=25;
1|P a ge
if(age>=18)
[Link]("entered age is major");
else
[Link]("entered age is minor");
Ouput:
entered age is major
nestedifelse statement
(1)//a java program to find nestedifelse statement
import [Link].*;
class nestedifelse
public static void main() throws IOException
{
double n1=-1.0, n2=4.5, n3=-5.3, largestnumber;
if(n1>=n2)
{
if(n1>=n3)
largestnumber=n1;
else
largestnumber=n3;
2|P a ge
}
else
if(n2>=n3)
largestnumber=n2;
else
largestnumber=n3;
}
[Link]("largest is:"+largestnumber);
Output:
largest is:4.5
(2) //a java program to find nestedifelse statement
import [Link].*;
class nestedifelsetest
public static void main() throws IOException
double n1=-1.0, n2=4.5, n3=-5.3;
if(n1>=n2)
if(n1>=n3)
[Link](n1);
}
else
3|P a ge
{
[Link](n3);
else
if(n2>=n3)
[Link](n2);
else
{
[Link](n3);
}
[Link]("largest is");
Output:
largest is:4.5
elseifladder statement
//a java program to find elseifladder statement
import [Link].*;
class elseifladdertest
public static void main() throws IOException
int number=0;
if(number>0)
4|P a ge
[Link]("number is positive");
else if(number<0)
[Link]("number is negative");
else
[Link]("number is zero");
Output:
number is zero
switch statement
//a java program to find switch statement
import [Link].*;
class switchtest
public static void main() throws IOException
{
int a=4;
switch (a)
case 1:
[Link]("sunday");
break;
case 2:
[Link]("monday");
5|P a ge
break;
case 3:
[Link]("tuesday");
break;
case 4:
[Link]("wednesday");
break;
case 5:
[Link]("thursday");
break;
case 6:
[Link]("friday");
break;
case 7:
[Link]("saturday");
break;
default:
[Link]("invalid input");
break;
Output:
Wednesday
Looping
for statement
//a java program to find for loop statement
import [Link].*;
6|P a ge
class fortest
public static void main() throws IOException
int i=1, f=1, n=5;
for(i=1;i<=n;i++)
f=f*i;
[Link]("the factorial value"+f);
Output:
the factorial value120
while statement
//a java prgram to find whileloop statement
import [Link].*;
class whiletest
{
public static void main() throws IOException
{
int i;
i=1;
while(i<=10)
[Link](i);
i++;
7|P a ge
Output:
1
10
dowhile statement
//a java program to find dowhileloop statement
import [Link].*;
class dowhiletest
public static void main() throws IOException
{
int i;
i=1;
do
{
[Link](i);
i++;
while(i<=10);
Output:
1
8|P a ge
2
10
break statement
//a java program to find break statement
import [Link].*;
class breaktest
public static void main() throws IOException
int i=10;
for(i=1;i<=10;i++)
{
if(i==5)
break;
[Link](i);
Output:
1
9|P a ge
3
continue statement
//a java program to find break statement
import [Link].*;
class continuetest
public static void main() throws IOException
int n=10;
for(int i=1;i<=10;i++)
if(i==5)
continue;
[Link](i);
}
}
Output:
1
10
10 | P a g e
Classes, Objects & Methods
Creating an object
import [Link].*;
class salestaxcalculator
float amount=100.0f;
float taxrate=10.2f;
public static void main()throws IOException
salestaxcalculator object1=new salestaxcalculator();
[Link]("Amount in object1:"+[Link]) ;
[Link]("Taxrate in object1:"+[Link]);
salestaxcalculator object2=new salestaxcalculator();
[Link]("Amount in object2:"+[Link]);
[Link]("Taxrate in object2:"+[Link]);
Output:
Amount in object1=100.0
Taxrate in object1=10.2
Amount in object2=100.0
Taxrate in object2=10.2
Constructor
Default constructor
//a java program to find default constructors
import [Link].*;
class Rectangle
11 | P a g e
{
int length,width;
Rectangle(int x,int y) //defining constructor
length=x;
width=y;
int rectArea()
return(length*width);
}
class RectangleArea
public static void main() throws IOException
{
Rectangle rect1=new Rectangle(15,10); //calling constructor
int area1=[Link]();
[Link]("Area1="+area1);
Output:
Enter x:15
Enter y:10
rectArea=150
parameterized constructor
//a java program to find parameterised constructor
import [Link].*;
class sum
12 | P a g e
int length,breadth;
sum(int x,int y)
length=x;
breadth=y;
int area()
return (length*breadth);
class summation
{
public static void main(String args[])
{
sum sum1 =new sum(10,20);
sum sum2 =new sum(30,20);
int z1= [Link]();
int z2= [Link]();
[Link]("sum1="+z1);
[Link]("sum2="+z2);
}
}
Output:
Enter x:10
Enter y:30
area:300
short program for constructor
//a java program to find constructors
import [Link].*;
13 | P a g e
class Rectangle1
int length;
int width;
Rectangle1(int x,int y)
length=x;
width=y;
int rectArea()
return(length*width);
}
Output:
Enter x:2
Enter y:4
rectArea:8
Method overloading:
By changing datatype of arguments
//a java program to find method overloadng by changing datatype of arguments
import [Link].*;
class calculate
void sum(int a,int b)
[Link]("sum is:"+(a+b));
}
void sum(float a, float b)
14 | P a g e
{
[Link]("sum is:"+(a+b));
public static void main() throws IOException
calculate cal =new calculate();
[Link](8,5);
[Link](4.6f,3.8f);
Output:
sum is:13
sum is:8.4
By changing no. of arguments
//a java program to find method overloading by changing [Link] arguments
import [Link].*;
class load
void volume(int x)
int volume=x*x*x;
[Link]("volume of cube is:"+volume);
void volume1(int r,int y)
double volume1=3.14*r*r*y;
[Link]("volume of cylinder is:"+volume1);
void volume2(int p,int q,int r)
15 | P a g e
int volume2=p*q*r;
[Link]("volume of cuboid is:"+volume2);
public static void main() throws IOException
load object=new load();
[Link](10);
object.volume1(2,10);
object.volume2(10,10,20);
Output:
volume of cube is:1000
volume of cylinder is:125.60000000000001
volume of cuboid is:2000
Short program for method overloading
//a java program to find method overloading
import [Link].*;
class Room
float length;
float breadth;
Room(float x,float y)
length=x;
breadth=y;
Room(float x)
length=breadth=x;
16 | P a g e
}
float area()
return (length * breadth);
Output:
Enter x=y=15
area:225
Nesting of methods
//a java program to find nesting of methods
import [Link].*;
class Nesting
{
int m,n;
Nesting(int x,int y)
m=x;
n=y;
int largest()
{
if(m>=n)
return(m);
else
return(n);
void display()
int large=largest();
17 | P a g e
[Link]("Largest value="+large);
class NestingTest
public static void main(String args[])
Nesting nest=new Nesting(50,40);
[Link]();
Output:
Largest value=50
Inheritace
Method overriding
//a java program to find method overriding
import [Link].*;
class Super
{
int x;
Super(int x)
this.x=x;
void display() //method defined
[Link]("Super x="+x);
18 | P a g e
}
class Sub extends Super
int y;
Sub(int x, int y)
super(x);
this.y=y;
void display() //method defined again
{
[Link]("Super x=" +x);
[Link]("Sub y=" +y);
}
}
class OverrideTest
public static void main(String args[])
Sub sl= new Sub(100,200);
[Link]();
}
Output:
Super x=100
Sub y=200
Final
final variable
19 | P a g e
//a jva program to find final variable
import [Link].*;
class finalvariable
public static void main() throws IOException
final int hours=24;
[Link]("Hours in 6 days="+hours*6);
Output:
Hours in 6 days=144
final class
//a java program to find final class
import [Link].*;
class finalclass
int x,y;
class sum extends finalclass
int z;
class FinalclassDemo
public static void main(String args[])
sum sum1= new sum();
sum1.z=10;
sum1.x=5;
20 | P a g e
sum1.y=5;
[Link]("X ="+sum1.x);
[Link]("Y ="+sum1.y);
[Link]("Z ="+sum1.z);
Output:
X=5
Y=5
Z=10
final method
//a java program to find final method
import [Link].*;
class finalmethod
final void m1()
[Link]("This is a final method.");
class FinalmethodDemo extends finalmethod
public static void main(String args[]) //void m1()
// COMPILE-ERROR! Can't override.
[Link]("Illegal!");
Output:
21 | P a g e
This is a final method
Illegal!
Abstract
abstract method
//a java program to find abstract method
import [Link].*;
//abstract parent class
abstract class Animal
//abstract method
public abstract void sound();
//Dog class extends Animal class
public class Dog extends Animal
public void sound()
[Link]("Woof");
public static void main(String args[])
{
Animal obj = new Dog();
[Link]();
abstract class
//a java program to find abstract class
import [Link].*;
abstract class square
22 | P a g e
{
abstract void draw();
class rectangle extends square
void draw()
[Link]("Draw clearly");
public static void main(String args[])
square fig= new rectangle();
[Link]();
Arrays, Strings, and Vectors
Array
1-D array
(1)first type
//a java program to find one dimensional array
import [Link].*;
class array
{
public static void main() throws IOException
int n=10;
int i=1;
for(i=1; i<=n; i++)
23 | P a g e
{
[Link](+i);
Output:
1
10
(2)second type
//a java program to find one dimensional array
import [Link].*;
class onedimensionalarray
public static void main() throws IOException
int[] age = new int[5];
for(int i=0;i<5;i++)
[Link](age[i]);
Output:
24 | P a g e
0
(3)third type
//a java program to find one dimensional array
import [Link].*;
class onedarray
public static void main() throws IOException
{
int[] age={12,4,5,2,5};
for(int i=0;i<5;i++)
{
[Link]("Element at index"+i+":"+age[i]);
Output:
Element at index0:12
Element at index1:4
Element at index2:5
Element at index3:2
Element at index4:5
2-d array
(1)first type
//a java program to find twodimensionalarray
import [Link].*;
class twodimensionalarray
25 | P a g e
{
public static void main() throws IOException
int[][] arr ={{10,20,30},{40,50,60},{70,80,90},{11,21,31}};
[Link](" ");
int i,j;
for(i=0;i<=4;i++)
for(j=0;j<=3;j++)
[Link](" "+arr[i][j]);
}
[Link](" ");
}
}
Output:
10 20 30
40 50 60
70 80 90
11 21 31
(2) second type
//a java program to find twodimensionalarray
import [Link].*;
class twodarray
public static void main() throws IOException
//Array of size 4*3 to hold integers
int[][] values ={{10,20,30},{40,50,60},{70,80,90},{100,200,300}};
[Link](" ");
26 | P a g e
//Nested loops to print the array in tabular form
for(int row=0;row<=4;row++)
for(int col=0;col<=3;col++)
[Link](" "+values[row][col]);
[Link](" "); //print new line
Output:
10 20 30
40 50 60
70 80 90
100 200 300
Strings
//the use of == for string comparison in java
import [Link].*;
class equalstrings
{
public static void main() throws IOException
String s1="computers";
String s2="computers";
[Link](s1+"=="+s2+":"+(s1==s2));
27 | P a g e
Output:
computers==computers:true
vector
(1)vector for add method
// Java code illustrating add() method
import [Link].*;
class Vector_Demo
{
public static void main(String args[])
// create default vector
Vector v = new Vector();
[Link](1);
[Link](2);
[Link]("java");
[Link]("programming language");
[Link](3);
[Link]("Vector is " + v);
}
Output:
1 2 java programming language 3
(2)working with vectors and arrays
import [Link].*;
class LanguageVector
{
public static void main(String args[])
Vector list = new Vector();
int length = [Link];
28 | P a g e
for(int i=0;i<length;i++)
[Link](args[i]);
[Link]("COBOL",2);
int size=[Link]();
String listArray[]=new String[size];
[Link](listArray);
[Link]("List of Languages");
for(int i=0;i<size;i++)
[Link](listArray[i]);
}
Command line input and output are:
C:JAVA\prog>java LanguageVector Ada BASIC C++ FORTAN Java
List of Languagges
Ada
BASIC
COBOL
C++
FORTAN
Java
Wrapper classes
(1)Converting String Objects to Numeric Objects
//a java program for wrapper classes
import [Link].*;
public class WrapperExample
29 | P a g e
public static void main(String args[])
int a=20;
Integer i= [Link](a);
Integer j=a;
[Link](a+" "+i+" "+j);
(2)Converting Primitive Numbers to Object Number using Constructor Methods
//a java program for wrapper class
import [Link].*;
public class JavaExample
{
public static void main(String args[])
{
//Converting int primitive into Integer object
int num=100;
Integer obj=[Link](num);
[Link](num+ " "+ obj);
Interfaces: Multiple Inheritance
Interfaces
Implementing Interfaces
//a java program to find interface area
import [Link].*;
interface Area //Interface defined
30 | P a g e
final static float pi=3.14f;
float compute(float x,float y);
class Rectangle implements Area //Interface implemented
public float compute (float x,float y)
return(x*y);
class Circle implements Area //Another implementation
{
public float compute(float x,float y)
return(pi*x*x);
}
class InterfaceTest
public static void main(String args[])
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area; //Interface object
area=rect; //Area refers to rect object
[Link]("Area of Rectangle=" +[Link](10,20));
area=cir;
[Link]("Area of Circle=" +[Link](10,0));
Output:
31 | P a g e
Area of Rectangle=200
Area of Circle=314
Implementing multiple inheritance
//a java program to find interface program
import [Link].*;
interface Area
float compute(float x,float y);
class Rectangle implements Area
public float compute(float x,float y)
return(x*y);
class Triangle implements Area
public float compute(float x,float y)
return(x*y/2);
}
}
class InterfaceArea
public static void main() throws IOException
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
32 | P a g e
area=rect;
[Link]("Area Of Rectangle="+[Link](1,2));
area=tri;
[Link]("Area Of Triangle="+[Link](10,2));
Output:
Area of Rectangle=2
Area of Triangle=10
Implementing multiple inheritance
//a java program for implementing multiple inheritance
import [Link].*;
class Student
int rollNumber;
void getNumber(int n)
rollNumber=n;
void putNumber()
[Link]("Roll no:"+rollNumber);
class Test extends Student
float part1,part2;
void getMarks(float m1,float m2)
part1=m1;
33 | P a g e
part2=m2;
void putMarks()
[Link]("Marks obtained");
[Link]("Part1="+part1);
[Link]("Part2="+part2);
interface Sports
float sportWt=6.0F;
void putwt();
class Results extends Test implements Sports
{
float total;
public void putWt()
[Link]("Sports Wt ="+sportWt);
void display()
{
total=part1+part2+sportWt;
putNumber();
putMarks();
putWt();
[Link]("Total score="+total);
class Hybrid
34 | P a g e
{
public static void main(String args[])
Results student1=new Results();
[Link](1234);
[Link](27.5F,33.0F);
[Link]();
Output:
Roll No:1234
Marks Obtained
Part1=27.5
Part2=33
Sports Wt=6
Total score=66.5
35 | P a g e
36 | P a g e