?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Variable in Java Programming with Example

    In this lesson, we will learn about the variable in Java programming and its application, along with an example.

    What is Variable

    A variable is like a container that stores information which can be used and manipulated by a program. Variables have names, and these names are used to access the stored data.

    Think variable as a box having a name where you can keep a piece of data that you can use later in your program. You can also change the data stored in a variable whenever you want.

    Video Explanation

    Click the button below to watch the explanation.

    video image

    A variable has three key concept and they are:

    • Name: This is the label you give to the variable.
    • Value: This is the information you want to store in the variable.
    • Data Type: This indicates what kind of data the variable is holding (e.g., a number, text, etc.). We will discuss about it in our next lesson.

    Let's see some real-life examples of variables. Image you are organizing a birthday party and need to keep track of various details. So let's see how you can store those details in variables.

    You need to store the total number of guest coming to the party.

    You can use Variable Name: guest_count and store the value 20 in it like.

    guest_count = 20


    You need to store the name of the person whose birthday it is.

    birthday_person = "Alice"


    You need to remember the date of the party.

    party_date = "2024-06-15"


    You need to keep track of how much money you have for the party.

    budget = 5000.00


    You need to know if the party will be held indoors.

    is_indoors = True

    Let's take another Example

    Example
    a = 5
    a = 25

    In the above example, a is used as a variable name to store the value 5 in it. So we can say that the value of the variable a is 5.

    On the second line we have changed the value of the variable a by storing a new value 25 in it. Now the new value of variable a is 25.

    So it is clear from the above example that a variable can store only one value at a time if you store any other value in it then the previous value will be erased and the new value will be stored in it.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Test Your Knowledge
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.