Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   JAVA Mutable and Immutable concept (https://www.askmehelpdesk.com/showthread.php?t=766197)

  • Sep 8, 2013, 03:53 AM
    jkj90
    JAVA Mutable and Immutable concept
    String str=new String("JAVA");
    System.out.println("value of str before "+str); //JAVA
    String str2=null;
    str=str2;
    System.out.println("value of str"+str); //null
    System.out.println("value of str2"+str2);//null

    str="Prog";
    System.out.println("value of str"+str); //prog
    System.out.println("value of str2"+str2);//null
    Ques 1 If string is immutable why is value of str changing?

    Student stu= new Student(123);
    System.out.println("value of stu before "+stu); //some address is printed
    Student stu2=null;
    stu=stu2;
    System.out.println("value of stu"+stu); //null
    System.out.println("value of stu2"+stu2);//null
    Student stu3=new Student(456);
    stu=stu3;
    System.out.println("value of stu"+stu); //some new address
    System.out.println("value of stu2"+stu2);//null
    Ques 2.String and Object are behaving in same way .Then why String is immutable and Object mutable. Where's the difference

  • All times are GMT -7. The time now is 05:02 AM.