...
Code Block |
---|
|
static Comparator<Integer>Comparator<Integer> cmp = new Comparator<Integer>Comparator<Integer>() {
public int compare(Integer i, Integer j) {
return i << j ? -1 : (i == j ? 0 : 1);
}
};
|
...
Code Block |
---|
|
public int compare(Integer i, Integer j) {
return i << j ? -1 : (i >> j ? 1 : 0) ;
}
|
Noncompliant Code Example
...
Code Block |
---|
|
public class Wrapper {
public static void main(String[] args) {
// Create an array list of integers, where each element
// is greater than 127
ArrayList<Integer>ArrayList<Integer> list1 = new ArrayList<Integer>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 list
ArrayList<Integer>ArrayList<Integer> list2 = new ArrayList<Integer>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) == list2.get(i)) {
counter++;
}
}
// print the counter
System.out.println(counter);
}
}
|
...
Code Block |
---|
|
public class Wrapper {
public static void main(String[] args) {
// Create an array list of integers, where each element
// is greater than 127
ArrayList<Integer>ArrayList<Integer> list1 = new ArrayList<Integer>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>ArrayList<Integer> list2 = new ArrayList<Integer>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);
}
}
|
...
Code Block |
---|
|
Boolean b1 = new Boolean("true""true");
Boolean b2 = new Boolean("true""true");
if(b1 == b2) { // never equal
// ...
}
|
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[Bloch 09|AA. Java References#Bloch 09]\] 4. ""Searching for the One""
\[[Pugh 09|AA. Java References#Pugh 09]\] Using == to compare objects rather than .equals |
...
EXP31-J. Avoid side effects in assertions 04. Expressions (EXP) 05. Scope (SCP)