commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Dubin" <b...@thedubins.com>
Subject RE: isBlank implementation question for educational purposes, please
Date Thu, 25 Jan 2007 21:57:18 GMT
Yes, I do want to learn and I appreciate your information.

-----Original Message-----
From: Aaron Freeman [mailto:aaron@sendthisfile.com] 
Sent: Thursday, January 25, 2007 3:53 PM
To: 'Jakarta Commons Users List'
Subject: RE: isBlank implementation question for educational purposes,
please

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.

Normally this would be a detail not worth pointing out since Paul's answer
was sufficent to show which is faster, but you did say you wanted to learn
the in's and out's. 

-a

> -----Original Message-----
> From: Paul J DeCoursey [mailto:paul@decoursey.net] 
> Sent: Thursday, January 25, 2007 3:00 PM
> To: Jakarta Commons Users List
> Subject: Re: isBlank implementation question for educational 
> purposes, please
> 
> His way will only be faster if the string is blank.  If the 
> string is not blank then the trim function will chew up 
> several processing cycles. 
> An example, say we pass in " dog"...
> 
> trim will trim off the first character and check to see if 
> there are more to trim on the beginning and then check the 
> end of the string.  The commons method will return false 
> after checking only two characters. 
> make sense?
> 
> Bill Dubin wrote:
> > Below are two implementations for the isBlank() method.  
> One is from 
> > the Commons Lang and the other was coded by a colleague of 
> mine and he 
> > claims his way is better and faster.
> >
> >  
> >
> > I would like to understand how or why his or the Commons 
> approach is better.
> > Maybe one is not better, but I still would like to have an 
> > understanding of how and/or why someone would choose one 
> way over the 
> > other so I can further my software development skillset.  
> Maybe there 
> > are ramifications one way or the other that I'm not seeing 
> and someone can shed some light on for me?
> >
> >  
> >
> > My only motivation for this question is to learn and 
> understand this 
> > stuff better.  Thank you for any explanation you can 
> provide!!  I do 
> > appreciate any assistance or lessons!!
> >
> >  
> >
> > //From Commons project
> >
> > public static boolean isBlank(String str) {
> >
> >         int strLen;
> >
> >         if (str == null || (strLen = str.length()) == 0) {
> >
> >             return true;
> >
> >         }
> >
> >         for (int i = 0; i < strLen; i++) {
> >
> >             if ((Character.isWhitespace(str.charAt(i)) == false)) {
> >
> >                 return false;
> >
> >             }
> >
> >         }
> >
> >         return true;
> >
> >  
> >
> >  
> >
> > Versus this way of doing the test:
> >
> >  
> >
> >  
> >
> >  
> >
> > //Modified with faster logic according to one of our developers
> >
> > public static boolean isBlank(String str){
> >
> >       int strLen;
> >
> >       if ((str == null) || ((strLen = str.length()) == 0))  {
> >
> >             return true;
> >
> >       }
> >
> >       else  {
> >
> >       str = str.trim();
> >
> >       if (str.length() == 0)  {
> >
> >                   return true;
> >
> >             }
> >
> >             else        {
> >
> >                   return false;
> >
> >             }
> >
> >       }
> >
> > }
> >
> >  
> >
> >
> >   
> 
> 
> ---------------------------------------------------------------------
> 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


---------------------------------------------------------------------
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