3

I am kinda new in java. I'm reading a chapter in a book about Comparable and Comparator interfaces. With the code beneath, it says "Find a way to modify that last line of code to make it work". Currently, it doesn't compile. What should be done with the last line ?

Hello Friend= new Hello("LOL");
Object myObj = Friend;
Hello myFriend = myObj; // What's wrong with this line ?
James P.
  • 17,929
  • 27
  • 89
  • 147
Limbo
  • 81
  • 4
  • Image you put `myObj = 5;` before that, which you can because it's defined as `Object`. What would `Hello myFriend = 5` mean? – Jeroen Vannevel May 10 '14 at 00:24
  • What do you mean by "equate two objects?" – Robert Harvey May 10 '14 at 00:26
  • In short, how could I modify that last line of code to make it work ? – Limbo May 10 '14 at 00:27
  • Type mismatch: cannot convert from Object to Hello – Erran Morad May 10 '14 at 00:27
  • 1
    http://stackoverflow.com/questions/2672645/casting-and-dynamic-vs-static-type-in-java/2672656#2672656 – user2864740 May 10 '14 at 00:29
  • @user3622264 Let's develop this question. Why do you want the last line to work ? – James P. May 10 '14 at 00:33
  • umm I can't really compile it because it is a question from a book that I was reading. It says "Find a way to modify that last line of code to make it work". It's a chapter on Comparable and Comparator interfaces; which I clearly do not know much about. – Limbo May 10 '14 at 00:34
  • Right. Does Friend really have a capital F like that ? It shouldn't if it's a variable name. – James P. May 10 '14 at 00:37
  • What the code is doing is creating a new Hello, putting it into an Object (topmost type in Java) and then attempting to put the Object into Hello. – James P. May 10 '14 at 00:39
  • Question improved and voila, one more question having a chance of getting decent answers. – James P. May 10 '14 at 00:41

4 Answers4

3
Hello Friend= new Hello("LOL");

Now you have variable named Friend of compile-time type Hello referring to an object of runtime type Hello

Object myObj = Friend;

Now you have a variable named myObj of compile-time type Object also referring to the same object of runtime type Hello. This is allowed because every Hello is also an Object.

Hello myFriend = myObj; // What's wrong with this line ?

This fails during compilation because myObj has the compile-time type Object and you're trying to assign it to a new variable of compile-time type Hello. And most Objects are not Hellos. The compiler doesn't try to figure out whether this particular one is, because that is not possible in general.

But you can force the compiler to do it anyway by assuring you that this particular Objects is in fact a Hello:

Hello myFriend = (Hello) myObj;

This is called casting.

Michael Borgwardt
  • 327,225
  • 74
  • 458
  • 699
1

Copy from comments:

Image you put myObj = 5; before that, which you can because it's defined as Object. What would Hello myFriend = 5 mean?

The problem you get ("incompatible types") is because you are trying to assign a variable defined as Object (up top in the hierarchy) to a variable defined as Hello (bottom in the hierarchy). In your situation it might work but the compiler forbids this because it has no (feasible) way of knowing what happens in between or after.

This is also a valid statement:

Object myObj = Friend;
myObj = 5;

But assigning 5 to a variable of type Hello will obviously not work. That's why it forbids this alltogether.

You can circumvent this by explicitly casting (Hello myFriend = (Hello) myObj;) but this will throw a runtime exception instead if it is an impossible cast.

Jeroen Vannevel
  • 41,258
  • 21
  • 92
  • 157
0

I think you mean assign, not equate. The line fails because the operator is chosen by the compile time type. You cannot assign Object to Hello because then you could bind an instance of a class like GoodBye to a reference of type Hello.

This is possible in other languages that are not statically typed (because the types at runtime are compatible), but not in Java.

Axel
  • 13,204
  • 4
  • 44
  • 72
  • Indeed. Java is strong-typed as opposed to PHP which is weak-typed. – James P. May 10 '14 at 00:31
  • When casting, you have a `Hello` on both left and right-hand-side, so technically you don't assign reference to `Object` any more... ;-) – Axel May 10 '14 at 00:35
0

You've "erased" the type of Friend,

// The root of the Java class hierarchy is Object, that mean that 
Object myObj = Friend; // <-- (Object) can refer to anything! String, Double, Hello. :)
                       // Or, every instance of anything is-a java.lang.Object.
Hello myFriend = (Hello) myObj; // minimum

What you should do is something with instanceof,

Object myObj = Friend;
if (myObj instanceof Hello) { // <-- runtime type checking. 
  myFriend = (Hello) myObj;
}

or Class#isAssignableFrom(Class) -

Object myObj = Friend;
if (Hello.class.isAssignableFrom(myObj)) { // <-- runtime type checking. 
  myFriend = Hello.class.cast(myObj); // <-- you can also cast with this.
}
Community
  • 1
  • 1
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226