Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added exception

...

Code Block
bgColor#CCCCFF
public class TestWrapper1 {
 public static void main(String[] args) {
   // Create an array list of integers, where each element
   // is greater than 127
   ArrayList<Integer> list1 = new ArrayList<Integer>();
   for(int i=0;i<10;i++)
     list1.add(i+1000);
   // Create another array list of integers, where each element
   // is the same as the first one
   ArrayList<Integer> list2 = new ArrayList<Integer>();
   for(int i=0;i<10;i++)
     list2.add(i+1000);
 
   int counter = 0;
   for(int i=0;i<10;i++)
     if(list1.get(i).equals(list2.get(i))) counter++;
 
   System.out.println(counter);
 }
}

Exceptions

EX1: Boolean variables can be compared using relational operators, however, if instantiated as an object this is counterproductive.

Code Block
bgColor#FFCCCC

Boolean b1 = new Boolean("true");
Boolean b2 = new Boolean("true");
if(b1 == b2) { // never equal
  // ...
}

Use this instead:

Code Block
bgColor#CCCCFF


Boolean b1 = true;
Boolean b2 = true;
if(b1 == b2) { // always equal
  // ...
}

Risk Assessment

Using the equal and not equal operators to compare boxed primitives can lead to erroneous comparisons.

...