Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 108

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Contact:

Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 108

Post by javiut »

Page 108 states that substring method of the String class the one with 2 parameters returns a NEW STRING and this not always true consider the following example.

Code: Select all

public class StringSubsString {
    public static void main(String[] args) {
        final String child = "JOHNSTEPHENORTIZRONDON";
        final String substring = child.substring(0,child.length());
        System.out.println(child==substring);/*PRINTS TRUE*/
    }    
}
This is returning the same object is just a String optimization.

Code: Select all

    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }
As you can see in the returning is saying if the beginning is the position 0 and the endIndex is the same of the end of the string the same String is returned.

Code: Select all

return ((beginIndex == 0) && (endIndex == value.length)) ? this
I think stating that a NEW STRING is returned is not true.

:cheers:

This same principle applies for the 1 parameter method.

this is also returning true.

Code: Select all

public class StringSubsString {
    public static void main(String[] args) {
        final String child = "JOHNSTEPHENORTIZRONDON";
        final String substring = child.substring(0);
        System.out.println(child==substring);/*PRINTS TRUE*/
    }    
}

Code: Select all

    public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

Code: Select all

return (beginIndex == 0) ? this

admin
Site Admin
Posts: 10398
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 108

Post by admin »

Yes, this fact is noted at the end of this section on pg 109:
One interesting thing about the methods detailed above is that they return the same string if there is no change in the string as a result of the operation.
May be it can be stated more clearly early on so that there is no confusion.

thank you for your feedback!

admin
Site Admin
Posts: 10398
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 108

Post by admin »

BTW, since you have given very valuable feedback, please send us your name. We will add it in the acknowledgements page of the book.

javiut
Posts: 37
Joined: Fri Oct 19, 2012 10:48 am
Contact:

Re: Error on OCP 1Z0-815 BOOK Deshmukh, Hanumant Page 108

Post by javiut »

Yes but i would be great if not stating that a substring is always returning a new String would be that returns a String or below a tip saying that if the index of the substring is 0 in the 1 parameter method or in the 2 parameters method the index is 0 and the endindex is the lenght of the string the same String is returned. Would be a honor for me My name is Cristian Daniel Ortiz Cuellar from Venezuela mylinkedin is https://www.linkedin.com/in/cristian-da ... -6ab4b253/ thanks mate. :joy:

Post Reply

Who is online

Users browsing this forum: clement_r, Google [Bot] and 8 guests