0% found this document useful (0 votes)
422 views41 pages

Jump Statements

Java has three types of jump statements: break, continue, and return. The break statement terminates a loop or switch statement. The continue statement skips to the next iteration of a loop. The return statement exits the current method. A class defines the data and methods that represent an object. Constructors are special methods that are called when an object is created to initialize it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
422 views41 pages

Jump Statements

Java has three types of jump statements: break, continue, and return. The break statement terminates a loop or switch statement. The continue statement skips to the next iteration of a loop. The return statement exits the current method. A class defines the data and methods that represent an object. Constructors are special methods that are called when an object is created to initialize it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

m Java does not offer a "go to" type of statement as

in some older languages


m There are three types of Jump statements in Java:
m the break,
m the continue,
m and the return.
m @t has 3 uses
M @t terminates a statement sequence in a switch statement
M @t can be used to exit a loop
M @t can be used as a ³civilized form of goto´
m Ry using break, you can force immediate
termination of a loop, bypassing the
conditional expression and any remaining code
in the body of the loop.
m îhen a break statement is encountered inside a
loop, the loop is terminated and program control
resumes at the next statement following the loop
class RreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
m The break statement can be used with any of Java's loops,
including intentionally infinite
loops.
class RreakLoop2 {
public static void main(String args[]) {
int i = 0;
while(i < 100) {
if(i == 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
m The break statement can also be employed by itself
to provide a "civilized" form of the goto statement
m The general form of the labeled break statement is
shown here:
break à à
m £ere, à à 
 à à    




m 
 
 executes, control is
transferred out of the named block of code.
m The labeled block of code must enclose the break
statement, but it does not need to be the immediately
enclosing block.
// Using break as a civilized form of goto.
class Rreak {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Refore the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
m The continue statement is used inside the loop
control block
m îhen the continue statement is executed , the
control skips the remaining portion of the loop and
goes to the beginning of the loop and continue.
Glass continueDemo
{
public static void main(String args[])
{
@nt i=0;
while(++i<10)
{
if(i%2==0)
continue;
System.out.println(i);
}
}
}
m The return statement is used to explicitly
return from a method.
m That is, it causes program control to transfer back
to the caller of the method.
m At any time in a method the return statement can
be used to cause execution to branch back to
the caller of the method.
m Thus, the return statement immediately
terminates the method in which it is executed.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Refore the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
m £ere, return causes execution to return to the Java
run-time system, since it is the run-time system that
calls main( ).
m Java allows multiple initialization and iteration in
for statement
m Each such statements are to be separated by a
comma(,)
m Rut there can be only one conditional expression
class GommaDemo
{
public stateic void main(String args[])
{
int n,i,sum=0;
for(i=1,n=5;n>0;i++,n--)
{
sum=i+n;
System.out.println(i+´+´+n+´=³+sum);
}
}
}
m A class is a blueprint for objects
m @t defines a type of object according to the data the
object can hold and the operations the object can
perform
m Glasses are basic building blocks for object
oriented programming in java
m A class defines the data, how it can be accessed
and the method that manipulates the data
class à   
    à
    à
// ...
    à
 
   à
// body of method
}
 
   à
// body of method
}
// ...
 
   à
// body of method
}
}
m The data, or variables, defined within a class are
called ›   › à
m The code is contained within 

m Gollectively, the methods and variables defined
within a class are called „„ à 
m åariables defined within a class are called
instance variables because each instance of
the class (that is, each object of the class) contains
its own copy of these variables.
class Rox {
double width;
double height;
double depth;
}
m To actually create a Rox object, you will use a
statement like the following:
m Rox mybox = new Rox();
m After this statement executes, mybox will be an
instance of Rox.
class Rox {
double width;
double height;
double depth;
}
// This class declares an object of type Rox.
class RoxDemo {
public static void main(String args[]) {
Rox mybox = new Rox();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("åolume is " + vol);
}
}
class Rox {
double width;
double height;
double depth;
}
class RoxDemo2 {
public static void main(String args[]) {
Rox mybox1 = new Rox();
Rox mybox2 = new Rox();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("åolume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("åolume is " + vol);
}
m }
m Rox mybox; // declare reference to object
m mybox = new Rox(); // allocate a Rox object
m The first line declares mybox as a reference to
an object of type Rox.
m After this line executes, mybox contains the
value null, which indicates that it does not yet
point to an actual object.
Œeclaring an object of type Rox
m the new operator dynamically allocates
memory for an object.
m It has this general form:

à   à  


m A class creates a new data type that can be used
to create objects. That is, a class creates a logical
framework that defines the relationship between
its members.
m îhen you declare an object of a class, you are
creating an instance of that class. Thus, a class is
a logical construct.
m An object has physical reality.
Rox b1 = new Rox();
Rox b2 = b1;
m b1 and b2 will both refer to the  
  
! 

