Skip to content

Commit 74a3887

Browse files
author
Ram swaroop
committed
added + modified content
1 parent 033b447 commit 74a3887

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

_posts/2015-05-14-variables-and-literals.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: post
33
title: Variables and Literals
4-
published: false
4+
published: true
55
---
66

77
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
106106
placing a zero in front of the number, as follows:
107107

108108
{% 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
113113
{% endhighlight %}
114114

115115
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 $$.
117117

118118
**Hexadecimal Literals**
119119

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:
120122

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 %}
121135

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:
122138

139+
{% highlight java %}
140+
long jo = 110599L;
141+
long so = 0xFFFFl; // Note the lowercase 'l'
142+
{% endhighlight %}
123143

124144
#### Floating-point Literals
125145

0 commit comments

Comments
 (0)