Wednesday, October 5, 2011
There is no call by reference in Java....
The Truth: There is no call by reference in Java.
let's see one example:
class Democall
{int ival1=2,ival2=3;
int fun1 (int a, int b) { a=a*2; b=b*2; return(a+b); }
int fun2 (Democall obj) { obj.ival1=obj.ival1*2; obj.ival2=obj.ival2*2; return(obj.ival1+obj.ival2);}
public static void main(String args[])
{
int i=2,j=3;
Democall obj1=new Democall();//new object creation
int k=obj1.fun1(i,j);//passing i,j values to fun1's a,b
System.out.println(i+" "+j+" "+k);
k=obj1.fun2(obj1);//passing value of object obj1 to the fun2's obj
System.out.println(obj1.ival1+" "+obj1.ival2+" "+k);
}
}
output:
2 3 10
4 6 10
Now you might say: yes it is call by reference as second output is indeed changing the values of object.
But the truth lies in the memory operations of java. The function fun1 is totally fine as it is working with primitive types and indeed its is well known as call by value, but in fun2 the obj is itself passed on to the function fun2, so the value of obj1 has to be copied in to obj. Now just think what could be the value of obj1, will it not be the address of the newly created object? So the address of new Democall() is acting as the value for obj1 which would be passed as value to obj, now if obj accesses data member ival1 and ival2 it would be obviously obj1's ival1 and ival2 which ultimately got reflected in main function.
So it will give you a look and feel of a pass by reference but in the actual working it is call by value.
Wednesday, August 10, 2011
Java is not a Purely Object Oriented Language....
Thursday, December 9, 2010
Important Facts about Java programming..
Important Basic Facts about Java
1. Multiple Inheritance can be achieved with the help of interfaces as they don’t implement any function just give a guidelines for the implementation.
E.g.
interface S{final int a=0;}
interface A extends S{}
interface B extends S{}
interface C extends B,A{
}
class Multiple implements C
{
public static void main(String args[])
{
System.out.println(a);
}
}
Output: 0
2. Parent class always passes its own function to the child with its definition in inheritance. So while re-implementation of any interface already implemented by the parent does not make any compulsion for the child class.
E.g.
interface S{final int a=0; void fun();}
interface A{void fun1();}
interface B extends S,A{void fun2();}
abstract class Abst implements B
{
public void fun(){System.out.println("Abtract");}
}
class last extends Abst implements S
{
public void fun1(){System.out.println("lastfun1");}
public void fun2(){System.out.println("lastfun2");}
}
class MultImp{
public static void main(String args[])
{
last l =new last();
l.fun();l.fun1();l.fun2();
}
}
Output:
Abtract
lastfun1
lastfun2
3. Multiple Inheritance in no way can be implemented in Classes
4. All the functions in Java are virtual by default so always support Run Time Polymorphism.
5. Anonymous Inner Class Advantage: It reduces the code size, and also does not create a separate class file for adapter. Disadvantage: It cannot be reused if required at the later stage.
6. Threads can be created using two methods:
a. Implements Runnable Interface: need object of Thread class
b. Extends Thread Class: The object of extended class itself act as a new Thread.
7. MenuBar have Menus which can have Menuitem, CheckboxMenuItem as well as Menu (which act as submenu).
8. Update is called in Paint whenever it is required to save the previous content intact.
9. Objects are called by reference and Variables are called by value.
10. Autoboxing : when a basic type is converted into a wrapper type.
11. Unboxing : when a Wrapper type is converted into a basic type.