In Java, You can create a String object as:  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 is this not compulsory in String's case?

The main reason you cannot create a button by Button bt1= "abc"; is because "abc" is a literal string (something slightly different than a String object, bytheway)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 note that you are NOT calling a java.lang.String constuctor when you type String s = "abc";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.

Showing Answers 1 - 1 of 1 Answers

sir

  string x="abc"

 string y ="abc"  i dont think that here they refer to same object.I have refer the complete reference..automatically when u asign stringz like this ..different objects will b created.so give me answer gain indetail

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions