Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java String constructor logic

Tags:

java

string

I was trying to understand how a java String was implemented.The jdk7 source code below shows a check for originalValue.length > size .I cant figure out how/when it would come true.I tried to use eclipse debugger on some java String creation statements,but this check was never true.Is it possible to devise a String argument which would make this check true?

public final class String{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

     /**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        int size = original.count;
        char[] originalValue = original.value;
        char[] v;
        if (originalValue.length > size) {
            // The array representing the String is bigger than the new
            // String itself.  Perhaps this constructor is being called
            // in order to trim the baggage, so make a copy of the array.
            int off = original.offset;
            v = Arrays.copyOfRange(originalValue, off, off+size);
        } else {
            // The array representing the String is the same
            // size as the String, so no point in making a copy.
            v = originalValue;
        }
        this.offset = 0;
        this.count = size;
        this.value = v;
    }
...
}
like image 375
damon Avatar asked Aug 17 '13 05:08

damon


People also ask

Can we write logic in constructor in Java?

It is not even used outside the constructor. So in such cases constructor should not be used.

What is string constructor in Java?

The String(Char[]) constructor copies all the characters in the array to the new string. The String(Char[], Int32, Int32) constructor copies the characters from index startIndex to index startIndex + length - 1 to the new string. If length is zero, the value of the new string is String. Empty.

What is Parameterless constructor in Java?

When a constructor is declared without any parameter or argument, then it is called a parameter-less constructor. A parameter-less constructor works like a default constructor and this constructor can contain statements, or it can be empty.

Can we write business logic in constructor?

Constructors are not for business logic. If you're lumbered with a legacy constructor like this, discover the steps to follow to transform the class into clean code.


2 Answers

Have a look at this piece of code:

String s1 = "123456789";
String s2 = new String(s1.substring(0, 2));

The second constructor will match condition. The trick is in substring method. It does not make a real substring, but rather copies underlying array and just sets new boundaries to it. The idea of constructing a new string is to make a copy of a string, not just assign the same array. That`s actually why taking small substring from a big string might lead to OOM exception. Because to represent a small piece of information big array is used.

like image 162
Mikhail Avatar answered Oct 13 '22 00:10

Mikhail


You can debug this. Value represents the underlying char[]. count represents the view

 String s = new String("Hello   "); //value= [H, e, l, l, o, , , ]  count=8

 String os = s.trim();  //value= [H, e, l, l, o, , , ]  count=5
like image 37
rocketboy Avatar answered Oct 13 '22 01:10

rocketboy