This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  Can  any one explain me please how (021 == Integer.valueOf( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Can  any one explain me please how (021 == Integer.valueOf("17"))" Watch " Can  any one explain me please how (021 == Integer.valueOf("17"))" New topic
Author

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

joe jonatanas
Greenhorn

Joined: Jul 13, 2012
Posts: 2
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

Joined: Oct 13, 2005
Posts: 37879
    
  22
Welcome to the Ranch

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

Joined: Oct 13, 2005
Posts: 37879
    
  22
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

Joined: Jul 13, 2012
Posts: 2
Thank you

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
You’re welcome
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can any one explain me please how (021 == Integer.valueOf("17"))
 
Similar Threads
Wrapper Class.
String Objects count
How many String Objects would be created?
K&B doubt pg 559 toArray(),asList()
K&B Self Test Chap 5 Question 4 - arguments