• Post Reply Bookmark Topic Watch Topic
  • New Topic

strange problem with int  RSS feed

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when you run this code , you will find that the "022" which is the "area code"
is replaced by "18" , I m just getting confuse, and finding a reason why?

and when i remove the "0" from "022" it works fine and shows 22 , can any one tell me why this is happening ?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's interpreting the 022 as Octal. whch is 2 x 8 + 2 which is 18.

WP
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java interprets numeric literal constants with a leading zero as octal numbers. 22 in octal is 18 in decimal.

Actually, even if this was not happening, the leading zero would be stripped; numerical data types cannot store such information. If you need to keep the leading zero, you need to store the data as String, not as an int or another number.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:It's interpreting the 022 as Octal. whch is 2 x 8 + 2 which is 18.

WP

Ok, thanks what should i do to take it just as int and not as Octal ?
i tried this
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Java interprets numeric literal constants with a leading zero as octal numbers. 22 in octal is 18 in decimal.

Actually, even if this was not happening, the leading zero would be stripped; numerical data types cannot store such information. If you need to keep the leading zero, you need to store the data as String, not as an int or another number.

thanks martin that was my answer
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote: . . .
i tried this
. . . and I presume it printed 18. Using new Integer.intValue(i) is a roundabout way to write i. As Martin has already told you, telephone numbers are not numbers. If you don’t want to do arithmetic with it, or i + 1 is not the next in the series (well for ints, anyway) it is not a number. Neither the "022" nor the "25555555" bit is a number.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
naved momin wrote: . . .
i tried this
. . . and I presume it printed 18. Using new Integer.intValue(i) is a roundabout way to write i. As Martin has already told you, telephone numbers are not numbers. If you don’t want to do arithmetic with it, or i + 1 is not the next in the series (well for ints, anyway) it is not a number. Neither the "022" nor the "25555555" bit is a number.

well sounds ok..but i don't understand why java is implemented in this way?
in c# the 0 is not shown only the 22 is displayed .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:
well sounds ok..but i don't understand why java is implemented in this way?


There's no good reason to write an int as 022 instead of 22 in your Java code, and even if you did, it would still end up as the exact same bytes anyway--you'd never be able to distinguish whether you had done i = 22; or i = 022;--at least not without adding extra space and time overhead to int.

So, since there's no good reason to write the int 22 as 022 in your Java code, and since there is some use (albeit limited) to being able to easily express octal numbers, the leading 0 was chosen as the way to identify and octal number.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Neither the "022" nor the "25555555" bit is a number...

But neither are they Strings.

Sorry to disagree, but I've had quite a lot of exposure to composite codes (which is, in fact, what a phone number is) in my career; and I hate the assumption that you can simply regard them as numeric Strings and be done with.

Telephone numbers, unfortunately, have been around (and persist) for an awfully long time in computer terms; so it's quite difficult to pinpoint a specific standard for them, but this might be a good place to start. However, I'd suspect very strongly that any area code beginning with '0' is an error.

The structure is basically: [country-code]-[area-code]-[exchange]-[number] (with the 2nd and/or third sometimes being omitted in small countries), and the last of these is most certainly a number because they are allocated/reallocated on a (normally) cyclic rotation until exhausted. There are obvious exceptions for emergency numbers and some other services (such as directory enquiries). If you are dialling '0' (and not '00') initially, it usually indicates that the call is internal to a country and actually has nothing to do with the number itself (at least as an identifier).

HIH

Winston (ex-Tel employee and geek)
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, the mobile phones were equipped with additional buttons over the good old 0 to 9 known from rotary dials, and mobile operators (at least in my country) didn't know better than to use them for their services; eg. dialing *99 (or something like that) leads to a help line of one of our operators. This probably makes good implementation of prospective TelephoneNumber class pretty hard...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Unfortunately, the mobile phones were equipped with additional buttons...eg. dialing *99 (or something like that)...makes good implementation of prospective TelephoneNumber class pretty hard...

Very true. Probably all soon be IP addresses anyway...

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!