Showing posts with label Literals in JAVA. Show all posts
Showing posts with label Literals in JAVA. Show all posts

Jul 5, 2020

Literals in JAVA | Java Language | Coding Winds

LITERALS IN JAVA

Constant values that are assigned to the variables are known as literals. They can be represented directly into the source code.

Ex:

int weight = 66; // known as decimal literals.

Though the way shown in the above example to initialize the variable is the standard way and most of the time everyone prefers this way only. But still there exist number of other ways to initialize a variable, using literals of different forms.

Integral literals

Octal Literal

An octal literal represents an integer . The digits in an octal literals are allowed from 0 to 7, and it is written with 0 (as a prefix) followed by the rest of the number or in short

Ex:

int weightInKg=0102;  //Octal literal for 66

If you will print weightInKg which is initialized in the octal literal, it will show 66 as an output.

 

Hexa-decimal literals

A hexa-decimal literals can also be used to represent an integer , formed with combination of letters (restricted from ‘a’ to ‘f’ only) and number (0-9). Also, it must contain ‘0X’ or ‘0x’ as a prefix. Note that the letters used in the hexa-decimal code can be of  upper as well as lower case.

Ex:

int var= 0X121CAB; //hexa-decimal literal for 1186987

If your will print var it will show 1186987 on the output screen.

Note: Letters in hexadecimal literals can be of any case(Upper or lower).

 

Binary literals

Likewise Octals and hexa-decimals, binary literals are also used to represent an integer. Binary literals are formed by the combination of binary numbers, i.e., 1s and 0s having a prefix 0B or 0b.

Ex:

int age=0b1011;     // binary literal for 11

If you will print age, it will show 11 in the output screen.

 

Character literals

Likewise integer char datatype, in char variables can also be assigned to literals in more than one way.

Single quote

The most standard way to assign literals to variables in java is by using single quotes. These literals are known as decimal literals.

Ex:

char var='c';    

Using Ascii values:

Every character in programming language has a certain numeric value known as an ASCII value (American Standard Code for Information Interchange). For example,  character ‘a’ has an ASCII value 97, starting from 97 all the following alphabets have the ASCII value in continuation till ‘z’ (ASCII value of ‘z’ is 122). Note that, if we talk about the same alphabets but in upper case, the ASCII value is not the same. For example, ASCII value of ‘A’ is 65 and all the following alphabets have the ASCII value in continuation till ‘Z’.(ASCII value of ‘Z’ is 90). Apart from alphabets different character such as ‘#’, ‘$ ,@ ,etc, also have a specific ASCII value. In fact whole numbers when treated as characters also has a specific ASCII value.

Ex:

char ch ='1'; 

int asciiValue = ch;

When we will print the variable asciiValue it will give 49 as an output which is the ASCII value of ‘1’ as a character. Similarly, we can also use an ASCII value to initialize a variable.

Ex:

char ch = 98;       //ASCII value for character ‘b’

Now, if we will print the value of the variable ch we will see the character ‘b’ popping up on the console.

Apart from using the ASCII value, literals for characters can also be in Octal and Hexa-decimal forms, but the allowed range is 0 to 65535.

Note: Negative numbers are not considered as character. If you will try to find the ASCII value for a negative number you will get an error.

Unicode Representation

A literal can also be initialized by using a Unicode representation which consists of a four digit hexa-decimal number having ‘\u’ as a prefix. Note that the Unicode is always represented within a single quote.

Ex:

char ch = '\u0065'; //Unicode for the alphabet ‘e’

 

Escape Sequence

An Escape sequence (such as ‘\t’, ‘\n’, etc) can also be used as a literal in Java.

Ex:

char ch= '\n'; or

char ch= '\t';

Now if we print the above initialized characters , then it will do its respective functions in the console. Below is the example which explains this in an elaborative manner.

char a =  'O';                   char a =  'O';  

char b = '\n';       OR                          char b= '\t';    

char c  =  'K';                        char c  =  'K';    

Outputs will be as follows: 

Boolean literals

Boolean literals consists of only two values i.e., true and false. Since boolean datatypes are used to check whether a given condition is true or false, at a given point of situation in a code.

Ex :

boolean isTall = true;  

Now if you will print the variable ‘isTall’ , the value true will be printed out in the output screen. Lets look another example to understand it carefully.

boolean isCool = true;

boolean isBoring =false;

System.out.println("Coding Winds is cool"+"-"+isCool);

System.out.print("Coding Winds is boring"+"-"+isBoring);

Output : 

Coding Winds is cool-true

Coding Winds is boring-false




String literals

Strings are one of the most important datatypes in Java, and we already talked about it a lot when we discussed about datatypes. But when it comes to literals, java has only one type of string literals. Any sequence of characters within double quote is known as string literals.

Ex:

String str ="Hello Readers";

In fact we have already discussed this, but what if we desire to wrap our string inside the double or single quotes? That is, what if we want our output to read “Hello Readers” instead of Hello Readers ? For this purpose we use back slashes followed by our single or double qoute (\’ or \”). Let’s look at an example for a clear view of this.

String str="\"Hello Readers\"";

Now if we will print the variable str, the output screen will read “Hello World”. Similarly we can also use \’  for inserting a single quote anywhere in our screen.


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!