JAVA 2.0 Notes
2. PROGRAM CONSTRUCTS
They are divided into
Sequence
Selection - if, switch
Iteration - while, do..while, for
Misc - break, exit, continue, and return
a) If
If (condition)
{
set of statements1;
}
else
{
set of statments2 ;
}
Example 3:
class if
{
public static void main(String a[ ])
{
int a=5,b=10;
if (a>b)
System.out.println("A is big");
else
System.out.println("B is big");
}
}
b) for
for (var=value1; condition; iteration)
{
statements;
}
Example 4:
class for
{
public static void main(String a[ ])
{
for(int a=0; a<10;>c) while and do..while
while(condition) do
{ {
statements; statements;
} } while(condition);
Example 5:
class dowhile
{
public static void main(String a[ ])
{
int a=5 ;
do
{System.out.println("A is big");
a--;
} while(a>0);
}
}
d) switch
switch (expr)
{
case value1:
statements1 ; break;
case value2:
statements2 ; break;
:
:
case valuen:
statementsn; break;
default:
statements ;
}
Example 6:
class switch
{
public static void main(String a[ ])
{
int a=5 ;
switch(a)
{
case 0:
System.out.println("Zero"); break;case 1:
System.out.println("One"); break;case 2:
System.out.println("Two"); break;default:
System.out.println("greater than 2");}
}
}
e) break, continue and exit
break will transfer the control out of the loop, in continue , the rest of the lines are ignored and loop continue after the next iteration. The exit function will stop the program execution.
3. CLASSES AND METHODS
Classes
The most important feature of the OOP is classes. A class defines the shape and behavior of an object and is a template for multiple object with similar features. It is a logical construct upon which the entire java language is built because it defines the shape and nature of the object.
To create a class, a source file with the class keyword in it, followed by a name and pair of curly braces for the body.
access class classname
{
type instance-variable;
type method(parameters)
{
body of method;
}
}
Once class is created, an instance of class is created by new keyword. The instance of class is used to access the variables and methods that form part of the class. The dot operator is used to get the value of the class variable (object.variable).
Instance Variable : Data is encapsulated in a class by declaring varables inside the class
declaration. Variables declared in this scope are called as Instance variables.
Class Variable : Class variables are global to class and all the instances of the class. To declare class variable static keyword is used.
The access of class may be public, private, abstract or final.
Methods
Methods are functions that operate on instances of classes in which they are defined. Method definition has four parts. They are name of the method, return type , list of parameters and body of the method.
returntype methodname (type arg1, type arg2,…)
{
Statements;
}
To call a method
methodname( ) or
obj.methodname(para1, para2,…) ;
An example for a class which has a class and method
Example 7:
class area
{
int len=10, bra=20;
void calc( )
{
int ara = len * bra;
System.out.println("The area is " + ara);
}
public static void main(String a[ ])
{
area a = new area( );
a.calc( );
}
}
Access Specifiers:
Public : If any method or variable is declared as public, it is visible to all
classes.Private : If any method or variable is declared as private , it is visible to
classes in which it is declared.Protected : It is visible in class and all its subclasses.
Package : It is indicated by the lack of any access modifier in a declaration.
It has an increased protection and narrowed visibility.Final : It can not overridden by subclass
Abstract : in abstract class without body, must be overridden by subclass.
Static : It is automatically called when creating instance of class.
This:
The this keyword is used inside any instance method to the current object.
Constructor
A constructor method is a special kind of method that determines how an object is initialized when created. They have the same name as the class but do not have any return type. Consturctor can also be overloaded.
Example 8:
class cons
{
int I; int j;
cons(int a, int b)
{ I=a; j=b; }
void print( )
{System.out.println("the addition of " + I + "and " + j + " is " +(I+j)); }public static void main(String s[ ] )
{
cons c = new cons(10,10);
c.print( );
}
}
No comments:
Post a Comment