|
1 | 1 | --- |
2 | 2 | layout: post |
3 | 3 | title: Variables and Literals |
4 | | -published: false |
| 4 | +published: true |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | Variables can be broadly classified in to 2 types in Java: |
@@ -106,20 +106,40 @@ Octal integers use only the digits 0 to 7. They have a radix of 8. In Java, you |
106 | 106 | placing a zero in front of the number, as follows: |
107 | 107 |
|
108 | 108 | {% highlight java %} |
109 | | -int six = 06; // Equal to decimal 6 |
110 | | -int seven = 07; // Equal to decimal 7 |
111 | | -int eight = 010; // Equal to decimal 8 |
112 | | -int nine = 011; // Equal to decimal 9 |
| 109 | +int six = 06; // equal to decimal 6 |
| 110 | +int seven = 07; // equal to decimal 7 |
| 111 | +int eight = 010; // equal to decimal 8 |
| 112 | +int nine = 011; // equal to decimal 9 |
113 | 113 | {% endhighlight %} |
114 | 114 |
|
115 | 115 | You can have up to 21 digits in an octal number, not including the leading zero. This is because no mater what number |
116 | | -system you use, the range of values that an `int` can hold is always between $$ -2^31 to +2^31-1 $$. |
| 116 | +system you use, the range of values that an `int` can hold is always between $$ -2^{31} $$ to $$ +2^{31}-1 $$. |
117 | 117 |
|
118 | 118 | **Hexadecimal Literals** |
119 | 119 |
|
| 120 | +Hexadecimal (hex for short) numbers are constructed using 16 distinct symbols. They have a radix of 16. Counting from |
| 121 | +0 through 15 in hex looks like this: |
120 | 122 |
|
| 123 | +{% highlight java %} |
| 124 | +0 1 2 3 4 5 6 7 8 9 a b c d e f |
| 125 | +{% endhighlight %} |
| 126 | + |
| 127 | +Java accepts uppercase or lowercase letters for the extra digits _(one of the few places Java is not case-sensitive)_. |
| 128 | +You represent an integer in hexadecimal form by placing a `0x` in front of the number, as follows: |
| 129 | + |
| 130 | +{% highlight java %} |
| 131 | +int x = 0X0001; // equals to decimal 1 |
| 132 | +int y = 0x7fffffff; // equals to decimal 2147483647 |
| 133 | +int z = 0xDeadCafe; // equals to decimal -559035650 |
| 134 | +{% endhighlight %} |
121 | 135 |
|
| 136 | +All four integer literals (binary, octal, decimal, and hexadecimal) are defined as `int` by default, but they may also |
| 137 | +be specified as `long` by placing a suffix of `L` or `l` after the number: |
122 | 138 |
|
| 139 | +{% highlight java %} |
| 140 | +long jo = 110599L; |
| 141 | +long so = 0xFFFFl; // Note the lowercase 'l' |
| 142 | +{% endhighlight %} |
123 | 143 |
|
124 | 144 | #### Floating-point Literals |
125 | 145 |
|
|
0 commit comments