Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    XdaX is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Integer negation does not give the desired result.

    Hey all,

    the following code
    Java Code:
    	public static void main(String[] args) {
    		System.out.println(50 - 050);
    	}
    does not give the desired result
    Java Code:
    0
    but gives
    Java Code:
    10
    instead.
    Any ideas?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    050 is the octal representation of the number 5*8+0*1 == 40. That explains the result.

    kind regards,

    Jos

  3. #3
    f0ns is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    4
    Rep Power
    0

    Default

    you beat me to it!

  4. #4
    XdaX is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Oh thanks,

    This explains a lot :)

    Strange that Java uses a leading 0 instead of an o to express octals. This leads in my opnion to the wrong result because leading 0's in decimal numbers should just be discarded.

  5. #5
    f0ns is offline Member
    Join Date
    Oct 2009
    Location
    Belgium
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by XdaX View Post
    Oh thanks,

    This explains a lot :)

    Strange that Java uses a leading 0 instead of an o to express octals. This leads in my opnion to the wrong result because leading 0's in decimal numbers should just be discarded.
    if you always have a 0 infront of your result somehow and don't want it to use the octals representation. you can always use one of java's methods to cut that off before using it in math expressions.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by XdaX View Post
    Strange that Java uses a leading 0 instead of an o to express octals. This leads in my opnion to the wrong result because leading 0's in decimal numbers should just be discarded.
    There's nothing strange about it. Every language needs a way of easily defining octal integers, and this is how it is done with Java. Note that this is for integer constants, something you the programmer have complete control over, so if you understand Java, and code carefully, you should be fine. Also note that where user input is concerned and you parse the integer via Integer.parseInt, a leading 0 will in fact be discarded. Again, you the programmer has complete control over this, and in fact you can parse the number as an octal by simply changing the parser's radix.

    Corrections to the above most welcome.

    Also, I want to wish a hearty welcome to Jos, a well-known and highly regarded contributer in the Sun Java forums.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    It's a C thingy which Java adopted; the reason was that no integral number has leading zeros so a zero introduces an octal number; the 0x or 0X notation is an extension to that lexical rule. They didn't use the 0b or 0B notation for binary numbers ... go and complain at Gosling, Kernighan and Ritchie for that ;-)

    kind regards,

    Jos

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Also, I want to wish a hearty welcome to Jos, a well-known and highly regarded contributer in the Sun Java forums.
    Thank you very much for the welcome; I just saw that I signed up here more than a year ago but I never posted nor lurked here (until now). I found this forum buried somewhere in my bookmarks ;-)

    kind regards,

    Jos

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I saw that joined date, compared it with the number of posts made and failed to make sense out of anything as well.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    I saw that joined date, compared it with the number of posts made and failed to make sense out of anything as well.
    Hi r035198x, I know you too from other forums! I assume it's you, nobody else would come up with a nickname like r035198x ;-)

    kind regards,

    Jos

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't make fun of my name.
    I'm respected here, see.

  12. #12
    XdaX is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    It's a C thingy which Java adopted; the reason was that no integral number has leading zeros so a zero introduces an octal number; the 0x or 0X notation is an extension to that lexical rule. They didn't use the 0b or 0B notation for binary numbers ... go and complain at Gosling, Kernighan and Ritchie for that ;-)
    Thanks for the explanation Jos. I think I will have to live with it... But it is not very consistent that leading 0's are accepted for ocals and hexals as
    Java Code:
    int i=00007;
    int j=0x007;
    is valid java code :-).

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Well for hexadecimal the x must be there so it is different.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Don't make fun of my name.
    I'm respected here, [I can't 'post' links here yet ...]
    I read it that you were exceptional in insulting and humiliating beginners ... what did you do? ;-)

    kind regards,

    Jos

    ps. I saw Phil is here as well.

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    I merely stated some of my special gifts.
    When I asked someone to hit their head on a desk I didn't mean to hit it there hard enough so it would crack or anything.


    r035198x(<---- Good boy.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    I merely stated some of my special gifts.
    When I asked someone to hit their head on a des I didn't mean to hit it there hard enough so it would crack or anything.


    r035198x(<---- Good boy.
    Tsk, tsk, making fun of innocent stupid newbies; they should be treated with respect and we should do their homework so they can party during the weekends and get a good grade without studying or working for it. Shame on you ;-)

    kind regards,

    Jos

    ps. I removed your links in my reply because this newbie is not allowed to post any links yet ;-)

  17. #17
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by JosAH View Post
    I read it that you were exceptional in insulting and humiliating beginners ... what did you do? ;-)

    kind regards,

    Jos

    ps. I saw Phil is here as well.
    Yup, hello Jos!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    Yup, hello Jos!
    Hi there; how many of the old timers hide in here actually? Am I part of a conspiracy or secret takeover now? ;-)

    kind regards,

    Jos

  19. #19
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    A few, pbrockway, Darryl, masijade, camickr.

    No conspiracy I know of.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    pbrocks away too, DB, camickr ...

    Edit: Finally Phil posts earlier than me!

Page 1 of 2 12 LastLast

Similar Threads

  1. column result query
    By vikashkumar in forum JDBC
    Replies: 0
    Last Post: 04-03-2009, 02:23 PM
  2. problem in get result than SQL to jtable
    By MS_Dark in forum New To Java
    Replies: 0
    Last Post: 12-13-2008, 12:52 PM
  3. problem with displaying result..
    By SCS17 in forum New To Java
    Replies: 4
    Last Post: 04-23-2008, 11:19 AM
  4. Iterating through result set in JSTL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:13 PM
  5. getting a random result
    By gradon in forum New To Java
    Replies: 2
    Last Post: 07-19-2007, 03:54 PM

Posting Permissions

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