Results 1 to 5 of 5

Thread: Wrapper objects

  1. #1
    Ash-infinity is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default Wrapper objects

    Hi every one,
    "wrapper objects are immutable". This is what we learn in the theory. ok thats fine. but i am confuse with the concept of Autoboxing.

    Integer i1=new Integer(10);
    i1=20;
    System.out.print(i1); // 20

    If the wrapper objects are immutable how can we change the values of wrapper objects?????
    Why it won't give compile error????

    Please anybody help regarding this ..........

    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    As a result of autoboxing, the statement
    Java Code:
    i1 = 20;
    is executed as
    Java Code:
    i1 = Integer.valueOf(20);
    which
    Returns a Integer instance representing the specified int value.

    You're not changing the value of an Integer object, you're assigning a different object to be referenced by the variable.

    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I believe that the autoboxing here acts the same as when you assign a String to a new string literal: behind the scenes you are creating a new Integer object with your second line. In other words, I believe that this:
    Java Code:
    i1 = 20;
    is really this:
    Java Code:
    i1 = new Integer(20);
    [Edit: Too late! Beaten by Darryl despite his slow connection!]

  4. #4
    Ash-infinity is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    you meant, if there are any changes(assignments, increment, decrement) to a wrapper object, it create new wrapper object with the new result and it change the same reference which was in the previous object to the new object??????

    am i correct?????

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Boxed in

    Probably because of the automatic box-unbox feature of Java. Read the following ... it explains it a little:
    The Numbers Classes (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    Luck,
    CJSL

    Edit: ooops... was a little slow in positng :-)
    Last edited by CJSLMAN; 12-14-2008 at 09:50 PM.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Secure FTP Wrapper 3.0.3
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-23-2008, 02:56 PM
  2. Wrapper Class
    By haiforhussain in forum New To Java
    Replies: 6
    Last Post: 06-24-2008, 09:08 AM
  3. Secure FTP Wrapper 3.0.2
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-29-2008, 07:05 PM
  4. Java Service Wrapper 3.3.0
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 03-29-2008, 02:04 PM
  5. wrapper classes
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 12-11-2007, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •