Java Strings: “String s = new String(”silly“);”

前端 未结 23 2395

I\'m a C++ guy learning Java. I\'m reading Effective Java and something confused me. It says never to write code like this:

String s = new String(\"silly\");         


        
相关标签:
23条回答
  • 2020-11-22 14:42

    String is a special built-in class of the language. It is for the String class only in which you should avoid saying

    String s = new String("Polish");
    

    Because the literal "Polish" is already of type String, and you're creating an extra unnecessary object. For any other class, saying

    CaseInsensitiveString cis = new CaseInsensitiveString("Polish");
    

    is the correct (and only, in this case) thing to do.

    0 讨论(0)
  • 2020-11-22 14:43

    - How do i make CaseInsensitiveString behave like String so the above statement is ok (with and w/out extending String)? What is it about String that makes it ok to just be able to pass it a literal like that? From my understanding there is no "copy constructor" concept in Java right?

    Enough has been said from the first point. "Polish" is an string literal and cannot be assigned to the CaseInsentiviveString class.

    Now about the second point

    Although you can't create new literals, you can follow the first item of that book for a "similar" approach so the following statements are true:

        // Lets test the insensitiveness
        CaseInsensitiveString cis5 = CaseInsensitiveString.valueOf("sOmEtHiNg");
        CaseInsensitiveString cis6 = CaseInsensitiveString.valueOf("SoMeThInG");
    
        assert cis5 == cis6;
        assert cis5.equals(cis6);
    

    Here's the code.

    C:\oreyes\samples\java\insensitive>type CaseInsensitiveString.java
    import java.util.Map;
    import java.util.HashMap;
    
    public final class CaseInsensitiveString  {
    
    
        private static final Map<String,CaseInsensitiveString> innerPool 
                                    = new HashMap<String,CaseInsensitiveString>();
    
        private final String s;
    
    
        // Effective Java Item 1: Consider providing static factory methods instead of constructors
        public static CaseInsensitiveString valueOf( String s ) {
    
            if ( s == null ) {
                return null;
            }
            String value = s.toLowerCase();
    
            if ( !CaseInsensitiveString.innerPool.containsKey( value ) ) {
                 CaseInsensitiveString.innerPool.put( value , new CaseInsensitiveString( value ) );
             }
    
             return CaseInsensitiveString.innerPool.get( value );   
        }
    
        // Class constructor: This creates a new instance each time it is invoked.
        public CaseInsensitiveString(String s){
            if (s == null) {
                throw new NullPointerException();
             }         
             this.s = s.toLowerCase();
        }
    
        public boolean equals( Object other ) {
             if ( other instanceof CaseInsensitiveString ) {
                  CaseInsensitiveString otherInstance = ( CaseInsensitiveString ) other;
                 return this.s.equals( otherInstance.s );
             }
    
             return false;
        }
    
    
        public int hashCode(){
             return this.s.hashCode();
        }
    

    // Test the class using the "assert" keyword

        public static void main( String [] args ) {
    
            // Creating two different objects as in new String("Polish") == new String("Polish") is false
            CaseInsensitiveString cis1 = new CaseInsensitiveString("Polish");
            CaseInsensitiveString cis2 = new CaseInsensitiveString("Polish");
    
            // references cis1 and cis2 points to differents objects.
            // so the following is true
            assert cis1 !=  cis2;      // Yes they're different
            assert cis1.equals(cis2);  // Yes they're equals thanks to the equals method
    
            // Now let's try the valueOf idiom
            CaseInsensitiveString cis3 = CaseInsensitiveString.valueOf("Polish");
            CaseInsensitiveString cis4 = CaseInsensitiveString.valueOf("Polish");
    
            // References cis3 and cis4 points to same  object.
            // so the following is true
            assert cis3 == cis4;      // Yes they point to the same object
            assert cis3.equals(cis4); // and still equals.
    
            // Lets test the insensitiveness
            CaseInsensitiveString cis5 = CaseInsensitiveString.valueOf("sOmEtHiNg");
            CaseInsensitiveString cis6 = CaseInsensitiveString.valueOf("SoMeThInG");
    
            assert cis5 == cis6;
            assert cis5.equals(cis6);
    
            // Futhermore
            CaseInsensitiveString cis7 = CaseInsensitiveString.valueOf("SomethinG");
            CaseInsensitiveString cis8 = CaseInsensitiveString.valueOf("someThing");
    
            assert cis8 == cis5 && cis7 == cis6;
            assert cis7.equals(cis5) && cis6.equals(cis8);
        }
    
    }
    
    C:\oreyes\samples\java\insensitive>javac CaseInsensitiveString.java
    
    
    C:\oreyes\samples\java\insensitive>java -ea CaseInsensitiveString
    
    C:\oreyes\samples\java\insensitive>
    

    That is, create an internal pool of CaseInsensitiveString objects, and return the corrensponding instance from there.

    This way the "==" operator returns true for two objects references representing the same value.

    This is useful when similar objects are used very frequently and creating cost is expensive.

    The string class documentation states that the class uses an internal pool

    The class is not complete, some interesting issues arises when we try to walk the contents of the object at implementing the CharSequence interface, but this code is good enough to show how that item in the Book could be applied.

    It is important to notice that by using the internalPool object, the references are not released and thus not garbage collectible, and that may become an issue if a lot of objects are created.

    It works for the String class because it is used intensively and the pool is constituted of "interned" object only.

    It works well for the Boolean class too, because there are only two possible values.

    And finally that's also the reason why valueOf(int) in class Integer is limited to -128 to 127 int values.

    0 讨论(0)
  • 2020-11-22 14:43

    Java creates a String object for each string literal you use in your code. Any time "" is used, it is the same as calling new String().

    Strings are complex data that just "act" like primitive data. String literals are actually objects even though we pretend they're primitive literals, like 6, 6.0, 'c', etc. So the String "literal" "text" returns a new String object with value char[] value = {'t','e','x','t}. Therefore, calling

    new String("text"); 
    

    is actually akin to calling

    new String(new String(new char[]{'t','e','x','t'}));
    

    Hopefully from here, you can see why your textbook considers this redundant.

    For reference, here is the implementation of String: http://www.docjar.com/html/api/java/lang/String.java.html

    It's a fun read and might inspire some insight. It's also great for beginners to read and try to understand, as the code demonstrates very professional and convention-compliant code.

    Another good reference is the Java tutorial on Strings: http://docs.oracle.com/javase/tutorial/java/data/strings.html

    0 讨论(0)
  • 2020-11-22 14:44

    I believe the main benefit of using the literal form (ie, "foo" rather than new String("foo")) is that all String literals are 'interned' by the VM. In other words it is added to a pool such that any other code that creates the same string will use the pooled String rather than creating a new instance.

    To illustrate, the following code will print true for the first line, but false for the second:

    System.out.println("foo" == "foo");
    System.out.println(new String("bar") == new String("bar"));
    
    0 讨论(0)
  • 2020-11-22 14:46

    Strings are special in Java - they're immutable, and string constants are automatically turned into String objects.

    There's no way for your SomeStringClass cis = "value" example to apply to any other class.

    Nor can you extend String, because it's declared as final, meaning no sub-classing is allowed.

    0 讨论(0)
  • 2020-11-22 14:46

    CaseInsensitiveString and String are different objects. You can't do:

    CaseInsensitiveString cis = "Polish";
    

    because "Polish" is a String, not a CaseInsensitiveString. If String extended CaseInsensitiveString String then you'd be OK, but obviously it doesn't.

    And don't worry about the construction here, you won't be making unecessary objects. If you look at the code of the constructor, all it's doing is storing a reference to the string you passed in. Nothing extra is being created.

    In the String s = new String("foobar") case it's doing something different. You are first creating the literal string "foobar", then creating a copy of it by constructing a new string out of it. There's no need to create that copy.

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