• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects question  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this is going back to the very basics but how this is valid




where as this is not



PS ignore the parameters,Integer extends from Number is the point I'm trying to get across.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:Ok so this is going back to the very basics but how this is valid



This line is not valid, as you should get a syntax error.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:where as this is not


PS ignore the parameters,Integer extends from Number is the point I'm trying to get across.


This line is not valid, as the Number class doesn't have a constructor that takes a string ... and ... a Number instance is not IS-A Integer.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because all Integers are Numbers, but not all Numbers are Integers. Besides, Number is an abstract class, so you can not directly create a new instance of it.

If you could assign a less specific type to a variable of a more specific type, you would be able to do the following:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I thought you meant about the number of quote marks.
Go and have a look at the subclasses of Number; there are about 14 of them. If you declare something as Number, it might be an Integer, a Double, a BigDecimal or a few other things. So Number n might be new Integer(...) or new Integer(...) or new Double(...) or new BigDecimal(...) or new SomethingElse(...). You might even be able to say Number n = 123; and get an INteger object by boxing, but I don't think I have ever tried it.
Now, when we get to your second example, you are declaring the variable as an Integer and then assigning a Number to it. How is the compiler to know that Number instance will be an Integer? The compiler cannot do what you can do and eyeball the code and see that the value you are passing is an integer (small i)? Anyway, the rules of the language don't require it to make such discrimination. It has to see that the type on the right of the = operator might be a different type from the declared type, and forbid that. That is because Java® is a strongly and statically type language. Strong typing means that the type of each object must match the type of all places where it is used. Otherwise you can get horrible runtime errors because the type is incorrect. Static typing means that types are checked at compile time, so you can be confident of their correctness at runtime.

Anyway, you have chosen a bad example for two reasons:-
  • 1: It would be better to use the valueOf methods of most of the Number classes in preference to the new operator because you may get better memory consumption.
  • 2: Number is an abstract class, so you can't simply write new Number(123) and have it compile. It doesn't even have a constructor taking such a parameter.
  •  
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's imagine you have a class Dog and that has subclasses JackRussell, Airedale, Rottweiler, etc. Now what if you say this sort of thing:-Yes, a Dog might be a Jack Russell. So that is correctly typed. But what if you turn it round?How is the compiler to know that jr is going to be a JackRussell? How do you know it isn't a Rottweiler? Or some other breed of dog not specified here, possibly a Collie? Maybe I shou‍ld have used a Swine class with GloucesterOldSpot and Tamworth subclasses. Then I could have said the compiler won't let you sell it a pig in a poke. If the compiler isn't sure that your JackRussell really is a JackRussell, it cannot say you have got correct typing and it will tell you to think again about which class you are declaring and which you are instantiating. Remember for safe typing, you must have the correct types shown to the compiler.Now, you have reassured the compiler that you really have a JackRussell, and as long as there really is such a thing as a JackRussell which is a Dog, you can get away with it. The compiler might not believe in Father Christmas any more, but when you cast things, it does “believe” you know what you are doing . . .
     
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:. . . The compiler . . . “believe[s]” you know what you are doing . . .
    The runtime is more difficult to fool; it will throw exceptions if that cast isn't correct. No, sorry when that cast isn't correct.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!