Methods In Java
OCJP help
OCJP ( oracle certified java programmer ) is the foundation to java programmers. in this site we are going to help you...........
Monday, November 10, 2014
Wednesday, November 5, 2014
Methods
There are two stages in methods
1.
Declaring the methods.
2.
Calling the methods.
Declaring
the methods.
·
In this stage we give a name to the method and it’s
return[D1]
type. We can give any name to a method. But in the run time pc only execute
the codes in the main method.
Example:
class cal{
void
add(){
int
x=2;
int
y=3;
System.out.println
(“sum = ” + (x+y ));
}
void
sub(){
int
x=2;
int
y=3;
System.out.println
(“sub = ” + (x-y ));
}
void
mul(){
int x=2;
int y=3;
System.out.println (“mul = ”
+ (x*y ));
}
void
div(){
int x=2;
int y=3;
System.out.println (“div = ”
+ (x/y ));
}
}
|
In the above example there is no main method. Therefore the output should be empty.
| output |
so we should call them in to the main method.
Ex :-
Entity: person
Properties: name, age, date of birth, weight
Behaviours: run, jump, talk
1. Real
world entity can be represented by a java class.
2. The
properties of the entity will be map to variable.
3. The
behaviours of the entity will be map to methods.
class person {
int age= 23;
String name = “Kamal”;
Int height = 125;
int weight = 90;
void run(){
//////////////
/////////////////
}
void run(){
//////////////
/////////////////
}
}
|
Object orient programming concept.
Object/entity : -
·
Object In the real world can be represented from
a java class.
Properties /atribures : -
·
Properties In the entity are map to global variable
in the java class.
The behaviours : -
·
The behaviours in the entity are map to methods
in the java class.
Formore about Object orient programming concept.
|
To call
a method / variable in a class an object/instant must be created of the given
class.
Calculator c1= new Calculator ();
Calculator :
reference data type ( Hold the reference memory address of the calculator
object)
c1 :
variable name
new Calculator (); : create the calculator object
·
multiple classes can be declared with different
with different names.
·
An object/instant can be created out of each
declared class.
·
Therefore each declared class type can become a reference
data
type.
Method
parameters
The compiler identifies / differentiate a method using the
method signature.
class Calculator{
void add ( int x , int
y ){
System.out.println();
}
public static void main(String
args[]){
Calculator
c1= new Calculator();
c.add( 2,3);
c.add(-2,10);
}
}
|
In the line void add ( int x , int
y ){ ,
the method parameters are int x , int y .
c.add( 2,3);
c.add(-2,10);
2,3
; -2,10 are method
arguments .
Method overloading.
Declaring multiple methods in a
class with the same method name but with different method parameters.
class Calculator{
void add ( int x , int y ){
System.out.println();
}
void add ( double x , double y ){
System.out.println();
}
public static void main(String
args[]){
Calculator
c1= new Calculator();
c.add( 2,3);
}
}
|
Method
return types.
class Test{
void m(){ -----------------------------------Ã no value will
be return.
int x;
x=10;
int y =3;
System.out.println(“Total = ”+ (x+y) );
}
int d(){ -----------------------------Ã int value will
be return.
int x;
x=10;
int y =3;
System.out.println(“Total = ”+ (x+y) );
Return ( x+y ); ->>>>>>>>
return statement
}
public static void main (String args []){
Test t= new Test();
t.m();
int x = t.d();
System.out.println ( x );
}
}
|
Monday, November 3, 2014
There are two stages in a Variable.
1.
Declaring the variable.
2.
Initializing the variable.
Declaring the variable.
·
Introduce compiler about the variable type.
Initializing the variable.
·
Give a value to variable.
class Test{
public static void
main (String args
[]){
int x; // Declaring the variable.
x=10; // Initializing
the variable.
System.out.println(x);
}
}
|
Ex 1:
java programme to print the value of x and y.
class Test{
public static void
main (String args
[]){
int x;
x=10;
int y =3;
System.out.println(“x
= ”+ x+“, y = ”+y);
}
}
|
Ex 1:
java programme to print the sum of x and y.`
class Test{
public static void
main (String args
[]){
int x;
x=10;
int y =3;
System.out.println(“x
= ”+ x+“, y = ”+y);
System.out.println(“Total
= ”+ (x+y)
);
}
}
|
Data types.
Primitive Data types.
byte
|
8 bits
|
Default data type for whole numbers.
|
short
|
16 bits
|
|
int
|
32 bits
|
|
long
|
64 bits
|
|
float
|
32 bits
|
Default data type for decimals.
|
double
|
64 bits
|
|
char
|
16 bits
|
|
boolean
|
1 bits
|
Subscribe to:
Comments (Atom)
