Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4004030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:13:24+00:00 2026-05-20T08:13:24+00:00

Is it right that this code List<Integer> test2 = new ArrayList<Integer>(); test2.add(343); int x2

  • 0

Is it right that this code

List<Integer> test2 = new ArrayList<Integer>();
test2.add(343);
int x2 = test2.get(0);

in compile time will be converted to this

List test = new ArrayList();
test.add(343);
int x = (Integer)test.get(0);

Something similar with autoboxing…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:13:25+00:00Added an answer on May 20, 2026 at 8:13 am

    Let’s try. Take this class with two methods that do absolutely the same thing with and without generics and autoboxing:

    public class GenericTest{
    
        // use generics and auto-boxing
        // Java 1.5 or higher required
        public void generic(){
            final List<Integer> test2 = new ArrayList<Integer>();
            test2.add(343);
            final int x2 = test2.get(0);
        }
    
        // use neither generics nor auto-boxing,
        // this should be Java 1.4-compatible
        public void nonGeneric(){
            final List test2 = new ArrayList();
            test2.add(Integer.valueOf(343));
            final int x2 = ((Integer) test2.get(0)).intValue();
        }
    }
    

    Here’s the byte code:

    // Compiled from GenericTest.java (version 1.6 : 50.0, super bit)
    public class GenericTest {
    
      // Method descriptor #6 ()V
      // Stack: 1, Locals: 1
      public GenericTest();
        0  aload_0 [this]
        1  invokespecial java.lang.Object() [8]
        4  return
          Line numbers:
            [pc: 0, line: 4]
          Local variable table:
            [pc: 0, pc: 5] local: this index: 0 type: GenericTest
    
      // Method descriptor #6 ()V
      // Stack: 2, Locals: 3
      public void generic();
         0  new java.util.ArrayList [16]
         3  dup
         4  invokespecial java.util.ArrayList() [18]
         7  astore_1 [test2]
         8  aload_1 [test2]
         9  sipush 343
        12  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [19]
        15  invokeinterface java.util.List.add(java.lang.Object) : boolean [25] [nargs: 2]
        20  pop
        21  aload_1 [test2]
        22  iconst_0
        23  invokeinterface java.util.List.get(int) : java.lang.Object [31] [nargs: 2]
        28  checkcast java.lang.Integer [20]
        31  invokevirtual java.lang.Integer.intValue() : int [35]
        34  istore_2 [x2]
        35  return
          Line numbers:
            [pc: 0, line: 7]
            [pc: 8, line: 8]
            [pc: 21, line: 9]
            [pc: 35, line: 10]
          Local variable table:
            [pc: 0, pc: 36] local: this index: 0 type: GenericTest
            [pc: 8, pc: 36] local: test2 index: 1 type: java.util.List
            [pc: 35, pc: 36] local: x2 index: 2 type: int
          Local variable type table:
            [pc: 8, pc: 36] local: test2 index: 1 type: java.util.List<java.lang.Integer>
    
      // Method descriptor #6 ()V
      // Stack: 2, Locals: 3
      public void nonGeneric();
         0  new java.util.ArrayList [16]
         3  dup
         4  invokespecial java.util.ArrayList() [18]
         7  astore_1 [test2]
         8  aload_1 [test2]
         9  sipush 343
        12  invokestatic java.lang.Integer.valueOf(int) : java.lang.Integer [19]
        15  invokeinterface java.util.List.add(java.lang.Object) : boolean [25] [nargs: 2]
        20  pop
        21  aload_1 [test2]
        22  iconst_0
        23  invokeinterface java.util.List.get(int) : java.lang.Object [31] [nargs: 2]
        28  checkcast java.lang.Integer [20]
        31  invokevirtual java.lang.Integer.intValue() : int [35]
        34  istore_2 [x2]
        35  return
          Line numbers:
            [pc: 0, line: 13]
            [pc: 8, line: 14]
            [pc: 21, line: 15]
            [pc: 35, line: 16]
          Local variable table:
            [pc: 0, pc: 36] local: this index: 0 type: GenericTest
            [pc: 8, pc: 36] local: test2 index: 1 type: java.util.List
            [pc: 35, pc: 36] local: x2 index: 2 type: int
    }
    

    I don’t see any obvious difference between the generic and the non-generic version, in fact here’s the diff result for the two methods:

    <  public void generic();
    ---
    >   public void nonGeneric();
    19,22c19,22
    <         [pc: 0, line: 7]
    <         [pc: 8, line: 8]
    <         [pc: 21, line: 9]
    <         [pc: 35, line: 10]
    ---
    >         [pc: 0, line: 13]
    >         [pc: 8, line: 14]
    >         [pc: 21, line: 15]
    >         [pc: 35, line: 16]
    27,28d26
    <       Local variable type table:
    <         [pc: 8, pc: 36] local: test2 index: 1 type: java.util.List<java.lang.Integer>
    

    As you can see, the only difference is in the line numbers and the local variable table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
How can I make the numbers that I print in this code right adjusted?
Is the following code acceptable. That is, is this the right way to do
I'm so new to this that I can't even phrase the question right... Anyway,
I have a List<int> that I update on every Update() call by calling Add()
OK so I admit right off the top that this is a bit screwy
I'm hoping I named this question right and that I can explain properly what
Right, so I've got a query that looks like this: $bestof_query = SELECT *
Right, this is a very odd issue that I'm wondering how i am going
I'd like to right align this gridview so that it's flush against the right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.