How string class difference from other classes?

后端 未结 4 674
不思量自难忘°
不思量自难忘° 2021-01-21 07:37

We can do:

String string = \"ourstring\";

But we can\'t create objects like this for user defined classes:

UserClass uc=\"\";

相关标签:
4条回答
  • 2021-01-21 08:15

    "" 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();
    
    0 讨论(0)
  • 2021-01-21 08:31

    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!

    0 讨论(0)
  • 2021-01-21 08:34

    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); 
     * 
    *

    */

    0 讨论(0)
  • 2021-01-21 08:35

    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.

    0 讨论(0)
提交回复
热议问题