You are on page 1of 15

Chapter 5

Introduction to Classes and


Methods
General form of a class
A class may contains 3 types of items:
Variables
Methods
Constructor
Format : class clasName
{
//instance variable declaration
type1 var1 =value1;
type2 var2=value2;
// constructor
clasName(cparams1)
{
//body of constructor
}
// Methods
rtype1 mthName1 (mparams1)
{
//body of methods
}
}
Creation of a simple class
class Sample
{
int a;
int b;
int c;
}
class DemoClass
{
public static void main (String args[ ])
{
Sample s = new Sample();
s.a=10;
s.b=20;
s.c=30;
System.out.println(s.a);
System.out.println(s.b);
System.out.println(s.c);
}
}
Constructors
constructor is a special method that creates and initializes an object of
particular class
It has the same name as that of class
Constructor may accept arguments
Constructor returns reference to the object that it creates
Default constructor has no arguments
It is not called directly it is invoked via the new operator
Constructor example
class Sample
{
int a;
int b;
int c;
Sample(int ax,int ay,int az)
{
a=ax;
b=ay;
c=az;
}
}
class DemoClass
{
public static void main (String args[ ])
{
Sample s = new Sample(300,340,400);
System.out.println(s.a);
System.out.println(s.b);
System.out.println(s.c);
}
}
Arithmetic Operator

Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
++ Increment
+= Addition Assignment
-= subtraction Assignment
*= multiply Assignment
/= division Assignment
%= modulus Assignment
Bitwise Operator

Operator Description
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise EX-OR
>> Shift Right
>>> Shift Right zero fill
<< Shift Left
&= Bitwise AND assign
|= Bitwise OR assign
^= Bitwise EX-OR assign
>>= Shift Right Assignent
Bitwise Operator

Operator Description
>>>= Shift Right zero fill
assignment
<<= Shift left assignment
Bitwise Operator
Class BitwiseOper
{
public static void main(String args[])
{
char c=‘A’; byte b= 100; short s= 100;
int i=-100; long lo= 100;
System.out.println(c & oxf);
System.out.println(b | 1);
System.out.println( s ^ 1);
System.out.println(~i);
System.out.println(lo | 1);
System.out.println(I >> 2);
System.out.println(s >>> 2);
System.out.println(I << 2);
}
}
Relational Operator

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Control Statements
if statement: It is one of Java’s selection statement or
conditional statement. It's operation is governed by the
outcome of a conditional test that evaluates to either
true/false.
Syntax: if(expr) statement;

if-else statement: else statement will be executed


when condition of if expression is false.
Syntax: if(exprn)
statement 1;
else
statement 2;
Control Statements
for statement: It is one of Java’s looping statement.
It allows one or more statements to be repeated.
Syntax: for (initialize; test; incrememt/decrement)
statements;

while statement: Statements after while are executed


only when condition is true.

Syntax: while (condition)


statement 1;
Control Statements
break statement:
break statement allows programmer to exit a loop from any point within its body,
bypassing its normal termination expression.
when break statement is encountered inside the loop ; loop is immediately terminated
and program control resumes at next statement following the loop.

Example:
class Breakdemo
{
public static void main(String args[ ])
{
int i;
for(i=1;i<100;i++)
{
System.out.println(i);
if(i==10) break;
}
}
}
Control Statements
continue statement:
Continue statement is somewhat apposite to break statement .It forces the
next iteration of the loop to take place.

Example:
class ContinueDemo
{
public static void main(String args[ ])
{
int i;
for(i=1;i<100;i++)
{
if(i < 95 ) continue;
System.out.println(i);

}
}
}
Control Statements
switch statement:
switch statement is a multiway branching statement. It tests the value of a
variable against a list of case values and according to values a block of
statements will be executed
3. The expression must be of type byte,short,int,char.
4. Break statement is optional,else execution will continue on into next
case.

Syntax:
switch (expression)
{
case value1: statements;
break;
case value 2 : statements;
break;
|
|
case value n: statements;
break;
default : statements;
}

You might also like