Results 1 to 6 of 6
- 07-26-2012, 09:39 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Difference between again == "y"; and again.equals("y");
Hey all,
This is my first post, so be gentle :P
So, I was following a tutorial and it prompted me to make a grade telling machine based on numeric input from the keyboard from java.util.Scanner
import java.util.*;
import java.io.*;
public class GradeCalculator {
public static void main(String[] args){
String again = "y";
while (again.equals("y")){
System.out.println("What is your numeric grade? ");
Scanner kbReader = new Scanner(System.in);
double grade = kbReader.nextDouble();
if (grade == 100){
System.out.println("PERFECT SCORE!\nWant to input another grade? ");
again = kbReader.next();
System.out.println(again == "y");
System.out.println(again.equals("y"));
} else if (grade>90 && grade < 100){
System.out.println("you get an A\nWant to input another grade? ");
again = kbReader.next();
when I ran it and entered 100 as the numeric grade, System printed this:
What is your numeric grade?
100
PERFECT SCORE!
Want to input another grade?
y
false
true
What is your numeric grade?
Why does again == "y"; return false?
any thoughts would be appreciated.
- 07-26-2012, 09:42 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Difference between again == "y"; and again.equals("y");
- 07-26-2012, 09:57 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Re: Difference between again == "y"; and again.equals("y");
Thanks, eRaaaa,
That helped but that leaves me confused about the function of the == operator.
The site says:
This is true only if name is a reference to the same object that "Mickey Mouse" refers to. This will be false if the String in name was read from input or computed (by putting strings together or taking the substring), even though name really does have exactly those characters in it.
the second sentence explains my situation, but in the first sentence, what does it mean to reference the same object that "Mickey Mouse" refers to?
Thanks!
- 07-26-2012, 10:05 PM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Difference between again == "y"; and again.equals("y");
2 variables have to literally reference the exact same object. For example:
In this case, button2 references the same object that is referenced by button1. There is only 1 JButton, as opposed to this example:Java Code:JButton button1 = new JButton(); JButton button2 = button1;
where 2 JButtons are created. Although the 2 JButtons have the same characteristics, they are still 2 separate objects.Java Code:JButton button1 = new JButton(); JButton button2 = new JButton();
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-27-2012, 09:40 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Difference between again == "y"; and again.equals("y");
This will result in a String object in the String literal pool, referenced by the variable 'mm'.Java Code:String mm = "Mickey Mouse";
This will give the variable 'nn' the same value as 'mm'. That value is the reference to the object in the literal pool.Java Code:String nn = mm;
This will, at run time, create a new String object on the heap and 'oo' will reference it.Java Code:String oo = new String("Mickey Mouse");
nn == mm as the references are the same.
oo != mm as the references are different.Please do not ask for code as refusal often offends.
- 07-27-2012, 06:35 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Handy conditional If code to handle "day" and "days". Needs a slight tweak.
By hamster in forum New To JavaReplies: 5Last Post: 04-29-2012, 07:18 AM -
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
What is the difference between an "Entity" and "Value Object"?
By vahini in forum Advanced JavaReplies: 4Last Post: 06-14-2011, 04:59 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks