Literals
Code  Result 


Decimal 35 Binary 35 Hex 35 Octal 35 
System.out.println("35 as Binary (Base 2): " + Integer.toString(35, 2));
System.out.println("35 as Ternary (Base 3): " + Integer.toString(35, 3));
System.out.println("35 as Octal (Base 8): " + Integer.toString(35, 8));
System.out.println("35 as Hexadecimal (Base 16): " + Integer.toString(35, 16));
results in:
35 as Binary (Base 2): 100011 35 as Ternary (Base 3): 1022 35 as Octal (Base 8): 43 35 as Hexadecimal (Base 16): 23
No. 18
Pretty may not be pretty
Q: 
Consider the following program:
This will run smoothly producing the expected output: 20 + 3 + 9 = 32 We now prettify our variable definitions by introducing right aligning numbers thereby padding leading positions with zeros: public static void main(String[] args) { int a = 20, b = 03, c = 09; // Compiler error: The literal 09 of type int is out of range System.out.println(a + " + " + b + " + " + c + " = " + (a + b + c)); } The above code does not compile due to a compiler error when
defining variable Explain the underlying cause. In particular: Why is TipReread the section on integer literal representations. 
A: 
Integer literals starting with “0” denote octal representation. The octal system's set of digits is {0, 1, 2, 3, 4, 5, 6, 7}. Therefore “9” is no valid digit. 
No. 19
Strange output
Q: 
Consider the following code:
On execution we receive the output 
A: 
This problem is related to the previous exercise: The integer literal 041 defines octal representation. Changing from octal to decimal representation yields 4 * 8 + 1 = 33. There are 11 types of people: Those, who can read binary codes, those who know what binary is and those who don't have a clue about binary at all. 
No. 20
Representing emoticons.
Q: 
We may use the HEAVY WEDGETAILED RIGHTWARDS ARROW character in Java™:
This results in: Arrow: ➽ Strangely the SMILING FACE WITH SUNGLASSES' (U+1F60E) emoticon appears to be different. Answer the following questions:

A: 

System.out.println(1000000000); // o.K.
System.out.println(2147483647); // o.K.: Largest int value 2^31  1
System.out.println(10000000000L); // o.K.: Using type long
System.out.println(10000000000 ); // Compile time error: Integer number
// larger than 2147483647 or
// 2^31  1, Integer.MAX_VALUE)
System.out.println("Hello"); // A String literal
System.out.println(33452); // An int literal
System.out.println(34.0223); // A double (floating point) literal
System.out.println(2147483648L); // A long literal
System.out.println("Value 1: " + 29);
System.out.println("Value 2: " + 0b11101);
System.out.println("Value 3: " + 0x1D);
System.out.println("Value 4: " + 035);
Value 1: 29 Value 2: 29 Value 3: 29 Value 4: 29
Literal  Discriminator  Type  Value 

29 
base 10 
Decimal  $2\times {{10}}^{1}+9\times {{10}}^{0}$ 
0b11101  0b, base 2  Binary 
$1\times {{2}}^{4}+1\times {{2}}^{3}+1\times {{2}}^{2}$ $+0\times {{2}}^{1}+1\times {{2}}^{0}$ 
0x1D  0x, base 16  Hexadecimal  $1\times {{16}}^{1}+13\times {{16}}^{0}$ 
035  0, base 8  Octal  $3\times {{8}}^{1}+5\times {{8}}^{0}$ 
No. 21
Poor mans ASCII table
Q: 
We want to construct a list of printable
ASCII characters. Write a Java™
application printing the character literals : 32 !: 33 ": 34 #: 35 $: 36 %: 37 &: 38 Notice the empty space being represented by decimal 32. 
A: 
Since
Using an explicit type conversion from char to int (a so called cast) yields an identical result:

No. 22
Integer value hexadecimal representation
Q: 
As you may know the RGB color model uses triplets of numbers to define color value components representing intensities of its three base colors Red, Green and Blue. The component values range from 0 to 255, the latter defining maximum intensity. The color “red” for example is being represented by (255, 0, 0). So the red component has maximum intensity while blue and green are zero. It is however common to use hexadecimal in favour of decimal values. Thus the same color “red” in the subsequent HTML example's heading font is now being represented by (FF,0,0):
Write a program printing hexadecimal representations like ${\mathrm{C0}}_{\mathrm{16}}$ as a decimal value. Complete the following code by assigning the hexadecimal
value
${\mathrm{C0}}_{\mathrm{16}}$ (The “silver” color's all three
component's intensity in
TipYou may want to consider the Primitive Data Types section learning about hexadecimal integer value representation. 
A: 
Using hexadecimal literals we have:

No. 23
Binary literals
Q: 

A: 

No. 24
Testing the limits (Difficult)
Q: 
A careful programmer is worried whether a short variable is large enough to hold color intensity values ranging from 0 to 255. Give an answer being based on the specification and not just by tryanderror. The programmer also looks for the smallest and largest
Our programmer is baffled:

A: 
Since variables of type In fact using just a The second question is more difficult to answer: The Java™ standard only defines
The second On the contrary the fourbyte two complement Avoiding this error requires representing
${2}^{\mathrm{15}}$ as an
Alternatively we may as well use ${2}^{\mathrm{15}}$ adding a unary minus sign:
Despite the unary minus the latter resembles our initial faulty value. The error in the first place is probably assigning a value
of
$+{2}^{\mathrm{15}}$ to a Finally using a cast strips off the two leading bytes from the int representation leaving us with the desired pattern for a short representing 32768: int short 00000000 00000000 10000000 cast > 10000000 00000000 00000000 
No. 25
Why using braces in System.out.println(...)
?
Q: 
TipExecute the above code omitting these “inner” braces. 

A: 

No. 26
Composing strings of literals and variables
Q: 
Consider the following code:
Complete the above snippet by adding code to produce the following output: 3 Games having 22 players each results in 66 players altogether. Write your code in a way that changing i.e. 
A: 

No. 27
Escaping double quotes
Q: 
Consider the following code:
The corresponding output will be TipHunt for “java escape double quote” and read about character literals in the “Language Fundamentals” / “Literals” section of [Kurniawan]. 
A: 
There are at least three solutions on offer:

No. 28
Supplementary string exercises
Q: 
Solve the following external exercises: 
int year = MMXIV; // Roman numerals representation
System.out.println("Olympic winter games: " + year);
Could this happen?
Olympic winter games: 2014