wonggr
wonggr

Reputation: 519

String to Object but not Object to String?

I can't really understand why this line of code doesn't compile:

String str = new Object();

while the following does:

Object o = new String("Hello");

To my understand, String, like every other class, extends Object. So why doesn't the first line compile?

Upvotes: 6

Views: 1167

Answers (2)

Victor2748
Victor2748

Reputation: 4199

Because String is an Object, but Object is not a String, just like every orange is a fruit, but not every fruit is an orange.

String is a class that extends Object, so you can simply write:

Object obj = "string";

But Object does not extend String, so:

String str = new Object();

won't compile.

Though, if you have an Object, and it is a String, you can do something that is called type casting or simply casting:

String str = (String) myObject;

But it might throw a ClassCastException if myObject is not originally type of String

Here, you can find more information on Object Casting in Java: http://www.javabeginner.com/learn-java/java-object-typecasting

Upvotes: 23

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

For this sort of thing it is useful to distinguish between the type of a variable, established in its declaration, and the class of an object, established when the object is created.

A variable is allowed to be null, or to point to any object whose class matches its type, or is a direct or indirect subclass of that class. Because String is a subclass of Object, a variable of type Object can point to an object of class String.

In your example, the compiler knows that a String expression must be null or point to a String object, so Object o = new String("Hello"); is known at compile time to be correct.

On the other hand, an expression or variable of type Object may reference a String, but it may reference something entirely different. The compiler requires an explicit cast to say the conversion is OK.

public class Test {
  public static void main(String[] args) {
    Object stringObject = "xyzzy";
    Object integerObject = new Integer(3);
    String test1 = (String)stringObject;
    System.out.println(test1);
    String test2 = (String) integerObject;
    System.out.println(test2);
  }
}

This program compiles because I've told the compiler the conversions are OK with my casts. It runs through the first println call, because stringObject really does point to a String. It fails with a ClassCastException on the line String test2 = (String) integerObject;. As far as the compiler knows, integerObject might point to a String, so it accepted my cast. At run time, the JVM finds it really points to an Integer and throws the exception.

The use of type Object for integerObject is significant. That makes String test2 = (String) integerObject; conversion of a type Object expression to type String, a conversion that might succeed, depending on what integerObject actually references. If integerObject had declared type Integer the compiler would have rejected the program. There is no object that can be referenced by both Integer and String expressions.

Upvotes: 4

Related Questions