OnJava8-Examples/operators/Literals.java

62 lines
1.9 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// operators/Literals.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
public class Literals {
public static void main(String[] args) {
int i1 = 0x2f; // Hexadecimal (lowercase)
2015-12-02 09:20:27 -08:00
System.out.println(
"i1: " + Integer.toBinaryString(i1));
2015-06-15 17:47:35 -07:00
int i2 = 0X2F; // Hexadecimal (uppercase)
2015-12-02 09:20:27 -08:00
System.out.println(
"i2: " + Integer.toBinaryString(i2));
2015-06-15 17:47:35 -07:00
int i3 = 0177; // Octal (leading zero)
2015-12-02 09:20:27 -08:00
System.out.println(
"i3: " + Integer.toBinaryString(i3));
2015-06-15 17:47:35 -07:00
char c = 0xffff; // max char hex value
2015-12-02 09:20:27 -08:00
System.out.println(
"c: " + Integer.toBinaryString(c));
byte b = 0x7f; // max byte hex value 10101111;
System.out.println(
"b: " + Integer.toBinaryString(b));
2015-06-15 17:47:35 -07:00
short s = 0x7fff; // max short hex value
2015-12-02 09:20:27 -08:00
System.out.println(
"s: " + Integer.toBinaryString(s));
2015-06-15 17:47:35 -07:00
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix (but can be confusing)
long n3 = 200;
// Java 7 Binary Literals:
2015-12-06 11:45:16 -08:00
byte blb = (byte)0b00110101;
2015-12-02 09:20:27 -08:00
System.out.println(
"blb: " + Integer.toBinaryString(blb));
short bls = (short)0B0010111110101111;
System.out.println(
"bls: " + Integer.toBinaryString(bls));
2015-12-06 11:45:16 -08:00
int bli = 0b00101111101011111010111110101111;
2015-12-02 09:20:27 -08:00
System.out.println(
"bli: " + Integer.toBinaryString(bli));
2015-12-06 11:45:16 -08:00
long bll = 0b00101111101011111010111110101111;
2015-12-02 09:20:27 -08:00
System.out.println(
"bll: " + Long.toBinaryString(bll));
2015-06-15 17:47:35 -07:00
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
// (Hex and Octal also work with long)
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
i1: 101111
i2: 101111
i3: 1111111
c: 1111111111111111
b: 1111111
s: 111111111111111
blb: 110101
bls: 10111110101111
bli: 101111101011111010111110101111
bll: 101111101011111010111110101111
2015-09-07 11:44:36 -06:00
*/