Variables in java

In this tutorial, we will see introduction to variables, declaration, and types.

Introduction

Variables in java is used to store values of elements while the program is executed.

Java variables are nothing but memory location. It stores the values which can be manipulated by program. Variables in java are strongly typed hence they all must have a datatype followed by an identifier.

Each variable has the specific data type which defines size and layout of the variable’s memory.

Declaring a variable

We can declare a variable as below:

Here is the example:

variable declaration

💡 Did you know?

value is optional while declaring variables. You can declare variable and assign value to it later in program.

Variable has a name that is used to identify it. The name of the variable is known as the variable’s identifier.

There are some naming conventions that you need to follow while declaring a variable.

  • You can use A to Z or a to z
  • You can use 0 to 9
  • You can use special symbol$ and _ to declare a variable
  • You can not start a variable name with number
  • You can not have spaces in variable name.
  • You can not use reserved keywords in variable name
  • Varibles names are case sensitive.

Examples:

You can declare variables in groups as well.

Assigning a value to variable

Each variable holds some data which it describes. We use = operator to assign value to a variable.

Compiler will give compile type error if value is not matched with datatype.
For example:
You can not assign String literal to int data type.

Types of variables

There are three kinds of variables in java.

Local variable

A Variable which is declared inside a method can be termed as Local Variable. It is mandatory to initialize local variable otherwise Compiler will complain about it.

Instance variable

A Variable which is declared at class level can be termed as Instance variable. It is not mandatory to initializeInstance variable.

All instance variable will be by default initialized by JVM.

Static variable

A Variable which is declared as static is known as Static variable. Static variables are class level variables.

Let’s understand it with the help of simple program:

When you run above program, you will get below output:

Instance variable: 0
Static variable: 20
Method local variable: 10

That’s all about Variables in java.

Related Posts

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.