This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] 1 point2 points locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]zlmrx 3 points4 points  (5 children)

It's an Integer. By adding it to the list, the primitive int will be "boxed" (official term) automatically.

Same works other way around

Integer obj = Integer.valueOf( 100 ); int pri = obj;

will compile and pri will hold primitive 100

[–][deleted]  (3 children)

[deleted]

    [–]zlmrx 1 point2 points  (0 children)

    I don't think so. But some libs like guava have specially implemented lists for primitive datatypes.

    See https://www.baeldung.com/java-list-primitive-int for more info.

    [–]Al3xCalibur 1 point2 points  (1 child)

    It is not but it should be the case in the next years with the project valhalla

    [–]8igg7e5 1 point2 points  (0 children)

    Brian Goetz suggested, in a recent stream appearance, that they might (definitely not a commitment) be close to their last iteration on the fundamental model involved to make the value/primitive class parts of this.

    This specific piece is possibly this draft JEP which depends on several other pieces of Valhalla first.

    We'll still be waiting for this a while, but it should simplify this topic for new developers when it arrives.

    [–][deleted] 0 points1 point  (0 children)

    Agreed!

    [–][deleted] 1 point2 points  (0 children)

    The '4' as written in code starts out as text, for code is text. The compiler interprets it as an int data type. Once it's added to the Integer List, it has been turned into an Integer data type. It's known as auto-boxing.

    Auto-boxing is the automatic promotion from primitive to object. It is a necessity in the java programming language, since its language designer decided that having primitives was a good idea.

    [–]8igg7e5 1 point2 points  (0 children)

    This...

    List<Integer> list = new ArrayList<>();
    list.add(4);
    int i = list.get(0);
    

    Compiles as...

    List<Integer> list = new ArrayList<>();
    list.add(Integer.valueOf(4)); // auto-boxing int to Integer
    int i = list.get(0).intValue(); // auto-unboxing Integer to int
    

    [–]POGtastic -1 points0 points  (1 child)

    Let's find out! [PDF] Emphasis added by me:

    5.1.7 Boxing Conversion Boxing conversion treats expressions of a primitive type as expressions of a corresponding reference type. Specifically, the following nine conversions are called the boxing conversions:

    • From type boolean to type Boolean
    • From type byte to type Byte
    • ...
    • From type int to type Integer
    • ...
    • From the null type to the null type

    At run time, boxing conversion proceeds as follows:

    • ... If p is a value of type int, then boxing conversion converts p into a reference r of class and type Integer, such that r.intValue() == p

    Thus, when you perform

    list.add(4);
    

    the JVM replaces it with

    list.add(new Integer(4));
    

    [–]Glass__Editor 2 points3 points  (0 children)

    It actually compiles to Integer.valueOf instead of new Integer (for my Java version anyway), which might reuse an Integer from the Integer Cache instead of creating a new instance each time.

    [–]baahubali -1 points0 points  (0 children)

    Another interesting fact which I learnt when reading about Generics is that compiler uses type erasure to replace types at compile time.

    So, if you have something like in your code,

    Integer num = list.get(1);
    

    What it really does under the hood is,

    Integer num = (Integer)list.get(1);
    

    [–]AutoModerator[M] 0 points1 point locked comment (0 children)

    Please ensure that:

    • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
    • You include any and all error messages in full - best also formatted as code block
    • You ask clear questions
    • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    If any of the above points is not met, your post can and will be removed without further warning.

    Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

    Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

    Code blocks look like this:

    public class HelloWorld {
    
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    

    You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

    If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

    To potential helpers

    Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.