• 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

using String/char in boolean statment

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
simple question from a simple mind: How do you use a String/char in a boolean statment? ie: if (userInput == 'N') {do something};
[This message has been edited by gaine chung (edited October 24, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use == for char comparison:
<pre>
char userInput = ...
if (userInput == 'N') { // do something }
</pre>
but use equals() when comparing String:
<pre>
String userInput = ...
if ("N".equals(userInput)) { // do something }
</pre>
The above form is safer than writing
<pre>userInput.equals("N")</pre>
because the latter will produce a NullPointerException if userInput is null.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
gaine chung
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was exactly what I was looking for. Thanks junilu!
- Gaine
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic