This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons 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: 38100
    
  22
Welcome to the Ranch

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

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

Joined: Oct 27, 2005
Posts: 19656
    
  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: 38100
    
  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.
 
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
Wrapper Class.
K&B Self Test Chap 5 Question 4 - arguments
K&B doubt pg 559 toArray(),asList()
String Objects count
How many String Objects would be created?