Professional Documents
Culture Documents
1 2
JAVA UNIT-2 JAVA UNIT-2
else - - if Ladder }
A common programming construct that is based upon a sequence conditions is the else-if else{
The if statements are executed from the top down. As soon as one of the conditions is true, dispatch execution to different parts of your code based on the value of an expression.
the statement associated with that if is executed, and the rest of the ladder is bypassed. If Syn:
none of the conditions is true, then the final else statement will be executed. The final else switch (expression)
acts as optional. If there is no final else and all other conditions are false, then no action will {
n3=sc.nextInt(); default:
3 4
JAVA UNIT-2 JAVA UNIT-2
The switch statement works like this: The value of the expression is compared with The exp1 is evaluated first. If the result is true, exp2 is evaluated and returned as the value of
each of the “ case “ values. If a match is found, the statements following that case are the conditional expression. Otherwise exp3 is evaluated and its value is returned. When the
executed. If none of the case values are not matched, then the optional default statement is conditional operator is used, the code becomes simpler, efficient. However the readability is
executed. The break statement is used inside the switch to terminate a statement sequence. poor.
Example: Example:
class ColorDemo{ import java .util.*;
public static void main(String args[])throws Exception{ class cond_ex{
char color ; public static void main(String args[]){
System.out.print("Enter a charecter : "); int x, y,big;
color=(char)System.in.read(); Scanner sc = new Scanner (System.in);
switch (color){ System.out.println("Enter two nos :");
case 'r': x=sc.nextInt();
System.out.println ("red"); break; y=sc.nextInt();
case 'g': big=(x>y) ? x: y;
System.out.println ("green"); break; System.out.println("Biggest no : "+big);
case 'b': }
System.out.println ("blue"); break; }
case 'y': Write a short note on while loop?
System.out.println ("yellow"); break;
The while loop is Java’s most fundamental looping statement. It repeats a statement or block
case 'w':
while its controlling expression is true. Here is its
System.out.println ("white"); break;
general form:
default:
Syn:
System.out.println ("No Color Selected");
while(condition)
}
{
}
// body of loop
}
}
Explain Conditional Operator?
Conditional Operator (? : ):
It is a ternary operator, useful for making two way decisions. This operator is a combination It is an entry control loop, i.e. the condition is checked at the time of entry point of the
of ? and : and takes three operands. The general form of this operator is loop body. First it evaluates the loop condition, if it is true then the loop body will
executes. After executing the loop body the control again transfers to the condition.
exp1 ? exp2: exp3; This process will continue until the condition will become false. Once the condition is
false then the control transfers to out of loop.
5 6
JAVA UNIT-2 JAVA UNIT-2
Example: Like initialization the increment part may also have more than one expression and
import java .util.*; separated by comma.
class series{ for(n=1, m=20 ; n<=m ; n=n+1, m=m-2)
public static void main(String args[]){ One or more sections can be omitted from the for statement. However, the semicolons
int n,i=1; separating the sections must remain.
Scanner sc = new Scanner(System.in); for( ; m<=10 ; )
System.out.print("Enter a no :");
n=sc.nextInt(); Initialization
while(i<=n){
System.out.print(i+" ");
i++;
} False Cond True Loop Body Increment
}
}
7 8
JAVA UNIT-2 JAVA UNIT-2
execution. In this the loop body will execute minimum one time. If the condition is true , the While loop is an entry controlled loop.
program continues to evaluate the body of the loop once again. This process continues as It is generally used for implementing common looping situations.
long as the condition is true. When the condition is false, the loop will be terminated, and the It executes if the condition is true.
control goes to the statement that appears immediately after the while statement. do .. while :
It is an exit controlled loop.
It is typically used for implementing menu-based programs where the menu required
Loop Body do
to be printed at least once.
{
The loop will execute minimum one time.
// Loop Body;
Exaple: 2. break can be used inside the switch block to come out of the switch block.
import java.util.*; 3. break can be used in nested blocks to go to the end of a block. Nested blocks
9 10
JAVA UNIT-2 JAVA UNIT-2
11 12
JAVA UNIT-2 JAVA UNIT-2
The new operator dynamically allocates memory for an object and returns a reference Explain method overloading?
to it.
In java it is possible to create methods that have the same name, but different
parameters lists and different definitions. This is called method overloading
Overloaded methods must differ in the type and/or number of their parameters.
When an overloaded method is called, Java looks for a match between the arguments
used to call the method and the method’s parameters. However, this match need not
always be exact.
When you overload a method, each version of that method can perform any activity
Explain how to access class members with an example? you desire. There is no rule stating that overloaded methods must relate to one
Every class type object will contain its own copy of the instance variables. To access another.
these variables, you will use the dot (.) operator. The dot operator links the name of Example:
the object with the name of an instance variable. class rectangle {
r1 . length=10 int length, width;
Example: void read( ){
class rectangle{ length=10;
int length, width; width=5;
void getdata(int x, int y){ }
length=x; void read(int x){
width=y; length=width=x;
} }
int area( ){ void read(int x, int y){
int a; length=x;
a=length*width; width=y;
return(a); }
}} int area( ) {
class shape { return(length*width);
public static void main ( String args[ ] ){ }
rectangle r = new rectangle( ); }
r.getdata(10,5); class overload_demo{
System.out.println("area of the rectangle is "+r.area( )); public static void main( String args[ ] ){
}} rectangle r1 = new rectangle();
rectangle r2= new rectangle();
r1.read(15,5);
13 14
JAVA UNIT-2 JAVA UNIT-2
What is a constructor and explain its properties with an example? What is a constructor explain its types with examples?
Constructor: Constructor:
Constructor is a member method used to construct the object. It has the same name as Constructor is a member method used to construct the object. It has the same name as
the class in which it resides and is syntactically similar to a method. Once defined, the the class in which it resides and is syntactically similar to a method. Once defined, the
constructor is automatically called immediately after the object is created, before the constructor is automatically called immediately after the object is created, before the
new operator completes. new operator completes.
Properties: We can define the following types of constructors
Constructors can’t return any value not even void also. The implicit return type of a Default constructor
class’ constructor is the class type itself. Parameterized constructor
The constructor must be public. Overloading of constructors
If you won’t create your constructor the JVM creates a constructor and is called Cloning of constructors.
default constructor. Once you define your own constructor, the default constructor is
no longer used.
Default constructor:
Apart from the object creation we can use the constructor method to initialize the
When we create a constructor with out any parameters then it is known as default
object.
constructor. We can instantiate the object normally.
Like other methods the constructor method also parameterized and overloaded.
Parameterized constructor:
Example: We can define a constructor which takes parameters as the normal method can.
When we are using a parameterized constructor to construct the object we need to
class rectangle{
int length, width; supply the required no , type of parameters at the time of instantiation, the constructor
is implicitly invoked by passing certain no of parameters.
public rectangle( ){ //constructor method
Example:
length=10;
class rectangle {
width=5;
} int length, width;
public rectangle (int x, int y){ // parameterized constructor
int area( ){
length=x;
return(length*width);
width=y;
}}
class constructor_demo{ }
15 16
JAVA UNIT-2 JAVA UNIT-2
17 18
JAVA UNIT-2 JAVA UNIT-2
class clone { }
public static void main ( String args[ ] ) { static void callme() {
rectangle r1 = new rectangle(10,5); System.out.println("a = " + a);
rectangle r2= new rectangle(r1); }}
System.out.println("Area of first recangle : "+r1.area()); class stat2 {
System.out.println("Area of Third recangle : "+r2.area()); public static void main ( String args[ ] ) {
}} StaticDemo.callme( );
Explain about static members of the class? System.out.println ("b = " + StaticDemo.b);
These are the members which resides in the class memory area and can be accessed }}
by all objects globally. We do this by declaring a member as static. What is access specifier ? Explain deferent Access Specifiers supported by java?:
When a member is declared static, it can be accessed before any objects of its class An access specifier is a key word that represents how to access a member of a class.
are created, and without reference to any object. There are four access specifiers in java.
static int count; only one accessibility modifier can be specified for a member
When a class is instantiated only one copy of static data members are created in the member is accessible from anywhere, both in the package containing its class and in
class memory. other packages where this class is visible. This is true for both instance and static
We can initialize the static variables by using static block, which gets executed members.
19 20
JAVA UNIT-2
Specification Members
Public Accessible everywhere
Protected Accessible by any class in the same package as its class, and
accessible only by subclasses of its class in other packages.
default (no modifier) Only accessible by classes, including subclasses, in the same
package as its class (package accessibility).
Private Only accessible in its own class and not anywhere else
21