commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul J DeCoursey <p...@decoursey.net>
Subject Re: isBlank implementation question for educational purposes, please
Date Fri, 26 Jan 2007 13:11:59 GMT
It is still constructing a new object, which is expensive.

Tahir Akhtar wrote:
> You are right!!! It even says that in comments above constructor. 
>
>     public String substring(int beginIndex, int endIndex) {
> 	if (beginIndex < 0) {
> 	    throw new StringIndexOutOfBoundsException(beginIndex);
> 	}
> 	if (endIndex > count) {
> 	    throw new StringIndexOutOfBoundsException(endIndex);
> 	}
> 	if (beginIndex > endIndex) {
> 	    throw new StringIndexOutOfBoundsException(endIndex -
> beginIndex);
> 	}
> 	return ((beginIndex == 0) && (endIndex == count)) ? this :
> 	    new String(offset + beginIndex, endIndex - beginIndex, value);
>     }
>     // Package private constructor which shares value array for speed.
>     String(int offset, int count, char value[]) {
> 	this.value = value;
> 	this.offset = offset;
> 	this.count = count;
>     }
>
> -----Original Message-----
> From: Steffen Heil [mailto:lists@steffen-heil.de] 
> Sent: Friday, January 26, 2007 3:04 PM
> To: 'Jakarta Commons Users List'
> Subject: RE: isBlank implementation question for educational purposes,
> please
>
> Hi
>
>   
>> Actually his will be slower even if it's blank.  Strings are 
>> immutable (meaning they can't be changed), so under the hood 
>> the trim() method will have the overhead of creating a new 
>> String object.  Then it will have to cycle through each 
>> character in the original String to copy the non-trimmed 
>> characters into the new String instance.  So even if it was 
>> blank it has the overhead of checking every character just to 
>> copy them into the new String that it will return.
>>     
>
> No.
> substring does not copy Strings.
> It just returns new String objects, that refer to the original char[], but
> have different offset and length values.
>
> I haven't looked at the implementation right now, but I assume, it justs
> counts isSpace(s) from the start and from the end and calls substring.
> Therefor it's execution time should be linear to the amount of spaces at the
> beginning and end and be independant from the length of the rest of the
> String.
>
> Regards,
>   Steffen
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message