Why do numbers(long, int) starting with 0 end up having other values?

Asked

Viewed 58 times

4

I was doing some tests on a phone number formatter when I tried this entry for a long: 033222221111L, compiled normally (Yes, in the real world it would not make sense an entry like this, if it is a numerical value). But what intrigued me was the fact that the exit was (036)6222-5993 where the expected was (033)22222-1111, then debugging I saw that there was nothing wrong with my phone formatter, but with past number, because the value that was coming to the method was 3662225993 instead of 33222221111, that would be the natural in the real world.

So I would like to know the reason why this happens? And what is the logic for that 033222221111L become 3662225993?

Simplicando o exemplo:

System.out.println(String.valueOf(033222221111L)); 
// output = 3662225993 ?????

Edit 1

Whereas the 0 is to represent that the value is octal, o por que dessa escolha pelo 0(zero), since it can easily cause confusion, as in my case, é herança do C?

  • 4

    if it is an entry in "Octal" format (different from the decimal system we use) due to the prefix "0" on the front, it is like "0x" for hexadecimals

  • @Leonardobosquett, I thought it would be some sort of input formatting code. I just don’t understand why use 0, since it can be easily confused. For hexadecimals is 0x, that makes more sense.

  • 2

    Under the prefix "0", it appears to be historical: http://stackoverflow.com/questions/1835465/where-did-the-octal-hex-notations-come-from

  • @Math, I searched here to see if I could find anything answer to the question, Soen, I would like a Portuguese version, but now I don’t know if I remove the question? Or I let it be closed as duplicate, to facilitate future searches for the same terms? What do you think?

  • 2

    @Fernando duplicates when created organically are usually beneficial to the site, I think there is no need to delete, even because you may notice that the question there was asked in a completely different way than yours. In addition it is important to see if they really are duplicated, so I understand they are, but see if there is any doubt left that can cause yours to remain open in search of something that the answer there does not meet.

  • @Math, the only doubt that remains in relation to his answer the question "possibly duplicated", is "why water loads" was used the value 0 (zero) for this? Since the link of Leonardobosquett, is not very convincing, for the answer accepts there start with "I Dunno ..." (I think ... or I don’t know ...), although it makes some sense. = D

  • @Math, analyzing the two questions better, which I think is different questions with equal answers, because the question there does not even begin with 0, but rather integer in general. Even if here it is closed without response, maybe it is interesting to keep to lead up to the answer. I will see if there is something in the Meta about.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.