Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Literals

What is a Literal?

A literal is a constant value written directly in the code.
It represents a fixed value assigned to a variable.

Literal Example

int a = 42;

a > Primitive Variable

= > Assignment Operator

42 > Integer Literal


| Literal Type           | Example(s)              | Description                                |
|------------------------|-------------------------|--------------------------------------------|
| Integer Literal        | `42`, `-7`, `88`        | Whole numbers without decimals             |
| Floating Point Literal | `3.0`, `-2.5`, `0.99f`   | Numbers with decimal points                |
| Character Literal      | `'a'`, `'1'`, `'%'`      | Single characters in **single quotes**     |
| String Literal         | `"Hello"`, `"123abc"`   | Sequence of characters in **double quotes**|
| Boolean Literal        | `true`, `false`         | Represents logical values                  |
| Null Literal           | `null`                  | Represents **null reference** for objects  |

Notes

Literals are used with primitive data types like int, float, char, boolean.

null is used with non-primitive types (like objects or Strings).

Each literal tells the compiler what kind of value is being assigned.

Example in Code:

int score = 88;           // Integer Literal

float pi = 3.14f;         // Floating Point Literal

char grade = 'A';         // Character Literal

String name = "John";     // String Literal

boolean isPassed = true;  // Boolean Literal

String city = null;       // Null Literal