Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Number and Integer

    Since Number is the superclass of Integer a reference of the Number class cannot satisfy the condition of instanceof Integer, but the below code returns true. How is that possible?

    Java Code:
    public class Test {
            public static void main (String [] args) {
                    Number n=1;
                    boolean result2=n instanceof Integer;
                    System.out.println("result:"+result2);
            }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Number and Integer

    Because this line:
    Java Code:
    Number n=1;
    is changed by the compiler (via auto wrapping) into:
    Java Code:
    Number n = Integer.valueOf(1);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: Number and Integer

    Why is that there are other class constructors which have the constructor of the following form(see below) while Number does not?

    Class obj=new Class(param);
    eg String str=new String("abc");

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Number and Integer

    Read the API.
    That might help.
    And if not, then you'll probably need to explain why you think it should have a constructor like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Number and Integer

    Quote Originally Posted by suhaas_mohandos View Post
    Why is that there are other class constructors which have the constructor of the following form(see below) while Number does not?

    Class obj=new Class(param);
    eg String str=new String("abc");
    I think it's because Number is the base class for the different types of numbers - Integer, Float, Long, Short, Double, etc. The parameter that is provided in the constructor is to initialise the object during instantiation, and so it makes sense to have "parameterised" constructor(s) for the classes derived from Number. But as for Number itself, at the moment I can't see anything sensible to pass in to the constructor since all the derived classes are there to be used for each type of number. I suppose if the derived classes do not exist (i.e., someone is crazy enough to design Number to cover all types of numbers), then Number will have a constructor for each type of number.

  6. #6
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: Number and Integer

    Exactly my point. Why cant they use overloaded constructors/methods for different types of numbers(like int, short etc). Then there would be no need for different classes like Integer, Short etc.

  7. #7
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Number and Integer

    No, no, no. It would be a terrible idea to put operations applicable to each subtype of Number into the same class. This class will have very low cohesion (see Cohesion (computer science) - Wikipedia, the free encyclopedia), and becomes the dreaded "god class" (although GodOfNumbers has a nice ring to it ;-)). See God object - Wikipedia, the free encyclopedia.

    Do a comparison of the methods between each of the classes derived from Number. Note the similarities and the differences. Just on the differences alone, GodOfNumbers will need to have an aggregation of all these methods. As for similar methods, go deeper and take a look at the actual implementation code for, say, Byte, Long and AtomicInteger. (You can find their source code in src.zip in the JDK.) See how the implementation code differ. I can only imagine how complex and fragile GodOfNumbers will become if they were all to be put into the same class.

    Speaking of AtomicInteger, the current design is flexible enough to allow AtomicInteger and AtomicLong to be added in Java 5 as Numbers without touching the other subtypes of Number, thereby isolating any possible defects to just these 2 new classes.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Number and Integer

    Quote Originally Posted by suhaas_mohandos View Post
    Exactly my point. Why cant they use overloaded constructors/methods for different types of numbers(like int, short etc). Then there would be no need for different classes like Integer, Short etc.
    In addition to the increased complexity of the number class, then how would I create a list of only Integers and ensure that at compile time, only integers are added to it?

    Java Code:
    List<Integer> list = new ArrayList<>();
    list.add(2.4); // <--------------------compile time error
    
    // or 
    list.add(2); // works.
    int a = list.get(0);  // also works 
    
    // vs
    List<Number> list = new ArrayList<>();
    list.add(2); // so far, so good
    
    // then later
    int a = list.get(0); // <---- won't wont work. list not guaranteed to hold only integers.
    
    //or sometime later
    
    Number numb = list.get(0);
    double x = numb * 23; // won't work either.  What do I cast numb too?  What method of numb do I call?
    Regards,
    Jim
    Last edited by jim829; 04-02-2014 at 03:56 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 03-17-2014, 02:51 AM
  2. Replies: 2
    Last Post: 06-22-2013, 08:49 PM
  3. Replies: 2
    Last Post: 11-27-2011, 06:11 PM
  4. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM
  5. Replies: 0
    Last Post: 04-06-2008, 08:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •