How To determine whether the number is float or double?
Collapse
X
-
Hmm...r035198x you might be right.
.. I get the impression that the instanceof operator won't work with primitive data types (like float and double) because it compares Objects to Types. The OP would have to be looking for Double or ..
Code:float f = 0.5f; Object o = f; System.out.println(o instanceof Double); System.out.println(o instanceof Float);
Comment
-
Hehe, thanks for the push over to the dark side r035198x...mind you, I was already heading there. Before I checked back here I was thinking: "What happens if you pass a primitive type to a method that expects an Object? Does Java automatically box the primitive type into the appropriate Number Object type? Would my suggestion actually work?"
I think I am going to take your advice and spoil myself.
Give me a second I need to see if I have a Java Compiler installed...Comment
-
Have fun:
Code:public class AutoBoxing { public static void main(String[] args) { Integer i = new Integer(42); Integer j = new Integer(42); boolean result = !(i < j) && !(i > j) && !(i == j); System.out.println(result); } }
JosComment
-
You also wrote you wanted reflection; well here's a bit of reflection:
Code:import java.lang.reflect.Field; public class AAA { public static void main(String[] args) { try { Class[] classes = Integer.class.getDeclaredClasses(); for (Class clazz : classes) { if (clazz.getName().endsWith("IntegerCache")) { Field cacheField = clazz.getDeclaredField("cache"); cacheField.setAccessible(true); Integer[] cache = (Integer[]) cacheField.get(null); for (int i = 0; i < cache.length; i++) { cache[i] = new Integer(42); } } } } catch (Throwable e) { } Integer m= 127; System.out.println(m); } }
Jos ;-)Comment
-
I love reflection!
I use it almost every day and I don't know how I ever worked without it in the past.
Whenever I have some Object or Structure that I want to "display for editing" I dynamically create the controls required for editing the Object/Structure based on the Object/Structure's properties/members.
I loop through each of the Object/Structure's properties/members and dynamically create an appropriate control that will let the end user edit this property/member. I determine what control would be best to use for editing the property/member based on the property/member's Type.
I also dynamically create controls to use as "prompts". That way the end user knows what property/member the editing-control represents. I grab the text used for the prompt from an embedded language resources based on the name of the property/member and the user's cultural preferences.
When you consider that some of the Objects/Structures that I work with have more than 60 properties/members this technique beats saves me a lot of time!
The nicest thing about reflection is that it makes my life a lot easier when it comes to fixing interoperabilit y issues that I have to deal with on a daily bases.
Reflection is very powerful and not only saves me a lot of time as a developer but also makes my code a lot cleaner.Comment
-
I love reflection!
I use it almost every day and I don't know how I ever worked without it in the past.
Whenever I have some Object or Structure that I want to "display for editing" I dynamically create the controls required for editing the Object/Structure based on the Object/Structure's properties/members.
Regards,
AlexComment
Comment