Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Object but not Object to String?

Tags:

java

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?

like image 640
wonggr Avatar asked Dec 15 '14 00:12

wonggr


People also ask

How do you change a String object into an object?

We can convert String to Object in java with assignment operator. Each class is internally a child class of Object class. So you can assign string to Object directly. You can also convert String to Class type object using Class.

Is String not an object?

A string is an object of type String whose value is text.

Can an object name be a String?

Unassigned properties of an object are undefined (and not null ). JavaScript object property names (keys) can only be strings or Symbols — all keys in the square bracket notation are converted to strings unless they are Symbols.

Is a String considered an object?

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.


2 Answers

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

like image 115
Victor2748 Avatar answered Oct 09 '22 11:10

Victor2748


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.

like image 44
Patricia Shanahan Avatar answered Oct 09 '22 10:10

Patricia Shanahan