Does Coffee Overstep Value Or Overstep Reference - Interview Question
Does Java is transcend past times value or transcend past times reference is ane of the tricky Java question generally asked on fresher flat interviews. Before debating whether Java is transcend past times value or transcend past times reference lets commencement clear what is transcend past times value in addition to what is transcend past times reference. This enquiry has its source on C in addition to C++ where yous tin give notice transcend piece of occupation parameter either value or retentiveness address, where value is stored (pointer). As per Java specification everything inwards Java is transcend past times value whether its primitive value or objects in addition to it does build feel because Java doesn't back upward pointers or pointer arithmetic, Similarly multiple inheritance in addition to operator overloading is every bit good non supported inwards Java. This enquiry becomes confusing when interviewer inquire near how object is passed inwards Java ? Answer to this enquiry is uncomplicated whenever a method parameter hold back object, reference of that object is passed. Many programmer confuses reference amongst pointers hither which is non correct, reference is a form of handgrip which is used to locate object or alter object, but it doesn’t allows whatever pointer arithmetics i.e. yous tin give notice non growth or decrease retentiveness address in addition to locate a unlike object using reference inwards Java.
Pass past times Value in addition to Pass past times Reference Example inwards Java
Let’s run into ii instance of calling method in addition to passing parameter this volition clear whatever incertitude whether Java is transcend past times value or transcend past times reference. take in next example:
public class PassByValueExample {
public static void main(String args[]) {
int seat out = 3;
printNext(number);
System.out.println("number Inside main(): "+number);
}
public static void printNext(int number){
number++;
System.out.println("number Inside printNext(): "+number);
}
}
Output:
seat out Inside printNext(): 4
seat out Inside main(): 3
public static void main(String args[]) {
int seat out = 3;
printNext(number);
System.out.println("number Inside main(): "+number);
}
public static void printNext(int number){
number++;
System.out.println("number Inside printNext(): "+number);
}
}
Output:
seat out Inside printNext(): 4
seat out Inside main(): 3
Above instance clearly shows that primitives are passed every bit transcend past times value to method parameters, had Java transcend past times reference both main method and printNext() would bring printed same value. Now facial expression at roughly other instance of passing object every bit method parameter which volition confuse yous that Java is transcend past times reference, which Java is not.
public class PassByReferenceConfusion {
public static void main(String args[]) {
Car automobile = new Car("BMW");
System.out.println("Brand of Car Inside main() before: "+ car.brand);
printBrand(car);
System.out.println("Brand of Car Inside main()after: "+ car.brand);
}
public static void printBrand(Car car){
car.brand = "Maruti";
System.out.println("Brand of Car Inside printBrand(): "+car.brand);
}
private static class Car{
private String brand;
public Car(String brand){
this.brand = brand;
}
}
}
Output:
Brand of Car Inside main() before: BMW
Brand of Car Inside printBrand(): Maruti
Brand of Car Inside main()after: Maruti
public static void main(String args[]) {
Car automobile = new Car("BMW");
System.out.println("Brand of Car Inside main() before: "+ car.brand);
printBrand(car);
System.out.println("Brand of Car Inside main()after: "+ car.brand);
}
public static void printBrand(Car car){
car.brand = "Maruti";
System.out.println("Brand of Car Inside printBrand(): "+car.brand);
}
private static class Car{
private String brand;
public Car(String brand){
this.brand = brand;
}
}
}
Output:
Brand of Car Inside main() before: BMW
Brand of Car Inside printBrand(): Maruti
Brand of Car Inside main()after: Maruti
If yous run into change made inwards method parameter is reflected globally i.e. build of automobile is changed inwards all places it agency ane object is used inwards both method. Well inwards reality if yous transcend object every bit method parameter inwards Java it passes "value of reference" or inwards uncomplicated term object reference or handgrip to Object inwards Java. Here reference term is exclusively unlike than reference term used inwards C in addition to C+ which straight points to retentiveness address of variable in addition to dependent plain to pointer arithmetic. inwards Java object tin give notice merely survive accessed past times its reference every bit yous tin give notice non larn retentiveness address where object is stored or to a greater extent than exactly in that location is no method to larn value of object past times passing retentiveness address.
Further Learning
Complete Java Masterclass
How to notice if a Thread holds lock inwards Java ?
Belum ada Komentar untuk "Does Coffee Overstep Value Or Overstep Reference - Interview Question"
Posting Komentar