# Literals in Java

May 1st, 2019

## Literals in Java

Any type of data can be used inside java programming. These data can be accessed using or without using variables inside our program.
Now, we’ll discuss that how these data is been divided and how many categories of data we have totally.

#### There are 5 basic categories which are:

1. Integral Literals
2. Floating Literals
3. Character Literals
4. String Literals
5. Boolean Literals

### Integral Literals :

Integrals are nothing but the whole numbers as we say in mathematics (i.e, the numbers without decimals and fractions).
This integral literal can be a binary number (base 2) or an octal (base 8) or a decimal (base 10) or a hexadecimal (base 16).
Now, these integrals are divided into the following types:

• byte
• short
• int
• long

The value for these integral type variables can be represented as decimals, octals, hexadecimals and binaries, which are also known as Decimal Literals, Octal Literals, Hexadecimal Literals and Binary Literals (which is added from jdk 1.7). Each of these above mentioned types (byte, short, int, long) has their own specific range of values, which we’ve already seen under the topic ‘Data Types’. Which means it accepts values only within its own range.
If any value beyond its range is tried to be assigned, then we will end up with loss of data. Therefore, while using decimals, octals and hexadecimals also if the value accommodates within its given specific range, then that value will be accepted by those literals while assigning.
Now, let us have a glance at the following :

#### Decimal Literals :

Allows digits from 0 – 9. e.g. int a = 123;

#### Octal Literals :

Allows digits from 0 – 7. But, these values should be prefixed with ‘0’. e.g. int a = 0147;

Allows digits from 0 – 9 and alphabets from a – f. And must be prefixed with ‘0x’ or ‘0X’. Supports uppercase and lowercase. Even though java is case-sensitive, here it is exceptional. e.g.int a = 0x253Face;

#### Binary Literals :

From jdk 1.7, we can have values of literals in binary form also. It allows only two digits in its values i.e, 0 and 1. But, these values have to be prefixed with ‘0b’ or ‘0B’.
Example: int a = 0b1011;

#### Example:

 ```public class Literals { public static void main(String[] args) { int a = 123;                  // Decimal Form int b = 0147;               // Octal Form int c = 0x253Face;      // Hexadecimal Form int d = 0b1011;                       // Binary Form System.out.println("Decimal Form : "+a); System.out.println("Octal Form : "+b); System.out.println("Hexadecimal Form : "+c); System.out.println("Binary Form : "+d); } }```

#### Output :

 ```Decimal Form : 123 Octal Form : 103 Hexadecimal Form : 39058126 Binary Form : 11```

### Floating Literals :

Floating literals are nothing but integrals values with a fractional part. We can’t provide octal, hexadecimal and binary values as floating literals. Only decimal values are allowed.

#### Example:

 5.3866e4 -> 53, 866 3e-11 -> 00000000003 1e+4 -> 10000 7.341E-3 -> 0.007341 3.5E+4 -> 35000 0.4e-6 -> 0.0000004 5.e10-> 50000000000

### Character Literals :

Alphabets or any values provided within single quotes are Character Literals.
Character Literals are of 4 types :

• Using Single Quotes
• Using Integral Literal
• Unicode Representation
• Escape Sequence

#### Using Single Quotes

e.g. char c = ‘l’;

#### Using Integral Literal

We can assign Unicode value.

##### Example:

char c = 052;
which represents character *
Here, the allowed range is 0 to 65535.

#### Unicode Representation

i.e, Representation of Unicode values in Hexadecimal pattern.

##### Example:

char c = ‘\u0062’; // represents lowercase character b
or
char c = ‘\u0041’; // represents uppercase character A

#### Escape Sequence

##### Example:
 ```char c = ‘\n’; or char c = ‘\t’; or char c = ‘\b’;```

### String Literals :

String Literals can be represented as a sequence of characters within double quotes.
String s = “New Delhi”;
String stmt = “Qutub Minar is in New Delhi,” + “ which is the capital of India.”;

### Boolean Literals :

Boolean literals contains only two values. i.e, true and false [Since, java is case sensitive, lowercase ‘true’ and ‘false’ are only allowed as boolean literals]. None of any other values are allowed here.
Example:

 ```boolean a=true; boolean b=false; boolean a=0; boolean b=1; boolean a= TRUE; boolean b=FALSE; boolean a=True; boolean b=False;```
Social Share:
{"ft_lang_label":"__label__en","ft_lang_prob":0.7422521,"math_prob":0.9727533,"size":4274,"snap":"2020-45-2020-50","text_gpt3_token_len":1128,"char_repetition_ratio":0.14918032,"word_repetition_ratio":0.012430939,"special_character_ratio":0.28427702,"punctuation_ratio":0.17612293,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97052854,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-30T13:09:35Z\",\"WARC-Record-ID\":\"<urn:uuid:336dfdd4-a9c8-492b-ba19-6b226da7507e>\",\"Content-Length\":\"242648\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0fe9fc77-9f51-4ef3-b1a9-a05af4699568>\",\"WARC-Concurrent-To\":\"<urn:uuid:c9ebc59e-b707-4271-a891-db665866413c>\",\"WARC-IP-Address\":\"3.223.153.182\",\"WARC-Target-URI\":\"https://www.gangboard.com/blog/literals-in-java\",\"WARC-Payload-Digest\":\"sha1:ZIWPK3XLFQXTB5FIZX7XSOMEZOGF7IQA\",\"WARC-Block-Digest\":\"sha1:D3QT7C6C74HCDP67GSTR6OO6VI6ZKA4Z\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141216175.53_warc_CC-MAIN-20201130130840-20201130160840-00046.warc.gz\"}"}