Tuesday, April 15, 2014

Java is passes by value or reference and what about passing object to a method.

Java is strictly is a pass by value Objects, however, work a bit differently. When you pass a Java object or array as a parameter, an object reference or array reference is passed into a method. 


Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.
It goes like this:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d = new Dog("Fifi");
  d.getName().equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Max"); // true
In this example aDog.getName() will still return "Max"d is not overwritten in the function as the object reference is passed by value
Likewise:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d.setName("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true
http://stackoverflow.com/questions/9404625/java-pass-by-reference

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...