aspose 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
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: 36508
    
  16
Welcome to the Ranch

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

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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: 36508
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can any one explain me please how (021 == Integer.valueOf("17"))
 
Similar Threads
K&B Self Test Chap 5 Question 4 - arguments
String Objects count
K&B doubt pg 559 toArray(),asList()
How many String Objects would be created?
Wrapper Class.