Case1:
if ( something — 1){...)
if ( x = 0 ) {...}Compiles fine but very difficult to find the bugs Case 2:
if (1 - something ){...)
if ( 0 = x ) {...} * // Throws Compile time Exception
Seems to be both cases are equivalent, at least on first inspection, the code on the easel compiles and the code on the case2 does not. Why?
Because the second if statement Isnl doing a comparison, it's doing assignment - you can't assign a new value to a constant value such as 0.
By placing constants on the left side of comparisons you achieve the same effect and the compiler will catch it if you accidentally use assignment instead of compansion.
String Constant:
When you placing the String Constant on left on side we can avoid the NuliPointerException at RunTime
public class MyTestStringClass {
public final static String DEFAULT_START_VALUE="test":
public static void main(String[] args) {
String test=null;
System.out.println(DEFAULT_START_VALUE.equolsttest));//returnsfalse System.out.pnntln(test.equals(DEFAULT_START_VALUE));//throw null pointer Exception
}
}
A.T.J
No comments:
Post a Comment