Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By sandman18and5

Thread: Difference between again == "y"; and again.equals("y");

  1. #1
    sandman18and5 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Difference between again == "y"; and again.equals("y");


  3. #3
    sandman18and5 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default 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!

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Difference between again == "y"; and again.equals("y");

    Quote Originally Posted by sandman18and5 View Post
    what does it mean to reference the same object that "Mickey Mouse" refers to?
    2 variables have to literally reference the exact same object. For example:

    Java Code:
    JButton button1 = new JButton();
    JButton button2 = button1;
    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 = new JButton();
    where 2 JButtons are created. Although the 2 JButtons have the same characteristics, they are still 2 separate objects.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Difference between again == "y"; and again.equals("y");

    Quote Originally Posted by sandman18and5 View Post
    what does it mean to reference the same object that "Mickey Mouse" refers to?

    Thanks!
    Java Code:
    String mm = "Mickey Mouse";
    This will result in a String object in the String literal pool, referenced by the variable 'mm'.

    Java Code:
    String nn = mm;
    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 oo = new String("Mickey Mouse");
    This will, at run time, create a new String object on the heap and 'oo' will reference it.
    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.

    ** This space for rent **

  6. #6
    sandman18and5 is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: Difference between again == "y"; and again.equals("y");

    Thanks guys, that was really helpful!
    awinston likes this.

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2012, 07:18 AM
  2. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  3. Replies: 4
    Last Post: 06-14-2011, 04:59 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •