Bit tricky question for ==

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shauank
    New Member
    • Dec 2007
    • 1

    Bit tricky question for ==

    Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

    public class Reflexive {
    public static void main(String[] args) throws Exception {

    <typeX> x = <valueX>;

    System.out.prin tln(x == x);
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shauank
    Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

    public class Reflexive {
    public static void main(String[] args) throws Exception {

    <typeX> x = <valueX>;

    System.out.prin tln(x == x);
    }
    }
    <spoiler>
    Suppose x is NaN ...
    </spoiler>


    Edit: Shouldn't the println say
    System.out.prin tln(x == <valueX>);

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      <spoiler>
      Suppose x is NaN ...
      </spoiler>


      Edit: Shouldn't the println say
      System.out.prin tln(x == <valueX>);
      Nope, according to the IEEE definition NaN should compare unequal even to itself.
      NaN == NaN should result in false.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Nope, according to the IEEE definition NaN should compare unequal even to itself.
        NaN == NaN should result in false.

        kind regards,

        Jos
        That's why I suggested it for the println to print false but it wont print false with x == x.
        As in
        [CODE=java]Double x = Double.NaN;
        System.out.prin tln(x == x);
        System.out.prin tln(x == Double.NaN);
        [/CODE]
        prints
        true
        false

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by r035198x
          That's why I suggested it for the println to print false but it wont print false with x == x.
          As in
          [CODE=java]Double x = Double.NaN;
          System.out.prin tln(x == x);
          System.out.prin tln(x == Double.NaN);
          [/CODE]
          prints
          true
          false
          Very cheeky. Try:
          [CODE=java]double x = Double.NaN;
          System.out.prin tln(x == x);
          System.out.prin tln(x == Double.NaN);
          [/CODE]
          prints:
          false
          false

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by r035198x
            Edit: Shouldn't the println say
            System.out.prin tln(x == <valueX>);
            If you're going to do that, you could also play games with Integer pooling:

            Code:
            Integer x = Integer.valueOf(0);
            System.out.println(x == x);
            System.out.println(x == Integer.valueOf(0));
            
            Integer y = Integer.valueOf(5000);
            System.out.println(y == y);
            System.out.println(y == Integer.valueOf(5000));
            This prints:
            true
            true
            true
            false

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Bugger, that autoboxing thingie again ... where's the guy/gal who invented it? The
              window on the attic is wide open; please climb those fine stairs now and wave towards
              the general direction of the hungry peasants.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                ... where's the guy/gal who invented it? T...
                kind regards,

                Jos
                Probably enjoying their holiday in the Bahamas with the money he got for inventing it.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by r035198x
                  Probably enjoying their holiday in the Bahamas with the money he got for inventing it.
                  Do you remember this thread?

                  kind regards,

                  Jos

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JosAH
                    Do you remember this thread?

                    kind regards,

                    Jos
                    It came to my mind when I saw this thread.

                    Comment

                    • denismox
                      New Member
                      • Jan 2008
                      • 1

                      #11
                      shauank, I gave you this test for *you* to solve by yourself and did not ask for a public help, didn't I?

                      ALL: shauank is an applicant for a job ad and he received a Java Quiz test. Just another cheater, I guess.

                      Originally posted by shauank
                      Please finish the declaration of variable x in the bold line below so that the next line prints “false”.

                      public class Reflexive {
                      public static void main(String[] args) throws Exception {

                      <typeX> x = <valueX>;

                      System.out.prin tln(x == x);
                      }
                      }

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by denismox
                        shauank, I gave you this test for *you* to solve by yourself and did not ask for a public help, didn't I?

                        ALL: shauank is an applicant for a job ad and he received a Java Quiz test. Just another cheater, I guess.
                        Didn't I see this fellow posting several of these interview questions on Sun's Java forums in order to elicit solutions?

                        Comment

                        Working...