DEV Community

Cover image for Java Literals free by CodeExampler

Posted on

Java Literals free by CodeExampler

literals in java – Concept Every Java Programmer Must Know
In this Java article, we'll learn one among the foremost fundamentals of Java that each Java learner must know. the implementation examples.

So firstly let’s start with an introduction to literal in Java then we'll move towards the kinds of Java literals.

Literals in Java
Literals are the info items that have fixed or constant values. Therefore we also call literals as constants in Java. Literals can represent various sorts of values like numeric, character, boolean, or String values. we will assign literals to any of the 8 primitive data types in Java.

For example, let’s consider an example that you simply frequently use in your programs.

int number = 20;
Here the amount is an integer variable and that we have assigned a worth 20 thereto . Here 20 is literal.

java literals

Note: We can’t change the worth inside the literal during the program execution.

Types of literals in java
Java allows 6 sorts of literals which are:

  1. Integer literals
  2. Floating literals
  3. Boolean literals
  4. Character literals
  5. String literals
  6. Null literal

Let’s take a deep dive into each of them:

  1. Java Integer Literals Integer literals are the entire numbers that don’t contain any fractional or exponential part. they will be either negative or positive or may contain no sign.

The rule to write down an integer literals is that there must be a minimum of one digit and there must not be any percentage point . There are often positive(+) or negative(-) signs during a literal. If the literal has no sign, then it's considered as a positive number. There shouldn't be any commas in an integer literal.

Integer Literals are further divided into three categories:

  1. Decimal Integer Literals
  2. Octal Integer Literals
  3. HexaDecimal Integer Literals

  4. Decimal Integer Literals(Base 10)

A decimal integer literal is an integer literal containing digits between 0 to 9. we will consider a sequence of digits to be a decimal integer literal unless it begins with 0 digits.
For example:

1256, 67, +98, -54 are some valid decimal integer literals. But the amount 0987 isn't a legitimate decimal integer literal.


int x1 = 100; //A valid decimal literal
int x2 = -467; //A negative decimal constant
int x3 = 089; //Not a decimal literal

  1. Octal Integer Literals(Base 8) An Octal integer literal may be a literal containing combination of digits between 0 to 7. The sequence of such literals always starts with 0 digits.

For example, a decimal integer 8 are often written as 010 within the octal number form. And 49 in decimal are often written as 061 as an octal integer literal. confirm while writing octal literals they ought to always begin with 0 and will not contain any 8 or 9 in them, as 8 and 9 are both invalid octal digits.


int x1 = 0765; //Octal constant
int x2 = 0987; //Invalid because it contains 8 and 9

  1. Hexadecimal Integer Literals(Base 16) A sequence of digits starting with either 0x or 0X falls under the category of hexadecimal integer literals. In such literals there are often a mixture of digits from 0 to 9 and also there are often alphabet characters from A to F.

We can use both uppercase and lowercase letters. All other letters aside from A to F are illegal. for instance , the decimal integer 12 are often written as 0XC in hexadecimal form.


int x1 = 0x54; //Hexadecimal integer
int x2 = -0X6BF8; //Valid negative hexadecimal literal

Note: we will also use the suffix l or L and u or U to represent Long and unsigned values respectively.

