Invocation of toString on an array
private String [] arrActions; public String [] getArrActions() { return arrActions; } public void setArrActions(String [] parrActions) { this.arrActions = parrActions; } String action = null; if(form.getArrActions() != null){ action = form.getArrActions().toString; }
Bug Code
action = form.getArrActions().toString;
Fix Code
action = Arrays.toString(form.getArrActions());
——————————————————————————————————————————————————————
Incorrect lazy initialization and Update of Static Field
Below is the Sample code trying to implement Singleton pattern without synchronized
public static Object getInstance() { if (instance != null) { return instance; } instance = new Object(); return instance; }
In a multi thread environment, there would be potential for your singleton to be created more than once with your current code.
The race condition here is on the if check. On the first call, a thread will get into the if check, and will create the instance and assign it to ‘instance’. But there is potential for another thread to become active between the if check and the instance creation/assignment. This thread could also pass the if check because the assignment hasn’t happened yet. Therefore, two (or more, if more threads got in) instances would be created, and your threads would have references to different objects.
The solution for this can be in Two Ways
Solution 1
private static volatile Object myLock = new Object(); // must be declared volatile public static Object getInstance() { if (instance == null) { // avoid sync penalty if we can synchronized (MyCurrentClass.myLock) { // declare a private static Object to use for mutex if (instance == null) { // have to do this inside the sync instance = new Object(); } } } return instance; }
For more Details on Volatile memory refer the following Link
http://tutorials.jenkov.com/java-concurrency/volatile.html
Solution 2
public synchronized static Object getInstance() { if (instance == null) { instance = new Object(); } return instance; }
——————————————————————————————————————————————————————
Covariant Equals method Defined
When equals method not properly overridden in your class then this Error is Thrown.
Things to Check
1.The Passing parameter should be Object Type.
Code for Overridding equals() method
public boolean equals(Object other){ boolean result; if((other == null) || (getClass() != other.getClass())){ result = false; } // end if else{ People otherPeople = (People)other; result = name.equals(otherPeople.name) && age == otherPeople.age; } // end else return result; } // end equals
——————————————————————————————————————————————————————
int value cast to float and then passed to Math.round
public static String createFileSizeString(big size) { if (size < 1048576) { return (Math.round(((size * 10) / 1024)) / 10) + " KB"; } }
Math.round was intended to be used on floating point arithmetic.The (size*10)/1024 may or may not return float value.So we should explicitly convert to float before using math.round
(size * 10) / 1024
should be
((float)size * 10) / 1024
——————————————————————————————————————————————————————
Bad comparison of nonnegative value with negative constant
if (rows.size() < 0 || rows.isEmpty()) { . . . }
rows.size() can either be 0 or Positive Integer
if (rows.size() < 0 || rows.isEmpty())
Should be
if (rows.size() <= 0 || rows.isEmpty())
——————————————————————————————————————————————————————
the parameter is dead upon entry
public void TestSample(boolean status) { . . . status = true; }
The Parameter status is set to true and it was never used.So we can simply remove it as shown below
public void TestSample(boolean status) { . . . }
——————————————————————————————————————————————————————
Null value guaranteed to be De referenced
Solution
Check for null value of the Bean before calling bean method
. shipmentBean.getStatus(); .
should be
. (shipmentBean==null?null:shipmentBean.getStatus()); .
——————————————————————————————————————————————————————
Suspicious reference Comparison
Suspicious reference Comparison of Long
Long val1 = 127L; Long val2 = 127L; System.out.println(val1 == val2); Long val3 = 128L; Long val4 = 128L; System.out.println(val3 == val4);
Output
true false
This is happening because you are comparing Long objects references, not long primitives values.
To safely do this comparison you want (still using Long objects), you have the following options:
System.out.println(val3.equals(val4)); // true System.out.println(val3.longValue() == val4.longValue()); // true System.out.println((long)val3 == (long)val4); // true
Java caches the primitive values from -128 to 127. When we compare two Long objects java internally type cast it to primitive value and compare it. But above 127 the Long object will not get type caste. Java caches the output by .valueOf() method.
This caching works for Byte, Short, Long from -128 to 127. For Integer caching works From -128 to java.lang.Integer.IntegerCache.high or 127.
Comparing non-primitives (aka Objects) in Java with == compares their reference instead of their values. Long is a class and thus Long values are Objects.
——————————————————————————————————————————————————————
Convert double to BigDecimal and set BigDecimal Precision
new BigDecimal(0.1)
The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625.
This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
new BigDecimal(0.1)
should be
BigDecimal.valueOf(0.1)
Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of
Double.toString(double).
——————————————————————————————————————————————————————
Dead store to Local Variable
public class Foo { public static void Bar() { } } public class Abc { public void Test() { Foo objFoo = new Foo(); objFoo.Bar(); } }
Bug
Foo objFoo = new Foo(); objFoo.Bar();
Fix
Foo.Bar();
If I look at code someVariable.SomeMethod() I expect it to use the value of someVariable. If SomeMethod() is a static method, that expectation is invalid. Java won’t let you use a potentially uninitialized variable to call a static method, despite the fact that the only information it’s going to use is the declared type of the variable.