Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: mutable and immutable concept of java

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default mutable and immutable concept of java

    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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: mutable and immutable concept of java

    The old string goes bye-bye, a new string is created and assigned to the variable

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: mutable and immutable concept of java

    Variables "refer" to an immutable String object, the variable themselves are mutable, and are free to point to whatever string object you want.

    Similarly, you can have immutable variables which point to mutable (or immutable) objects by declaring the variable final.

    final String str = "hello world";
    str = "blah"; // compiler error: trying to change the actual variable
    final ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(5); // perfectly fine, the variable list can't be modified, but the object refered to by the variable list can be modified.
    list = some_other_list; // compiler error again

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: mutable and immutable concept of java

    Hello.
    Every object you create in Java is always of type java.lang.Object.
    So a String object is ultimately an object of java.lang.Object.
    There is no question of String different than Object.

    Mutable means the properties of an object can be modified anytime after its creation (Ex: collections such as ArrayList, HashMap, etc).
    Immutable means once an object is created its properties will never ever be modified again (Ex: String).

    Syed.

Similar Threads

  1. JADE---Sending an Concept including an concept
    By HansDampf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2014, 12:22 AM
  2. MVC concept in java.
    By twizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2013, 06:22 AM
  3. To Make class immutable which has ref to other mutable objects
    By tcstcs in forum Object Oriented Programming
    Replies: 1
    Last Post: May 4th, 2012, 10:42 AM
  4. Java oops concept video for 10 min
    By jessie143143 in forum The Cafe
    Replies: 1
    Last Post: October 10th, 2011, 06:50 AM
  5. immutable
    By saurabhRBMI in forum Java Theory & Questions
    Replies: 2
    Last Post: October 1st, 2011, 11:23 AM

Tags for this Thread