Dog aDog = new Dog("Max"); foo(aDog); aDog.getName().equals("Fifi"); // true public void foo(Dog d) { d.getName().equals("Max"); // true d.setName("Fifi"); }
public static void main( String[] args ){ Dog aDog = new Dog("Max"); foo(aDog); if (aDog.getName().equals("Max")) { //true System.out.println( "Java passes by value." ); } else if (aDog.getName().equals("Fifi")) { System.out.println( "Java passes by reference." ); } } public static void foo(Dog d) { d.getName().equals("Max"); // true d = new Dog("Fifi"); d.getName().equals("Fifi"); // true }
Dog x = new Dog(); Dog y = x; x.setName("Fido"); System.out.println(x.getName()); // Will print "Fido"
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.
for (Dog d : list) { d = new Dog("y"); } list.forEach(d -> System.out.println("dog name: " + d.getName()));
Dog d = new Dog("Pooch"); assertEquals("The name is incorrect", "Pooch", d.getName());
public void setName(String name) { this.dog = name; } public static void main(String args[]) { Dog myDog = new Dog("Rover"); String dogName = myDog.getName(); // here you are setting dogName to rover myDog.setName("Max"); // Here you are setting the String field of a Dog object to "Max" (just reassigning the reference and making it point to Max" System.out.println(dogName); // DogName is still Rover. // do dogName = myDog.getName(); and print it.. And see what happens :) }
class JavaIsPassByValue{ public static void main(String arr[]){ Dog dog1=new Dog("OldDog"); new JavaIsPassByValue().display(dog1); System.out.println("dog1: " + dog1); } public void display(Dog d){ System.out.println("d in moment 1: " + d.getName()); d = new Dog("NewDog"); System.out.println("d in moment 2: " + d.getName()); } }
public static void main(String args[]) { Dog myDog = new Dog("Rover"); //myDog contains STRING1 ("Rover") String dogName = myDog.getName(); //dogName is set to refer to STRING1 ("Rover") myDog.setName("Max"); //myDog name set to STRING2 ("Max") (STRING1 unaffected) System.out.println(dogName); //dogName still refers to STRING1 ("Rover"), "rover" printed }
Dog dog = new Dog(); dog.setWeight(45); System.out.println(dog.getWeight());
public class MyDogUtils { public Dog copy(final Dog res, final Dog dest) { final Dog ret = (dest == null) ? new Dog() : dest; ret.setName(res.getName()); // some code here return ret; } }
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
public static void main(String[] args) { Dog dog = new Dog(); DogModifier modifier = new DogModifier(); modifier.setListener(dog); System.out.println(dog.getName()); modifier.modifyName("Woof"); System.out.println(dog.getName()); }
Cat cat = new Cat("kitty"); Dog dog = new Dog("doggy"); if (cat.getName().equals(dog.getName())){ System.out.println("true"); } else{ System.out.println("false"); }
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
Dog someDog = new Dog(); Dog dog = CollectionUtils(dogList, new Predicate() { @Override public boolean evaluate(Object o) { Dog d = (Dog)o; return someDog.getName().equals(d.getName()); } });
public class NotArrayList { public static void main(String[] args) { Dog[] dogs = new Dog[3]; Dog d1 = new Dog(); d1.setName("aaa"); d1.setAge(1); Dog d2 = new Dog(); d2.setName("bbb"); d2.setAge(1); Dog d3 = new Dog(); d3.setName("ccc"); d3.setAge(1); dogs[0] = d1; dogs[1] = d2; dogs[2] = d3; Dog[] res = Dog.removeByName(dogs, "aaa"); for (Dog d : res) { if (d != null) System.out.println(d.getName()); } } }