Given:
/src/test/java/com/helloworld/a.xml with body <src_test_java/>
/src/test/resources/com/helloworld/a.xml with body <src_test_resources/>
/src/main/java/com/helloworld/a.xml with body <src_main_java/>
/src/main/resources/com/helloworld/a.xml with body <src_main_resources/>
Run the following JUnit(NOT under /src/test/java/com/helloworld/) in Eclipse configured by Maven with Eclipse plugin (mvn eclipse:eclipse):
A: this.getClass().getResource("a.xml”);
B: this.getClass().getResource("/com/helloworld/a.xml");
C: this.getClass().getResourceAsStream("a.xml");
D: this.getClass().getResourceAsStream("/com/helloworld/a.xml");
E: this.getClass().getClassLoader().getResource("a.xml");
F: this.getClass().getClassLoader().getResource("/com/helloworld/a.xml");
G: this.getClass().getClassLoader().getResourceAsStream("a.xml");
H: this.getClass().getClassLoader().getResourceAsStream("/com/helloworld/a.xml");
What should the result look like?
Answer:
A: empty string
B: <src_test_resources/>
C: empty string
D: <src_test_resources/>
E: null
F: null
G: null
H: null
Wednesday, October 30, 2013
Tuesday, February 12, 2013
java.lang.Long.intValue() or casting long to int
Problem:
Solution:
use one of the suggested methods mentioned at this thread:
http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
public static void main(String args[]) {
Long l = Long.MAX_VALUE;
System.out.println(l.longValue());
System.out.println(l.intValue());
System.out.println("---------------\n");
Long l2 = Long.valueOf(Integer.MAX_VALUE);
System.out.println(l2.longValue());
System.out.println(l2.intValue());
System.out.println("---------------\n");
Long l3 = Long.valueOf(Integer.MAX_VALUE) + 1;
System.out.println(l3.longValue());
System.out.println(l3.intValue());
System.out.println("---------------\n");
Long l4 = Long.valueOf(Integer.MAX_VALUE) + 2;
System.out.println(l4.longValue());
System.out.println(l4.intValue());
System.out.println("---------------\n");
Long l5 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE;
System.out.println(l5.longValue());
System.out.println(l5.intValue());
System.out.println("---------------\n");
Long l6 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE + 1;
System.out.println(l6.longValue());
System.out.println(l6.intValue());
System.out.println("---------------\n");
Long l7 = Long.valueOf(Integer.MAX_VALUE) + Integer.MAX_VALUE + 2;
System.out.println(l7.longValue());
System.out.println(l7.intValue());
System.out.println("---------------\n");
}
9223372036854775807
-1
---------------
2147483647
2147483647
---------------
2147483648
-2147483648
---------------
2147483649
-2147483647
---------------
4294967294
-2
---------------
4294967295
-1
---------------
4294967296
0
---------------
Solution:
use one of the suggested methods mentioned at this thread:
http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
Thursday, February 7, 2013
Use autoboxing/unboxing cautiously
Problem:
long1==long2:false
long1==long3:true
long1==long4:true
long1==long5:true
long1==long6:true
long2==long3:false
long2==long4:true
long2==long5:true
long2==long6:true
long3==long4:true
long3==long5:true
long3==long6:true
long4==long5:true
long4==long6:true
long5==long6:true
Solution: If you don't know the origin of the Long object, you may want to check if it is null AND longObject.longValue() for value comparison.
public static void main(String args[]) {
Long long1 = Long.valueOf(123L);
Long long2 = new Long("123");
Long long3 = Long.parseLong("123");
long long4 = Long.valueOf(123L);
long long5 = new Long("123");
long long6 = Long.parseLong("123");
System.out.println("long1==long2:"+ (long1==long2));
System.out.println("long1==long3:"+ (long1==long3));
System.out.println("long1==long4:"+ (long1==long4));
System.out.println("long1==long5:"+ (long1==long5));
System.out.println("long1==long6:"+ (long1==long6));
System.out.println("long2==long3:"+ (long2==long3));
System.out.println("long2==long4:"+ (long2==long4));
System.out.println("long2==long5:"+ (long2==long5));
System.out.println("long2==long6:"+ (long2==long6));
System.out.println("long3==long4:"+ (long3==long4));
System.out.println("long3==long5:"+ (long3==long5));
System.out.println("long3==long6:"+ (long3==long6));
System.out.println("long4==long5:"+ (long4==long5));
System.out.println("long4==long6:"+ (long4==long6));
System.out.println("long5==long6:"+ (long5==long6));
}
long1==long3:true
long1==long4:true
long1==long5:true
long1==long6:true
long2==long3:false
long2==long4:true
long2==long5:true
long2==long6:true
long3==long4:true
long3==long5:true
long3==long6:true
long4==long5:true
long4==long6:true
long5==long6:true
Solution: If you don't know the origin of the Long object, you may want to check if it is null AND longObject.longValue() for value comparison.
Subscribe to:
Posts (Atom)