File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Head First Android this week in the Android 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: 43367
    
  32
Welcome to the Ranch

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

Joined: Oct 13, 2005
Posts: 43367
    
  32
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: 43367
    
  32
You’re welcome
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20048
    
  30

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 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43367
    
  32
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.
 
wood burning stoves
 
subject: Can any one explain me please how (021 == Integer.valueOf("17"))
 
It's not a secret anymore!