Literals and Variables

description of image


Literals


Literals are fixed values that we can use directly in our programs.For example, 5, -12.5, 'c' etc.
Different types of literals are mentioned below.

Types of literals

1.Integers


Numeric value without any decimal part.For example, 4,-6,0,345,etc.


2.Floating-point Numbers


Numeric values that have decimal parts. For example, 3, -4.32, 8.0 etc.


3.Characters


Single character enclosed inside single quotes. For example, ‘a’ ‘u’ ‘+’ etc.

Variables


In programming, variables are used to store data so that we can use them later in the program.
Next, we will learn to create variables.

Create a variable


There are two things needed to create a variable:

● Variable name – a unique name to indicate the variable

● Data Type – the type of data variable can store

For example,

int age;

Here, we have created a variable named age. This variable can only store integer values. We know this because we have

used int to declare the variable.

Note: The ;(semicolon) at the end is mandatory. It indicates the end of the statement.

Create double variable


To create variables that can store decimal numbers, we can use double.

For example,

double salary;

Here, salary is a variable that can only store floating-point numbers (decimal numbers).



Print Variables


● Run the following program in your compiler to get the feel of how variables are used in a Java program.



● copy the below code and paste it your java compiler.



Quiz


Question Text