• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equalsIgnoreCase()...Strange

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note the line # 7. It prints true with argument as temp, but generates a compiler error if I modify it as;
System.out.println(s.equalsIgnoreCase("abc123"));
Anybody who can help me???
1. class StringFunctions
2. {
3. public static void main(String args[])
4. {
5. String s = new String("ABC123");
6. String temp = "abc123"; 7.System.out.println(s.equalsIngnoreCase(temp));
// System.out.prinltn(s.equalsIgnoreCase("abc123")); //generates error
8. }
9. }
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The argument for the equalsIgnoreCase expects a String object which is out on the heap. If you feed it "abc123" you are feeding it a String literal, which is in the Constant Pool. As you can tell, the compiler handles the two slightly differently.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you pass a string object or a string literal the code compiles perfectly well as it should. Cindy, by the way have you tried to compile it??
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a typo on line 7. prinltn instead of println
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanveer Mehmood:
Please note the line # 7. It prints true with argument as temp, but generates a compiler error if I modify it as;
System.out.println(s.equalsIgnoreCase("abc123"));
Anybody who can help me???
1. class StringFunctions
2. {
3. public static void main(String args[])
4. {
5. String s = new String("ABC123");
6. String temp = "abc123"; 7.System.out.println(s.equalsIngnoreCase(temp));
// System.out.prinltn(s.equalsIgnoreCase("abc123")); //generates error
8. }
9. }


Hi Tanveer
I have compiled the program n its running correctly. The result is:
true
true
But there r two spelling mistakes in the code which u have posted.
. In line 7 instead of writing equalsIgnoreCase, u have written equalsIngnoreCase.
. In the commented line, u have written System.out.prinltn, instead of System.out.println
Other than that the program is running fine.....
Regards
@li
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic