what's the difference between the two initialization:
Object x = new String();
String x = new String();
in java
thank!
what's the difference between the two initialization:
Object x = new String();
String x = new String();
in java
thank!
Object x = new String(); // pointing to a String and saying - Hey, Look there! Its an Object
String x = new String();// pointing to a String and saying - Hey, Look there! Its a String
More importantly : The methods of String that can be accessed depend on the reference. For example :
public static void main(String[] args) {
Object o = new String();
String s = new String();
o.split("\\."); // compile time error
s.split("\\."); // works fine
}
There's no difference in the initializations, only in the declarations and therefore in the way the rest of your code sees the variables type.
Object x = new String();
here, x
has access only to the Object
's methods and members.
(To use String
members, you have to typecast the x
to String
(using Downcasting in Java))
String x = new String();
here, x
has access to all the methods and members of Object
as well as String
.
The difference is that in the first option x
will be considered by the compiler as an Object and in the second it'll be considered as a String. Example:
public static void main (String[] args) throws Exception {
Object x = new String();
test(x);
String y = new String();
test(y);
// and you can also "trick" the compiler by doing
test((String)x);
test((Object)y);
}
public static void test(String s) {
System.out.println("in string");
}
public static void test(Object o) {
System.out.println("in object");
}
will print:
in object
in string
in string
in object
Both are same, X will refer to the string object.
But x variable in Object x = new String();
need to be Type casted to String , using x.toString()
or (String)x
before making use of it.
As noted by the other answerers, both cases will result in the variable x holding a reference to a String. The only difference is how subsequent code will see the reference: as an Object vs String, i.e. determining which operations the compiler will allow on the reference.
Essentially, the question highlights the difference between a statically typed language (e.g. Java) and a dynamic one (e.g. Python, Javascript, etc.). In the latter you don't have to declare the type of the reference, so the code for this is simply something like:
var x = new String();
In this case, the compiler infers the type at runtime, but you lose some static type checking at compile time.
In everyday Java code you will always use the form:
String x = new String();
since that will allow you to treat x like a String and call, e.g. the method toUpperCase()
on it. If x were an Object
, the compiler won't allow you to call that method, only Object methods, e.g. equals()
.
However, the exact opposite is true in the following case:
List list = new ArrayList();
ArrayList list = new ArrayList();
Unless you specifically want to use methods exposed by ArrayList (unlikely), it is always better to declare list
as a List
, not ArrayList
, since that will allow you to change the implementation to another type of List later, without changing the calling code.
Difference is you need to cast it a lot however you can use the same variable and reinitialize it with another type. It is very usefull when you are working with changing variables..
Example:
Object x = new String();
if(x instanceof String){
System.out.println("String");
}
x = new ArrayList<String>();
if(x instanceof ArrayList){
System.out.println("ArrayList");
}
returns:
String ArrayList