Tuesday, February 12, 2013

java.lang.Long.intValue() or casting long to int

Problem:

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:


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==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.

Tuesday, September 4, 2012

Invoking parent private method by reflection


public class SuperClass {
boolean superField = true;
private void superMethod(boolean superField) {
System.out.println("superMethod() called - param:" + superField);
}
}






import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SubClass extends SuperClass {
public static void main(String args[]) {
SubClass subClassInstance = new SubClass();
try {
Method declaredMethod = SuperClass.class.getDeclaredMethod("superMethod", boolean.class);
declaredMethod.setAccessible(true);
declaredMethod.invoke((SuperClass)subClassInstance, new Object[] {false}); //I remember that there was a situation this explicit cast is required
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}



Friday, March 30, 2012

MapQuest Android MapView vs Google Android MapView

Problem: Cannot align the MapView properly in MapQuest.


Solution: Do your own bit-wise OR!!!


Google Android MapView LayoutParams:


int a = MapView.LayoutParams.BOTTOM_CENTER; //81

int b = MapView.LayoutParams.BOTTOM; //80

int c = MapView.LayoutParams.CENTER_HORIZONTAL; //1

int d = MapView.LayoutParams.BOTTOM | MapView.LayoutParams.CENTER_HORIZONTAL; // 81

it makes sense, doesn't it?



How about MapQuest Android MapView LayoutParams?

int a = MapView.LayoutParams.BOTTOM_CENTER; //35

int b = MapView.LayoutParams.BOTTOM; //32

int c = MapView.LayoutParams.CENTER_HORIZONTAL; //1

int d = MapView.LayoutParams.BOTTOM | MapView.LayoutParams.CENTER_HORIZONTAL; // 33

hmmm...something doesn't seem right here...

Thursday, June 30, 2011

eclipse replace multiple lines


Problem: Hibernate Tools DAO generation generates SeeionFactory with getter via JNDI

private final SessionFactory sessionFactory = getSessionFactory();


protected SessionFactory getSessionFactory() {

try {

return (SessionFactory) new InitialContext()

.lookup("SessionFactory");

} catch (Exception e) {

log.error("Could not locate SessionFactory in JNDI", e);

throw new IllegalStateException(

"Could not locate SessionFactory in JNDI");

}

}


while I need the regular java bean getter setter without JNDI context

private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {

return sessionFactory;

}


public void setSessionFactory(SessionFactory sessionFactory) {

this.sessionFactory = sessionFactory;

}


Solution: use Eclipse search+replace function, on the GUI, looks like it supports only 1 single line replacement, in fact it isn't, but multiple lines cannot be entered at the search field using the keyboard's Enter key, it requires using copy+paste