• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a command line argument, a comparison, an interesting result

 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following program compiles

I ran the program with the command "java StringTest /?" at the DOS prompt
The output was:
false
/?
/?
Can someone explain this?
I want to know if a user uses the program with the parameter /?
 
Asuthosh Borikar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try s.equals("/?") in place of s=="/?".
The "==" operator tests if the two object references point to the same object, which is not true in this case.
String class overrides the "equals" method in the Object class,
to compare the values of the two strings in question and return true if they are the same. It DOES NOT check to see if both the strings point to the same object in memory.
 
Carlos Failde
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes of course. Much thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic