Numbers
Numbers in Java can be written in many different forms. Depending on the prefix or suffix we use, we can declare it’s base (binary, hex, octal), and even it’s type (int, long, float).
Alterations in the base:
With literal prefixes we can define 4 different numeric bases, and they are: Octal, Hexadecimal, Decimal and Binary.
We can represent an Octal number by adding the 0 prefix to it.
We can represent a Hexadecimal number by adding the 0x prefix to it.
When we don’t add any prefix, the number is treated as a decimal number.
We can represent a Hexadecimal number by adding the 0x prefix to it.
When we don’t add any prefix, the number is treated as a decimal number.
And with Java 7 we have a new literal prefix to represent binary base, which is the 0b prefix.
Here’s an example of all of them together:
public static void main(String[] args){ int decimal = 100 ; // 100 em decimal. int octal = 0144 ; // decimal 100 represented in octal base. int hex = 0x64 ; // decimal 100 represented in hexadecimal base. int bin = 0b1100100; // decimal 100 represented in binary base // Only works in Java 7 version or higher. System.out.println(decimal); // Prints '100' System.out.println(octal); // Prints '100' System.out.println(hex); // Prints '100' System.out.println(bin); // Prints '100' } |
Alterations in the type
When we’re talking about the type we have a few suffixes that help us define what type of variable we’re working with.
It’s important to say that if we don’t add any suffix to a whole number, e.g. 12, it’s treated as an int, and if we don’t add any suffix to a number with a floating point, e.g. 3.14, it’s treated as a double.
What if I want a float, or a long, or a byte?
In case you want to declare that your number is a long, instead of an int, you can use the L suffix.
Important: The suffixes are case insensitive, which means l (lower case) is the same as L (upper case).
So we define a long like this:
public static void main(String[] args){ long l1 = 351698791198l; long l2 = 351698791198L; } |
It’s the same for floats but the suffix in this case is the f:
public static void main(String[] args){ float f1 = 3 .14f; float f2 = 3 .14F; } |
What about the byte? I’m adding the byte just for the sake of information, because it doesn’t fit in the “literals” subject. And the reason for that is that there isn’t any suffix to represent a byte. In case you want a byte you’ll have to do a cast.
public static void main(String[] args){ byte b = ( byte ) 12 ; } |
Other literals:
We have one more literal, that is related to floating point numbers, it’s less known than the others. And it’s the literal E.
This literal is the exponent. The number after the E indicates the power of 10 by which the number before the E will be multiplied. Example:
3E2 is 3 x 10² that is 300. 1.07E-4 is 1.07 x 10-4 that is 0.000107
Here’s the above example, written in code:
public static void main(String[] args){ double d1 = 3E2; double d2 = 1 .07E- 4 ; System.out.println(d1); System.out.println(d2); } |
0 comments:
Post a Comment