473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Java Equality

23
I am new to Java, and I am really confused by equality. Here is my current code:

Expand|Select|Wrap|Line Numbers
  1. String dm = JOptionPane.showInputDialog("Yes or no?");  
  2. if(dm=="yes") 
  3. {
  4.    System.out.println("You chose yes.");
  5. }
  6. else
  7. {
  8.    System.out.println("You chose no.");
  9. }
  10.  
Even if I type in yes at the prompt, it always prints "You chose no". Is there something I don't understand about equality comparisons in Java? Please give me a modified version of my code that will work.

Thanks,
Anon
Sep 16 '07 #1
2 1382
JosAH
11,448 Expert 8TB
I am new to Java, and I am really confused by equality. Even if I type in yes at the prompt, it always prints "You chose no". Is there something I don't understand about equality comparisons in Java? Please give me a modified version of my code that will work.

Thanks,
Anon
In Java all variables that are 'objects' (i.e. an instance of a class) are actually
just pointers to the real objects. Primitives such as ints, doubles etc. are stored
in the variable themselves.

Suppose you and I both point to a String "yes". Most likely I'm pointing to another
instance of a "yes" String than you are. Are we both pointing to the same
thing? I'd say no; that's exactly what the == operator does. It checks whether
or not both pointers are equal, i.e. if they point to the same thing.

What you want is to check whether or not those two separate things *represent*
the same thing. This ----> "yes" is another one than that ----> "yes", but they
both represent the same String wih content/value "yes".

Checking whether or not two different things represent the same value, you
should use the equals() method.

For primitives you can use the == operator again if you want to check them for
equality, e.g. 42 == 42 is true.

kind regards,

Jos
Sep 16 '07 #2
anon538
23
Thanks, it works perfectly.
Sep 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Maxi | last post by:
I have this SQL statement and I need to change the WHERE clause in the SQL statement to its java equivatlent. Something like this: @command1, @username2 ,@receiver is input parameter WHERE (...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
19
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.