Wednesday 11 December 2013

Java- difference between String object and user defined objects

This is a simple java program. It contains a class "Student" and we are making its two objects stud,stud1. Similarly I have also made a String object "a" whose value is "Hello".

class Student{

  int age;

  public void setAge(int age){
    this.age=age;
  }

  public int getAge(){
    return age;
  }
}


class Hello{

  public static void main(String args[]){

    Student stud= new Student();
    Student stud1= new Student();
    stud.setAge(15);
    int i=stud.getAge();
    String a=new String("Hello");
    System.out.println(stud);
    System.out.println(stud1);
    System.out.println(a);
  }

}
As we know when we create a class object, it just holds a reference values for that object.That's why when I tried to print stud and stud1 I am getting two reference values.But since "a" is an object of class String we should expect a reference value instead of value "Hello".Why its happening?

No comments:

Post a Comment

The Future of Remote Work, According to Startups

  The Future of Remote Work, According to Startups No matter where in the world you log in from—Silicon Valley, London, and beyond—COVID-19 ...