We can do:
String string = \"ourstring\";
But we can\'t create objects like this for user defined classes:
UserClass uc=\"\";
""
is syntactic sugar for returning a String
object from the interned String pool.
Consider it as the exception rather than rule. Regular object assignments need to take the form
MyObject myObject = new MyObject();
For details on this topic, review the online Javadoc: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Strings are nice in Java. They get the benefits of being used like primitives but are internally Objects. There is an internal "interned String pool" that keeps track of String objects for you. This is done primarily for efficiency's sake but the abstraction is neat enough that you can pretend that a String is just a primitive like an int or a char.
Please remember to avoid creating a String manually using a constructor like you would most objects!
actually String string="ourstring" call the String Class default constructor new String(char value[]). if you can't see yet,advise you to read the String.clss
this is String.class descripe
/**
* The String
class represents character strings. All
* string literals in Java programs, such as "abc"
, are
* implemented as instances of this class.
*
* Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example: *
* String str = "abc"; *
* is equivalent to: *
* char data[] = {'a', 'b', 'c'}; * String str = new String(data); *
* Here are some more examples of how strings can be used: *
** System.out.println("abc"); * String cde = "cde"; * System.out.println("abc" + cde); * String c = "abc".substring(2,3); * String d = cde.substring(1, 2); *
*/
java.lang.String
is a special class.
Feel free to read http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
It says
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
...
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
No other classes have this special support from Java language.
You should be very careful with its +
feature: it is widely discussed as not safe for performance and memory.