Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is not (123 == 0123) in java?

Tags:

I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third "if" block is not reachable. Why?

When I add a leading zero to a number, the equal operator returns false.

int var = 123; if (var == 0123) {     //not reachable } if (var == 123) {     //reachable } if (var == (int)0123) {     //not reachable } if (var == (int)123) {     //reachable } 
like image 810
Bobs Avatar asked May 05 '12 11:05

Bobs


People also ask

What is the meaning of =+ in Java?

It's the Addition assignment operator. Let's understand the += operator in Java and learn to use it for our day to day programming. x += y in Java is the same as x = x + y. It is a compound assignment operator. Most commonly used for incrementing the value of a variable since x++ only increments the value by one.

Can short be negative Java?

Short values are stored in 2 bytes and contains positive or negative integer numbers.

What is the value of 9 2 in Java?

9 / 2 is integer division. It evaluates to 4.


2 Answers

0123 is an octal number (leading 0), while 123 is a decimal number.

so 0123 actually equals to 83.

like image 115
MByD Avatar answered Sep 19 '22 12:09

MByD


Any integer Number Leading With Zero is octal Number (base 8).

0123 is octal Number and 123 is Decimal Number

 0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)         =3+16+64+0         =83    
like image 23
Samir Mangroliya Avatar answered Sep 21 '22 12:09

Samir Mangroliya