wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please Help" Watch "Please Help" New topic
Author

Please Help

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277

Hi ,
Can someone please explain what is going on here
Q#1
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces �True�
Q#2
if( "String".startsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces �True �
I really don�t have any idea of output ?( why �True�?)
Regards
vivek

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Looking forward to hear from u guys.
I would realy appreciate it.
vivek
Ram Prasad
Greenhorn

Joined: Jul 18, 2000
Posts: 4
Hi Vivek,
I think the result is correct.
Just compile this code and see.
class javaprog {
public static void main(String [] ag)
{
//blank string
if("String".endsWith(" "))
System.out.println("True");
else
System.out.println("False");
}
}
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi
Thanks for your time and effort. I know it does print 'true' but my question is why???

please help me!
vivek
hemanshow
Greenhorn

Joined: Jun 26, 2000
Posts: 25
try:
if( "string".equals( "string" + "" ) ){
System.out.println( "wow, true" );
}else{
System.out.println( "see, false :-)" );
}
AND try ..........
if( "".equals( null ) ){
System.out.println( "wow, true" );
}else{
System.out.println( "see, false :-)" );
}

so every String object can have as many "" object at end.
NOTE : "" != null
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi hemanshow,
Thanku so much. i really appreciate it.
thanks
vivek
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
Here is a documented explanation from String API:
startsWith:
...
Note also that true will be returned if the argument is an empty string
...
endsWith:
...
Note that the result will be true if the argument is the empty string
...
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Thanks rajsim.
vivek
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help