Literals in Java

 Literals in Java are like the ingredients you use when cooking a recipe. They are the specific values you directly write into your code, and they have a fixed meaning. Think of them as the actual numbers, text, or other data that you put into your Java program.

Here are some common types of literals in Java:

Integer Literals: These are whole numbers, like 5, 10, or -3. They don't have decimal points.

Floating-Point Literals: These are numbers with decimal points, like 3.14 or -0.5. They can represent fractions.

Character Literals: These are single characters enclosed in single quotes, like 'A' or '7'.

String Literals: These are sequences of characters enclosed in double quotes, like "Hello, World!".

Boolean Literals: These are the words "true" and "false," representing yes and no or on and off.

Null Literal: This is the word "null," representing the absence of a value.

Literals are like the raw ingredients in a recipe, and you can use them to create the data you need in your Java program.

age stores an integer literal.

pi stores a floating-point literal.

grade stores a character literal.

message stores a string literal.

isSunny and isRainy store boolean literals.

name stores a null literal, indicating the absence of a value.

When you run this program, it will display the values stored in these variables, demonstrating the use of different types of literals in Java.


Post a Comment

0 Comments