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. 16
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 by padding leading positions with zeros: 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 even compile. 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 octal digit. |
No. 17
Strange output
|
Q: |
Consider the following code: On execution we receive |
|
A: |
This problem is related to the previous exercise: The
integer literal 041 defines octal representation of an
Converting octal to decimal representation yields 4 * 8 + 1 = 33.
|
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.System.out.println("i carrying double: " + i))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 literalSystem.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
No. 18
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! Produce the above output by solely using the character
representations |
|
A: |
We assign our Using an explicit type conversion (cast) from char to int yields an identical result: |
No. 19
Integer value hexadecimal representation
|
Q: |
You may be familiar with the color model using triplets of numbers defining colors. These triplets represent the tree color component intensities of this model's 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): The red component having maximum intensity while blue and green are zero. In RGB it's common using hexadecimal in favour
of decimal values. Thus the aforementioned color
“red” in the subsequent HTML example's heading font
may also be represented by (FF, 0,
0) or Write a program printing hexadecimal representations of a single intensity 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. 20
Binary literals
|
Q: |
|
||||||||
|
A: |
|
No. 21
Testing the limits (Difficult)
|
Q: |
|
|
A: |
|
No. 22
Why using braces in System.out.println(...) ?
|
Q: |
TipExecute the above code omitting the “inner” braces pair. |
||||
|
A: |
|
No. 23
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. 24
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. 25
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
