OnJava8-Examples/operators/Literals.java

49 lines
1.7 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// operators/Literals.java
2015-06-15 17:47:35 -07:00
public class Literals {
public static void main(String[] args) {
int i1 = 0x2f; // Hexadecimal (lowercase)
2015-11-03 12:00:44 -08:00
System.out.println("i1: " + Integer.toBinaryString(i1));
2015-06-15 17:47:35 -07:00
int i2 = 0X2F; // Hexadecimal (uppercase)
2015-11-03 12:00:44 -08:00
System.out.println("i2: " + Integer.toBinaryString(i2));
2015-06-15 17:47:35 -07:00
int i3 = 0177; // Octal (leading zero)
2015-11-03 12:00:44 -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-11-03 12:00:44 -08:00
System.out.println("c: " + Integer.toBinaryString(c));
2015-06-15 17:47:35 -07:00
byte b = 0x7f; // max byte hex value10101111;
2015-11-03 12:00:44 -08:00
System.out.println("b: " + Integer.toBinaryString(b));
2015-06-15 17:47:35 -07:00
short s = 0x7fff; // max short hex value
2015-11-03 12:00:44 -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:
byte blb = (byte)0b00110101;
2015-11-03 12:00:44 -08:00
System.out.println("blb: " + Integer.toBinaryString(blb));
2015-06-15 17:47:35 -07:00
short bls = (short)0b0010111110101111;
2015-11-03 12:00:44 -08:00
System.out.println("bls: " + Integer.toBinaryString(bls));
2015-06-15 17:47:35 -07:00
int bli = 0b00101111101011111010111110101111;
2015-11-03 12:00:44 -08:00
System.out.println("bli: " + Integer.toBinaryString(bli));
2015-06-15 17:47:35 -07:00
long bll = 0b00101111101011111010111110101111;
2015-11-03 12:00:44 -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
*/