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
1|Page
ifelse statement
/*a java program to find if else statement*/
import [Link].*;
class ifelsetest
public static void main() throws IOException
int age=25;
if(age>=18)
[Link]("entered age is major");
}
else
[Link]("entered age is minor");
}
}
Ouput:
entered age is major
2|Page
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;
}
else
if(n2>=n3)
{
3|Page
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)
4|Page
{
[Link](n1);
else
[Link](n3);
}}
else
if(n2>=n3)
[Link](n2);
else
[Link](n3);
}
[Link]("largest is");
}
Output:
largest is:4.5
5|Page
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)
[Link]("number is positive");
else if(number<0)
[Link]("number is negative");
}
else
[Link]("number is zero");
}
6|Page
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");
break;
case 3:
[Link]("tuesday");
break;
case 4:
7|Page
[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
8|Page
Looping
for statement
//a java program to find for loop statement import
[Link].*;
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
9|Page
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++;
Output:
1
2
10 | P a g e
7
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:
11 | P a g e
1
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;
12 | P a g e
[Link](i);
Output:
1
3
4
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;
13 | P a g e
}
[Link](i);
Output:
1
2
3
4
6
10
Classes, Objects & Methods
Creating an object
import [Link].*;
class salestaxcalculator
float amount=100.0f;
float taxrate=10.2f;
14 | P a g e
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
int length,width;
15 | P a g e
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
16 | P a g e
parameterized constructor
//a java program to find parameterised constructor
import [Link].*;
class sum
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]();
17 | P a g e
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].*;
class Rectangle1
{ int
length; int
width;
Rectangle1(int x,int y)
{
length=x;
width=y;
}
int rectArea()
18 | P a g e
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)
[Link]("sum is:"+(a+b));
19 | P a g e
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)
20 | P a g e
double volume1=3.14*r*r*y;
[Link]("volume of cylinder is:"+volume1);
void volume2(int p,int q,int r)
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].*;
21 | P a g e
class Room
float length;
float breadth;
Room(float x,float y)
length=x;
breadth=y;
Room(float x)
length=breadth=x;
float area()
return (length * breadth);
}
Output:
Enter x=y=15
area:225
22 | P a g e
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();
[Link]("Largest value="+large);
}
23 | P a g e
}
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)
24 | P a g e
{
this.x=x;
void display() //method defined
[Link]("Super x="+x);
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
{
25 | P a g e
public static void main(String args[])
Sub sl= new Sub(100,200);
[Link]();
Output:
Super x=100
Sub y=200
Final final
variable
//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);
}
}
26 | P a g e
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;
sum1.y=5;
[Link]("X ="+sum1.x);
27 | P a g e
[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()
28 | P a g e
{
// COMPILE-ERROR! Can't override.
[Link]("Illegal!");
Output:
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()
29 | P a g e
[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
abstract void draw();
class rectangle extends square
void draw()
[Link]("Draw clearly");
}
public static void main(String args[])
30 | P a g e
{
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++)
[Link](+i);
31 | P a g e
}
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++)
32 | P a g e
{
[Link](age[i]);
Output:
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]);
}
33 | P a g e
}
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
{
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++)
34 | P a g e
{
[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](" ");
35 | 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
36 | P a g e
{
String s1="computers";
String s2="computers";
[Link](s1+"=="+s2+":"+(s1==s2));
}
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");
37 | P a g e
[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];
for(int i=0;i<length;i++)
[Link](args[i]);
[Link]("COBOL",2);
int size=[Link]();
String listArray[]=new String[size];
[Link](listArray);
38 | P a g e
[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
39 | P a g e
public static void main(String args[])
int a=20;
Integer i= [Link](a);
Integer j=a;
[Link](a+" "+i+" "+j);
Output:
20 20 20
(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);
40 | P a g e
Output:
100 100 100
Interfaces: Multiple Inheritance
Interfaces
Implementing Interfaces
//a java program to find interface area
import [Link].*;
interface Area //Interface defined
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);
41 | P a g e
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:
Area of Rectangle=200
Area of Circle=314
42 | P a g e
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
43 | P a g e
public static void main() throws IOException
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
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)
44 | P a g e
rollNumber=n;
void putNumber()
[Link]("Roll no:"+rollNumber);
class Test extends Student
float part1,part2;
void getMarks(float m1,float m2)
part1=m1;
part2=m2;
void putMarks()
[Link]("Marks obtained");
[Link]("Part1="+part1);
[Link]("Part2="+part2);
interface Sports
45 | P a g e
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
{
public static void main(String args[])
46 | P a g e
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
47 | P a g e
48 | P a g e