Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can any one explain me please how (021 == Integer.valueOf("17"))

 
joe jonatanas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

I can't understand one line in this code. Can any one explain me please how (021 == Integer.valueOf("17"))

Code


output:23
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Simple! numbers beginning with 0 are octal, so 021 equals 17.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find all the details in the Java Language Specification, which is not easy to understand, but you see it applies to integer literals.
 
joe jonatanas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Simple! numbers beginning with 0 are octal, so 021 equals 17.

But only in numerical literals. Integer.parseInt("011") will simply return 11, which is why you won't see "1" in the result. To parse that String as octal one should use Integer.parseInt("011", 8).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49770
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, that applies to integer literals. Not to floating‑point literals, I don’t think, having had a quick look at the Java Language Specification section I quoted earlier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic