Skip to content

Commit 70c10fd

Browse files
author
Ram swaroop
committed
added content
1 parent 74a3887 commit 70c10fd

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ These are numbers with a radix of 10 which we use most commonly. They do not nee
8787
as below:
8888

8989
{% highlight java %}
90-
int length = 343;
90+
int length = 343; // 343 is the literal
9191
{% endhighlight %}
9292

9393
**Binary Literals**
@@ -143,16 +143,41 @@ long so = 0xFFFFl; // Note the lowercase 'l'
143143

144144
#### Floating-point Literals
145145

146+
Floating-point numbers are defined as a number, a decimal symbol, and more numbers representing the fraction. For
147+
example,
146148

149+
{% highlight java %}
150+
double d = 11301874.9881024;
151+
{% endhighlight %}
147152

153+
By default, floating-point literals are defined as `double` (64 bits) so if you want to assign a floating-point literal
154+
to a variable of type `float` (32 bits), you must attach the suffix `F` or `f` to the number. So, the below code
155+
generates a compiler error:
148156

157+
{% highlight java %}
158+
float f = 23.467890; // Compiler error, possible loss
159+
// of precision
160+
{% endhighlight %}
149161

162+
This happens because we're trying to fit a larger number (64 bits) into a (potentially) less precise "container"
163+
(32 bits).
150164

165+
Now as by default floating-point literals are of type `double`, it is optional to attach a suffix of `D` or `d` when you
166+
want to assign it to a variable of type `double`. For example,
167+
168+
{% highlight java %}
169+
double d = 110599.995011D; // Optional, not required
170+
double g = 987.897; // No 'D' suffix, but OK because the
171+
// literal is a double by default
172+
{% endhighlight %}
151173

174+
#### Boolean Literals
152175

153176

154177

178+
#### Character Literals
155179

156180

157181

182+
#### String Literals
158183

0 commit comments

Comments
 (0)