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. 12
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 TipRe-read 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. 13
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. |
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 | |
0b11101 | 0b, base 2 | Binary |
|
0x1D | 0x, base 16 | Hexadecimal | |
035 | 0, base 8 | Octal |
No. 14
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. 15
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 as a decimal value. Complete the following code by assigning the hexadecimal
value
(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. 16
Binary literals
Q: |
|
A: |
|
No. 17
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 try-and-error. The programmer also looks for the smallest and largest
Our programmer is baffled:
|
A: |
Variables of type In fact using just a The second question is more difficult to answer: The Java™ standard only defines
The second On contrary the four-byte two complement Using a cast cuts off the two leading bytes of
|
No. 18
Why using braces in System.out.println(...)
?
Q: |
TipExecute the above code omitting the “inner” braces pair. |
||||
A: |
|
No. 19
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. 20
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. 21
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