What are Literals in Java ?
Any constant value which can be assigned to a variable is called a literal in java.
// Here 10,20 is a constant/literal.
int x = 10;
int y = 20;
There are different types of literals in java like integral literals, floating point literals, character literals, string literals and boolean literals you don't have to worry a little I will walk you to each of them so that it becomes a piece of cake for you to grasp.Types of Literals in java
- Integral Literal - It means integer values (like whole numbers).
- Floating point literal - It means numbers with decimal points.
- Character Literals - It means single character enclosed in a single quote (').
- String Literals - It means sequence of characters enclosed in a double quotes (").
- Boolean Literals - it represents true or false.
class Main {
public static void main(String[] args) {
// Integer literal
int num = 100; // '100' is an integer literal
// Floating-point literal
double price = 11.99; // '11.99' is a floating-point literal
// Character literal
char grade = 'B'; // 'A' is a character literal
// String literal
String name = "Bobby"; // "Alice" is a string literal
// Boolean literal
boolean isActive = true;
// here you can assign true or false to the boolean literal.
// Printing the values of all variables
System.out.println("Number: " + num); // Output: Number: 100
System.out.println("Price: " + price); // Output: Price: 11.99
System.out.println("Grade: " + grade); // Output: Grade: B
System.out.println("Name: " + name); // Output: Name: Bobby
System.out.println("Active: " + isActive); // Output: isActive: true
}
}
As you have seen in the above code:100 - integral literal
11.99 - floating literal
B - character literal
Bobby - String Literal
True - Boolean Literal
now you already get the basic idea about literals in java and how to use them in the code too now you need to grab some important info along with this too:
Types of integral literals -
Byte - you can assign the values directly to the variable like byte a = 2;
short - you can assign the values directly to the variable like short b = 3;
int - you can assign the values directly to the variable like int c = 20;
Long - you can not assign the values directly to the variable like Long a = 1000000 you have to add a prefix L or l after writing the constant value something like this Long a = 1000000l or 1000000L.
Types of Floating literals -
float - you have to write something like float variablename = 10.12f or 10.12F small f or capital F you have to add this suffix at the last of constant value.
Double - here even if you don't add D or d at the last it will still work fine like you have seen in the above example you can add also it will work in the similar way in both the case you can write Double mydouble = 10.11111d or Double mydouble = 10.11111 both will compiled successfully.
Naming convention of variables
we have almost reached at the end of the article now let's take a quick note on naming convention of variables in java when naming variables in Java, there are some rules and conventions which you need to keep in mind which are as follows:
- Variable names must start with a letter, dollar sign ($), or underscore (_).
- Variable names can contain letters, digits, dollar signs, or underscores, but cannot start with a digit.
- Java is case-sensitive, so dog and Dog will be treated as two different variables.
- Avoid using reserved keywords (like int, class, public, etc.) as variable names.
public class VariableNamingDemo {
public static void main(String[] args) {
// ✅ Correct variable naming (camelCase)
int age = 25;
String studentName = "Alice";
double accountBalance = 1520.75;
boolean isJavaFun = true;
// ❌ Bad naming examples
int AGE = 30; // All caps (looks like a constant)
String SName = "Bob"; // Starts with uppercase (class naming style)
double acc_bal = 200; // Underscore (not recommended in variables)
int x = 10; // Too short, unclear meaning
// Printing variables
System.out.println("Student: " + studentName);
System.out.println("Age: " + age);
System.out.println("Balance: " + accountBalance);
System.out.println("Enjoys Java? " + isJavaFun);
}
}
now you have reached at the end of the article hope you understood complete concept around literals in java let's wrap up this article and see you in some other amazing article 😊.
Comments
Post a Comment
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.