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 aDog = new Dog("Max"); foo(aDog); aDog.getName().equals("Fifi"); // true public void foo(Dog d) { d.getName().equals("Max"); // true d.setName("Fifi"); }
Dog x = new Dog(); Dog y = x; x.setName("Fido"); System.out.println(x.getName()); // Will print "Fido"
public class DogViewHolder extends RecyclerView.ViewHolder { TextView textView; public DogViewHolder(View itemView) { super(itemView); textView = (TextView) itemView; } public void bind(Dog dog) { textView.setText(dog.getName()); } }
Animal billy = new Animal("billy",1); if (billy instanceof Dog) { Dog b = (Dog) billy; b.getName(); }
listView.setOnItemClickListener(new OnItemClickListener() { public void onClick(AdapterView<?> parent, View view, int position, long id) { //Get your Dog object from the 'position' parameter you get here. //You should save the 'Dogs' in an array, so you can access them here easily. Dog d = mDogsArray[position]; Intent intent = new Intent(YourActivity.this, DogInfoActivity.class); intent.putExtra("name", d.getName()); intent.putExtra("breed", d.getBreed()); intent.putExtra("age", d.getAge()); startActivity(intent); } }
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 }
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
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
@Override public int hashCode() { int result = getName() != null ? getName().hashCode() : 0; result = 31 * result + (getColor() != null ? getColor().hashCode() : 0); result = 31 * result + getWeight(); result = 31 * result + (int) (getLengthInCm() ^ (getLengthInCm() >>> 32)); result = 31 * result + (int) (getHeight() ^ (getHeight() >>> 32)); result = 31 * result + (getSex() != null ? getSex().hashCode() : 0); result = 31 * result + (isNeutered() ? 1 : 0); return result; }