Friday, September 21, 2018

Integer with leading zeroes

Question:
When I write System.out.println(0123); I get 83 however System.out.println((int)0123F);prints 123.
Why does it work that way?

Answer:

Octal (base-8 number)

0123 means octal 123, that is 1*8*8 + 2*8 + 3, which equals 83. For some reason, octal floats are not available.
Creating 0123 means the integer 83. Creating 0123F means the floating 123. When converted back to integer, it remains 123.
Just don't use the leading 0 if you don't mean octal. After all, they are not exactly useful(and programmers who do know about octal numbers will get confused when they see 09F).

No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...