Tricky stuff

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #1

    Tricky stuff

    As everybody knows (or should know) the following is useless:

    [code=java]
    private static int x= 42;
    ...
    x= x++;
    [/code]
    The value of x will stay 42. Or does it? Have a look at the following little experiment
    and try to figure out what is happening:

    [code=java]
    public class TrickyStuff {

    private static int x= 42;

    public static void main(String[] args) {

    (new Thread() {
    public void run() {
    while(x == 42);
    System.out.prin tln( "x != 42" );
    System.exit(0);
    }
    } ).start();

    System.out.prin tln("x= "+x);
    while(true) { x=x++;}
    }
    }[/code]

    kind regards,

    Jos
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    It's because 42 is a special kind of integer, of course!
    ; )

    Here's another one:

    [CODE=java]public class TrickyStuff {

    public static void main(String[] args) {

    double a = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
    double b = 0.8;

    System.out.prin tln("Are a and b the same? "+(a==b));
    }
    }[/CODE]
    The output is "Are a and b the same? false". How is this possible?
    It's not for you Jos: I know you know the answer.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by prometheuzz
      It's not for you Jos: I know you know the answer.
      I don't want to participate in any contest that wants me to participate :-P

      kind regards,

      Jos (duh ;-)

      Comment

      • Tripmaker
        New Member
        • Jun 2007
        • 60

        #4
        Originally posted by prometheuzz
        It's because 42 is a special kind of integer, of course!
        ; )

        Here's another one:

        [CODE=java]public class TrickyStuff {

        public static void main(String[] args) {

        double a = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
        double b = 0.8;

        System.out.prin tln("Are a and b the same? "+(a==b));
        }
        }[/CODE]
        The output is "Are a and b the same? false". How is this possible?
        It's not for you Jos: I know you know the answer.

        I'm new at programming so I'm going to take a calculated guess. The answer for a = 0.7999999999999 999 . The reason being double belongs to Floating-point numbers which represent numbers with fractional precision. Double doesn't round off a number.

        Petra

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Tripmaker
          I'm new at programming so I'm going to take a calculated guess. The answer for a = 0.7999999999999 999 . The reason being double belongs to Floating-point numbers which represent numbers with fractional precision. Double doesn't round off a number.

          Petra
          Very close; bookmark this document and read it when you find some spare time.

          kind regards,

          Jos

          ps. now what about that x == 42 thingie in my original post?

          Comment

          • sumittyagi
            Recognized Expert New Member
            • Mar 2007
            • 202

            #6
            Originally posted by JosAH
            As everybody knows (or should know) the following is useless:

            [code=java]
            private static int x= 42;
            ...
            x= x++;
            [/code]
            The value of x will stay 42. Or does it? Have a look at the following little experiment
            and try to figure out what is happening:

            [code=java]
            public class TrickyStuff {

            private static int x= 42;

            public static void main(String[] args) {

            (new Thread() {
            public void run() {
            while(x == 42);
            System.out.prin tln( "x != 42" );
            System.exit(0);
            }
            } ).start();

            System.out.prin tln("x= "+x);
            while(true) { x=x++;}
            }
            }[/code]

            kind regards,

            Jos

            Why is it useless?
            x is not declared final, and thus is not a constant.

            Originally posted by prometheuzz
            It's because 42 is a special kind of integer, of course!
            ; )
            I didn't get this line. It would be great if you could explain this one.
            Thanks!

            Regards,
            Sumit Tyagi.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by sumittyagi
              Why is it useless?
              x is not declared final, and thus is not a constant.
              So you're not surprised by the output it shows?


              Originally posted by sumittyagi
              I didn't get this line. It would be great if you could explain this one.
              Thanks!
              42 is 54 using base 13.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by sumittyagi
                Why is it useless?
                x is not declared final, and thus is not a constant.



                I didn't get this line. It would be great if you could explain this one.
                Thanks!

                Regards,
                Sumit Tyagi.
                It's not a constant alright but when you do
                [CODE=java]while(true) {
                x = x++;
                }[/CODE]
                the value of x does not change since x will be assigned to the previous value always which will be the initial value. The post increment won't take any effect there. But when a thread is reading off the values of x ...

                Comment

                • Tripmaker
                  New Member
                  • Jun 2007
                  • 60

                  #9
                  Originally posted by JosAH
                  As everybody knows (or should know) the following is useless:

                  [code=java]
                  private static int x= 42;
                  ...
                  x= x++;
                  [/code]
                  The value of x will stay 42. Or does it? Have a look at the following little experiment
                  and try to figure out what is happening:

                  [code=java]
                  public class TrickyStuff {

                  private static int x= 42;

                  public static void main(String[] args) {

                  (new Thread() {
                  public void run() {
                  while(x == 42);
                  System.out.prin tln( "x != 42" );
                  System.exit(0);
                  }
                  } ).start();

                  System.out.prin tln("x= "+x);
                  while(true) { x=x++;}
                  }
                  }[/code]

                  kind regards,

                  Jos

                  Let see if I can get this right. The value stays at 42 because of the while statement. In laymans terms while x == 42 the system prints out x != 42 and the program is ended. Everytime you re-run the program you keep getting the same answer. You will never get to the second while statement.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Tripmaker
                    Let see if I can get this right. The value stays at 42 because of the while statement. In laymans terms while x == 42 the system prints out x != 42 and the program is ended. Everytime you re-run the program you keep getting the same answer. You will never get to the second while statement.
                    Put the program on your compiler and test it. Realize also that there are two threads running here.

                    Comment

                    • sumittyagi
                      Recognized Expert New Member
                      • Mar 2007
                      • 202

                      #11
                      Originally posted by JosAH
                      So you're not surprised by the output it shows?
                      Yea! I am not surprized by the output, because output is as I expected it to be.
                      ---------- java ----------
                      x= 42
                      x != 42
                      Normal Termination
                      Output completed (1 sec consumed).


                      Originally posted by JosAH
                      42 is 54 using base 13.
                      But how does that make a difference. I mean does this fact have any influence on the output.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by sumittyagi
                        Yea! I am not surprized by the output, because output is as I expected it to be.
                        ...
                        Would you then care to explain why the value of x changes (or seems to change)?

                        Comment

                        • sumittyagi
                          Recognized Expert New Member
                          • Mar 2007
                          • 202

                          #13
                          Originally posted by r035198x
                          It's not a constant alright but when you do
                          [CODE=java]while(true) {
                          x = x++;
                          }[/CODE]
                          the value of x does not change since x will be assigned to the previous value always which will be the initial value. The post increment won't take any effect there. But when a thread is reading off the values of x ...
                          x++ is having a side effect of incrementing the value of x.
                          so it will be solved as follows:
                          x = x++;
                          x = 42 (value replaced, so x becomes 42);
                          x++ (x++ have side effect of incrementing x, so now x becomes 43);

                          Comment

                          • prometheuzz
                            Recognized Expert New Member
                            • Apr 2007
                            • 197

                            #14
                            Originally posted by sumittyagi
                            Yea! I am not surprized by the output, because output is as I expected it to be.
                            ---------- java ----------
                            x= 42
                            x != 42
                            Normal Termination
                            Output completed (1 sec consumed).
                            Huh? Have we compiled and run the same code (from the original post)? I don't get that output!


                            Originally posted by sumittyagi
                            But how does that make a difference. I mean does this fact have any influence on the output.
                            That was a joke of me (42 being a special kind of integer).

                            Comment

                            • sumittyagi
                              Recognized Expert New Member
                              • Mar 2007
                              • 202

                              #15
                              Originally posted by prometheuzz
                              Huh? Have we compiled and run the same code (from the original post)? I don't get that output!
                              Then what output are u getting?

                              Originally posted by prometheuzz
                              That was a joke of me (42 being a special kind of integer).
                              There are a lots of things in java that make you bite your finger, so I never take any statement as joke.

                              But that was nice one!!! LOL.

                              Comment

                              Working...