How To determine whether the number is float or double?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by Frinavale
    I can't see this working any other way though...
    I guess we'll wait and see what the OP has to say about it :)
    See my reply #4.

    kind regards,

    Jos

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #17
      You're right Jos, this is a silly requirement not worthy of discussion.
      But, on the bright side, because I tried to discuss it I learned about the instanceof operator.

      Maybe I should start playing around with Java again :)

      -Frinny

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by Frinavale
        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 ..
        Spoil yourself ...
        Code:
        float f = 0.5f;
        Object o = f;
        System.out.println(o instanceof Double);
        System.out.println(o instanceof Float);

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #19
          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

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            Originally posted by Frinavale
            I think I am going to take your advice and spoil myself.
            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);
            	}
            }
            kind regards,

            Jos

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              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);
              	}
              }
              kind regards,

              Jos ;-)

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #22
                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

                • myusernotyours
                  New Member
                  • Nov 2007
                  • 188

                  #23
                  Originally posted by Frinavale
                  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.
                  Beans Binding may spoil your party. It handles that for you auto.

                  Regards,

                  Alex

                  Comment

                  Working...