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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With