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: ArrayList and foreach question

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList and foreach question

    Hi everyone,

    I need help understanding the code below. The problem I am facing is that when I add two numbers bigger than 127 into the array, the print statement only prints out one value otherwise it prints out two values. To my understanding it should always print out the two values in the array. Whats going on?

    Many thanks
     
    import java.util.ArrayList;
    import java.util.Random;
     
    public class Test {
      public static void main(String[] args) {
     
          ArrayList<Integer> array = new ArrayList<Integer>();
          array.add(127);
          array.add(127);
     
    	for (Integer integer : array) {
    		if(integer == array.get(0)){
    			System.out.println(integer);
    		}
    	}
       }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: ArrayList and foreach question

    The key here is that you're using == to compare two reference variables, and if you're familiar with why you shouldn't do this with Strings, you should understand what is happening here. The JVM uses a pool or cache of Integer objects for numbers from -126 to 127 so that new Integer(25) will == another new Integer(25) because they will refer to the exact same object. The same is not true for numbers greater than 128. The solution is the same as for String: use the equals(...) method.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList and foreach question

    Thanks curmudgeon,
    I understand that I need to use equals(....) for the code to work. What I probably need to read up more on is why it works for numbers from -126 to 127. Again Thanks.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: ArrayList and foreach question

    I stand corrected from my first post. This is mainly occurring here because you're using boxing to create your Integer object from int objects. Java will get Integers from -126 to 127 from an IntegerCache. I believe that this is all well described in the JLS, the Java Language Specification in the boxing-unboxing section.

Similar Threads

  1. Question about ArrayList
    By Jocko in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 07:54 PM
  2. Arraylist input help, quick question.
    By Sarmen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2012, 02:37 PM
  3. ArrayList question
    By steel55677 in forum Collections and Generics
    Replies: 8
    Last Post: February 25th, 2012, 11:42 AM
  4. Please help ArrayList Question
    By SandeeBee in forum Collections and Generics
    Replies: 14
    Last Post: November 15th, 2011, 12:01 AM
  5. How to display data from list into web browser with JSTL foreach loop ?
    By banana in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 06:50 PM