<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged literals</title>
<link>https://coderlegion.com/tag/literals</link>
<description>Powered by Question2Answer</description>
<item>
<title>Data Types in Java, part 2 - specifying literals</title>
<link>https://coderlegion.com/5754/data-types-in-java-part-2-specifying-literals</link>
<description>&lt;p&gt;What is a Literal? A Literal in the Java source code is used to specify values(constants). In Java, we can specify values of the primitive types, the &lt;code&gt;String&lt;/code&gt; type and the &lt;code&gt;null&lt;/code&gt; type. &lt;code&gt;null&lt;/code&gt; is a special type which does not have a name, we only have a value of type &lt;code&gt;null&lt;/code&gt; which is assignable to any reference type.&lt;/p&gt;
&lt;p&gt;How do we specify literal value for the integral data types in Java? The JVM is a 32 bit machine. The basic integral type is &lt;code&gt;int&lt;/code&gt;, i.e. even if we use operations on &lt;code&gt;byte&lt;/code&gt; it would internally still be using 32 bits. How do we specify literal values of type &lt;code&gt;int&lt;/code&gt;? Let’s look at the &lt;code&gt;boolean&lt;/code&gt; expressions given in Listing below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(12 == 012)
(12 == 014)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which of the above two &lt;code&gt;boolean&lt;/code&gt; expressions are &lt;code&gt;true&lt;/code&gt;? The first or the second? It seems that the first is &lt;code&gt;true&lt;/code&gt; and the second is &lt;code&gt;false&lt;/code&gt;; no, it is the other way round. The first expression is &lt;code&gt;false&lt;/code&gt; and the second is &lt;code&gt;true&lt;/code&gt;, not only in Java but also in &lt;code&gt;C&lt;/code&gt;. In fact the token &lt;code&gt;019&lt;/code&gt; is not legal in a Java file Let us look at another expression.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(12 == 0x0C)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Will this expression evaluate to &lt;code&gt;true&lt;/code&gt;? Yes, it will be &lt;code&gt;true&lt;/code&gt;. When any token starts with ‘&lt;code&gt;0x&lt;/code&gt;’ or ‘&lt;code&gt;0X&lt;/code&gt;’, then the following characters are considered to be hexadecimal digits. Similarly, when a token begins with a ‘&lt;code&gt;0&lt;/code&gt;’(digit zero), then the following characters are considered as octal digits. So in the case of Java, an in-teger constant value can be specified either using &lt;em&gt;octal&lt;/em&gt;, &lt;em&gt;decimal&lt;/em&gt; or &lt;em&gt;hexadecimal&lt;/em&gt;.  To specify an &lt;code&gt;int&lt;/code&gt; literal in &lt;code&gt;hexadecimal&lt;/code&gt;, begin the literal with either ‘&lt;code&gt;0x&lt;/code&gt;’ or ‘&lt;code&gt;0X&lt;/code&gt;’, followed by the hexadecimal digits for the literal value. To specify an &lt;code&gt;int&lt;/code&gt; literal in octal, begin the literal with a ‘&lt;code&gt;0&lt;/code&gt;’, followed by the octal digits for the literal value, and to specify an &lt;code&gt;int&lt;/code&gt; literal in decimal, specify the literal value without starting with a zero.&lt;/p&gt;
&lt;p&gt;From Java 7 onwards, we can specify integral literal value in binary also. If a token begins with ‘&lt;code&gt;0b&lt;/code&gt;’ or '&lt;code&gt;0B&lt;/code&gt;’, then the following characters are considered to be bits. There is another feature in Java 7 for integral literal value which can increase the readability of large tokens. When specifying any of the integral literal value, we can use the `_' (underscore) character for grouping, e.g. in case of binary literals, we might like to group the bits in sequences of four bits. The following two lines of code are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int a = 0b1010_0101_1100_0011;
int a = 0b1010010111000011;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The grouping character in numeric literal cannot be used at the beginning or the end. This grouping character can also be used when specifying numeric literals in decimal, hexadecimal or octal. e.g. The following two lines of code are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;int a = 10000000;
int a = 10_000_000; // This is easier to read as ten million
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If a literal of the &lt;code&gt;long&lt;/code&gt; data type is required, then it must be suﬀixed by either ‘&lt;code&gt;l&lt;/code&gt;’ (lowercase letter &lt;code&gt;L&lt;/code&gt;) or ‘&lt;code&gt;L&lt;/code&gt;’. ‘&lt;code&gt;L&lt;/code&gt;’ is preferred as we tend to misread ‘&lt;code&gt;l&lt;/code&gt;’ (lowercase letter &lt;code&gt;L&lt;/code&gt;) as the digit ‘&lt;code&gt;1&lt;/code&gt;’ (one). An example of &lt;code&gt;long&lt;/code&gt; type is given below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;long tenBillion = 10_000_000_000;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Literals of the floating-point data types are also possible. When we use &lt;code&gt;7.5&lt;/code&gt;, it is considered as literal of type &lt;code&gt;double.&lt;/code&gt; If we want to have a literal of type &lt;code&gt;float&lt;/code&gt;, it should be suﬀixed with the letter ‘&lt;code&gt;f&lt;/code&gt;’ or ‘&lt;code&gt;F&lt;/code&gt;’. Example of float and double literals are given below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;float f = 7.5f;
double d = 7.5;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Literals for the &lt;code&gt;char&lt;/code&gt; types are declared by having the single character within two single quote characters. e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;'A' '0' '*'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For some special characters like the single quote character, double quote character, the new line character or tab character, we use the backslash character as an escape character and then specify these characters similar to &lt;code&gt;C&lt;/code&gt;. Table below lists the characters which can be written with the help of escape character.&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
  &lt;tr&gt;&lt;td&gt;Character Literal&lt;/td&gt;&lt;td&gt;Character&lt;/td&gt;&lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tr&gt;&lt;td&gt;'`\b`'&lt;/td&gt;&lt;td&gt;backspace&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\f`'&lt;/td&gt;&lt;td&gt;form feed&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\n`'&lt;/td&gt;&lt;td&gt;line feed&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\r`'&lt;/td&gt;&lt;td&gt;carriage return&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\t`'&lt;/td&gt;&lt;td&gt;tab character&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\\`'&lt;/td&gt;&lt;td&gt;back slash&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\b'`'&lt;/td&gt;&lt;td&gt;single quote&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;'`\b&quot;`'&lt;/td&gt;&lt;td&gt;double quote&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;code&gt;char&lt;/code&gt; literals may also be specified using their octal values as in C, e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;char ch = '\030';
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Literals for the &lt;code&gt;String&lt;/code&gt; class are specified by enclosing a sequence of Unicode characters within double quotes(&quot;). examples of String literals are as follows:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=3696007931030496064&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;The first &lt;code&gt;String&lt;/code&gt; literal is a sequence of 11 characters, the second String literal above is a sequence with zero characters, or we can call it as an empty &lt;code&gt;String&lt;/code&gt; and the third literal is a &lt;code&gt;String&lt;/code&gt; literal with three character, which also contains the greek letter for pi. The literals for the &lt;code&gt;char&lt;/code&gt; type and the &lt;code&gt;String&lt;/code&gt; type, may use Unicode escapes, discussed in earlier tutorial on Data Types part 1.&lt;/p&gt;
&lt;p&gt;Using Unicode escape the third literal above could also have been written as&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;2\u03C0r&quot;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From Java 15 onwards, String literals can also be specified using text block, where the characters of the String literal are specified using triple 'double quotes'.&lt;br&gt;
This literal can span multiple lines and the characters do not need escaping.&lt;br&gt;
e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;String code =
    &quot;&quot;&quot;
    void main() {
        IO.println(&quot;Hello world&quot;);
    }
    &quot;&quot;&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This tutorial is part 2 of the tutorial on Data types in Java, which covered the primitive types. This will be continued and concluded in part 3 for the data types, which will cover the reference data types.&lt;/p&gt;
</description>
<category>Java Basics</category>
<guid isPermaLink="true">https://coderlegion.com/5754/data-types-in-java-part-2-specifying-literals</guid>
<pubDate>Mon, 29 Sep 2025 11:17:59 +0000</pubDate>
</item>
</channel>
</rss>