package com.techvidvan.literals;
public class IntegerLiterals {
public static void main(String[] args) {
int decimalLiteral = 561; // decimal literal
int octalLiteral = 01204; // octal literal
int hexaDecimalLiteral = 0x1BfA; // Hexa-decimal literal

System.out.println("The value of Decimal literal is: " + decimalLiteral);
System.out.println("The value of Octal literal is: " + octalLiteral);
System.out.println("The value of Hexa-decimal literal is: " + hexaDecimalLiteral);
The value of Decimal literal is: 561
The value of Octal literal is: 644
The value of Hexa-decimal literal is: 7162

  1. Floating literals in Java Floating literals are the literals that have fractional parts and decimal points in them. they're also referred to as the important literals. we will write to them in two forms-either in fractional form or in exponential form.

We can’t specify them in octal or hexadecimal values; they will only be in decimal form. there's a decimal point(.) between the digits.

The rule to write down a floating literal is that:
It should be within the fractional form containing a minimum of one digit before a percentage point and a minimum of one digit after the percentage point . There also can be a positive (+) or negative (-) sign before a floating literal. a true or floating literal with none sign is taken into account to be positive.

Some valid floating literals are:

2.0, 16.7, -13.0, -0.00987

Some invalid floating literals are:

7 (No decimal point)

  1. (No digit after decimal place) +14/6 (/- illegal symbol used) 17.257.87 (Two decimal points) 34,789.50 (No commas allowed)

This was the representation of floating literals in fractional form. Now we'll discuss the exponent sort of floating literals.

A floating literal within the exponential form consists of two parts: mantissa and exponent. for instance , 6.8 are often written as 0.68 x 101 = 0.68E01, where the mantissa part is 0.68(part appearing before E) and therefore the exponent part is 1(the part appearing after E). E01 represents 101.

Some valid real literals within the exponent form are:


Some invalid real literals within the exponent form are:

172.E5 (At least a digit must follow the decimal point)
1.7E (No digit specified for exponent)
0.17E2.3 (Exponent cannot have fractional part)
17,445E02 (No comma allowed)

Note: Every floating type may be a double type and this is often the rationale why we cannot assign it on to the float variable, to flee this example we use f or F as a suffix, and for double we use d or D.

package com.techvidvan.literals;
public class FloatingPointLiteral {
public static void main(String args[]) {
double floatingLiteral1 = 987.678; // fractional floating literal
double floatingLiteral2 = 089.0987 // It also acts as fractional floating litera
double floatingLiteral3 = 1.234e20; // Exponential form

System.out.println("The floating literal in fractional form is: " + floatingLiteral1;
System.out.println("The floating literal in fractional form is: " + floatingLiteral2);
System.out.println("The floating literal in exponential form is: " + floatingLiteral3);
The floating literal in fractional form is: 987.678
The floating literal in fractional form is: 89.0987
The floating literal in exponential form is: 1.234E20

  1. Boolean Literals in Java The Boolean literals have only two values- true and false. They contain only Boolean values. These are used for assigning flag variables or when there's a requirement for checking any condition.

The Boolean literal can’t take any value aside from true and false. we've to use the boolean data type.


boolean b1 = true; //valid boolean value
boolean b2 = false; //valid boolean value
boolean b3 = 5; //invalid boolean value

package com.techvidvan.literals;
public class BooleanLiterals {
public static void main(String args[]) {
boolean b1 = false;
// boolean b2 = 7; this may generate a mistake , so comment this line
int x = 20;
if (x they two == 0) {
b1 = true;
System.out.println("The result for the if condition is: " + b1);
System.out.println("The number is even");
else {
b1 = false;
System.out.println("The result for the if condition is: " + b1);
System.out.println("The number is even");

The result for the if the condition is: true
The number is even

  1. Java Character literals A character literal represents one character that's enclosed during a quotation mark ‘’, as in ‘Z’. The rule for writing a personality literal is that it must contain one character enclosed within one quote. we've to use the char data type to represent a personality literal.

For example,

char ch = ‘A’;

Java allows us to possess certain non-graphic characters as character literals. Nongraphic characters are the characters that we can’t directly from the keyboard; example backspaces, tabs, etc. These nongraphic characters are often represented as escape sequences.

There are many escape sequences in Java and that they are listed within the following table:

Escape Sequences in Java

Some valid character literals are:
char c = ‘a’;
char c = ‘%’;
char c = ‘\t’;
char c = ‘\’;
char c = ‘4’;
char c = ‘S’;

We can also use the character literals to represent an integer literal that shows the Unicode value of the character. It can specify the integer in decimal, octal or hexadecimal form but within the range of 0-65535.

Example, ch = ‘063’; is valid.

We can also specify the character literals within the UNICODE representation ‘\uxxxx’. Here xxxx represents 4 hexadecimal numbers.

For example, ch = ‘Ω’ //Here u03a9 may be a hexadecimal number.

package com.techvidvan.literals;
public class CharacterLiteral {
public static void main(String[] args) {
char character = 'd';
char unicodeCharacter = 'd';
System.out.println("Character literal value is: " + character);
System.out.println("Value of unicode character is: " + unicodeCharacter);
System.out.println("\" may be a symbol");
Character literal value is: d
Value of Unicode character is: d
” may be a symbol

  1. Java String Literals The String literals represent multiple characters. A sequence of zero or more characters written within quotation mark ” ” is understood as a string. Each character during a string literal also can represent an escape sequence. All String-Literals belong to class String.

For example, Some valid string literals are:

String s = “Abc”
String s = “TechVidvan”
String s = “I love my country”
String s = “Rita\’s book” // \’ is an escape sequence
String s = “\ab” //\a is escape sequence

Some invalid string literals are:

String s = HelloWorld; //No quotation mark
String s = ‘TechVidvan’; //Single quote not allowed

package com.techvidvan.literals;
public class StringLiteral {
public static void main(String[] args) {
String myString = "Welcome to TechVidvan Tutorials of Java";

// If we assign without "" then it treats as a variable and causes compiler error //String myString1 = Hello;

System.out.println(“The String is: “ + myString);
The String is: Welcome to TechVidvan Tutorials of Java

  1. Null Literals Finally, the Null literals are the values that represent null values. The null literals have only one value (null). It shows the null reference. A null literal is of the null type.

Discussion (3)

codeexampler profile image
CodeExampler Author

max function in python By CodeExampler

codeexampler profile image
CodeExampler Author

pg in noida search local business online by Leads4Needs website

codeexampler profile image
CodeExampler Author • Edited on

learn php in free by CodeExampler Website switch php