  ›   àà  
„„copy any part of the original object.
m @t simply makes b2 refer to the same object as
does b1. Thus, any changes made to the
object through b2 will affect the object to
which b1 is referring, since they are the same
object.
"   à
// body of method
}
m £ere,   
  "  

   à #including class types that you create.
@f the method does not return a value, its return type must be
void.
m The name of the method is specified by „ › 
à àidentifier other than those already used by other items
within the current scope.
m The   à $" 
     
   

m Aarameters are essentially variables that receive the value of the
!"   
the method when it is called. @f the method
has no parameters, then the parameter list will be empty.
class Rox {
double width;
double height;
double depth;
// display volume of a box
void volume() {
System.out.print("åolume is ");
System.out.println(width * height * depth);
}
}
class Rox {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
m The type of data returned by a method must be
compatible with the return type specified by the
method. For example, if the return type of some
method is boolean, you could not return an
integer.
m The variable receiving the value returned by a
method (such as vol, in this case) must also be
compatible with the return type specified for the
method.
class Rox {
double width;
double height;
double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
m A
" 
 à% 
    à
"
  

m &    the class in which it
resides and is syntactically similar to a method.
m Once defined, the constructor is automatically
called immediately after the object is created,
before the new operator completes.
m they have no return type, not even void.
m This is because the implicit return type of a class'
constructor is the class type itself.
class Rox {
double width;
double height;
double depth;
// This is the constructor for Rox.
Rox() {
System.out.println("Gonstructing Rox");
width = 10;
height = 10;
depth = 10;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class Rox {
double width;
double height;
double depth;
// This is the constructor for Rox.
Rox(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
m this can be used inside any method to refer to
the  object. That is, this is always a
reference to the object on which the method
was invoked.
m oou can use this anywhere a reference to an
object of the current class' type is permitted.
m it is illegal in Java to declare two local variables with the
same name inside the same or enclosing scopes.
m you can have local variables, including formal parameters
to methods, which overlap with the names of the class'
instance variables.
m £owever, when a local variable has the same name as an
instance variable, the local variable    
  à
m this operator lets you refer directly to the object, you
m can use it to resolve any name space collisions that might
occur between instance variables and local variables
Rox(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
m Java handles deallocation automatically. The
technique that accomplishes this is called !  !

àà 

m &
à'
  
 
 
(# 
  "  to be no longer
needed, and the memory occupied by the object
can be reclaimed.
m no explicit need to destroy objects
m Sometimes an object will need to perform some action
when it is destroyed.
m For example, if an object is holding some non-Java
resource such as a file handle or window character
font, then you might want to make sure these
resources are freed before an object is destroyed.
m To handle such situations, Java provides a
mechanism called  à% 

m )using finalization, you can define specific actions
that will occur when an object is just about to be
reclaimed by the garbage collector.
m To add a finalizer to a class, you simply define the finalize(
) method. The Java run time calls that method whenever
it is about to recycle an object of that class.
m @nside the finalize( ) method you will specify those
actions that must be performed before an object
m is destroyed.
m The garbage collector runs periodically, checking for
objects that are no longer referenced by any running state
or indirectly through other referenced objects.
m Right before an asset is freed, the Java run time calls the
finalize( ) method on the object.

You might also like