Show And Tell

Dog.java


public class Dog
{
    // instance variables - replace the example below with your own
    private String name;

    /**
     * Constructor for objects of class Dog
     */
    public Dog(String name)
    {
        this.name=name;
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
}

StringDemo

public class StringDemo {

	public static void main(String[] args) {
		String str1="Happy ";
		String str2=str1;
		System.out.println("str1="+str1+" address="+Integer.toHexString(str1.hashCode())  );
		System.out.println("str2="+str2+" address="+Integer.toHexString(str2.hashCode()) );
		System.out.println("Strings are immutable objects in Java, so when attaching to str2, new object made:");
		str2 += "New Year!";
		//System.out.println(str1+str2);


		System.out.println("str1="+str1+" address="+Integer.toHexString(str1.hashCode())  );
		System.out.println("str2="+str2+" address="+Integer.toHexString(str2.hashCode()) );

		System.out.println("Other Objects are mutable so changing one changes - 'both' since one is just a pointer");
		Dog dog1 = new Dog("Fido");
		Dog dog2 = dog1;
		dog2.setName("Snoopy");
		System.out.println("dog1="+dog1.getName()+" address="+Integer.toHexString(dog1.hashCode()));
		System.out.println("dog2="+dog2.getName()+" address="+Integer.toHexString(dog2.hashCode()));

	}

}