Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Java deal with hard coded numeric values?

Tags:

java

int

I have decompiled a Java class which contain this line :

DatabaseError.throwSqlException((int)23);

Until now i believe that Java consider that literal numeric value is int, So why they cast 23 ? And in general how Java deal with hard coded numeric values ?

Edit: Im not interested in this specific case, but interested to go deeper with Java in general.

like image 502
54l3d Avatar asked Jul 12 '26 21:07

54l3d


1 Answers

I still believe it is related to the used decompiler.

It can be assumed that the signature of the method is DatabaseError.throwSqlException(int i).

The statement DatabaseError.throwSqlException((int)23) would be compiled as DatabaseError.throwSqlException(23) - the literal is of type int and the method parameter type is of type int

the DatabaseError.throwSqlException((int)23L) would be compiled as DatabaseError.throwSqlException(23) - the value can be stored without precision lose in an int and the method parameter type is of type int

The byte code is in both cases

bipush 23    // push the value on the operand stack
invokestatic // DatabaseError.throwSqlException:(I)V

Decompiled it would be DatabaseError.throwSqlException(23). Because in case it was 23L in the source code, this information isn't in the bytecode.

A bytecode similar to DatabaseError.throwSqlException((int)23) could be

ldc2_w       // push a long from constant pool on the operand stack
l2i          // convert top value on the operand stack from long to int 
invokestatic // DatabaseError.throwSqlException:(I)V

But this is decompiled by jad and jd-gui to DatabaseError.throwSqlException((int)23L) (notice the L after the value).

edit The above bytecode is decompiled by CFR as DatabaseError.throwSqlException((int)23). So it really depend on the used decompiler.

like image 173
SubOptimal Avatar answered Jul 15 '26 11:07

SubOptimal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!