Java string declarations

Status
Not open for further replies.

Atreyo Bhattacharjee

Commendable
Feb 7, 2017
207
0
1,690
Just out of curiosity, why can we declare Strings in Java like:

Java:
String str = "Cake";

If String is a class, and to make an object with all other classes we need to write something like:

Java:
Object muffin = new Object();
 
Solution
Because when they wrote the specs, they wanted to define String constants even though String is not a primitive. Nobody would want to use Java if you had to write
String str = new String(new char[] {'C','a','k,'e'});

Brian28

Distinguished
Jan 28, 2016
48
15
18,565
Because when they wrote the specs, they wanted to define String constants even though String is not a primitive. Nobody would want to use Java if you had to write
String str = new String(new char[] {'C','a','k,'e'});
 
Solution
Status
Not open for further replies.