In Java, you can create a String object as below :
String str = "abc"; &
String str = new String("abc");
Why cant a button object be created as :
Button bt = "abc"
Why is it compulsory to create a button object as:
Button bt = new Button("abc"); Why this is not compulsory in String's case?
Button bt1= "abc";
It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that you are NOT calling a java.lang.String constructor when you type
String s = "abc"; Why cant a button object be created as :
Button bt = "abc"
Why is it compulsory to create a button object as:
Button bt = new Button("abc"); Why this is not compulsory in String's case?
Button bt1= "abc";
It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that you are NOT calling a java.lang.String constructor when you type
For example
String x = "abc";
String y = "abc";
refer to the same object. While String x1 = new String("abc");
String x2 = new String("abc"); refer to two different objects.
String x2 = new String("abc"); refer to two different objects.
Comments
Post a Comment