Latest :

Java Variables – Tutorial With Examples

Java variables are nothing but a similar way we use a variable in mathematics also. Assume if we want to find an area of a rectangle, the formula we use is a=l*b. In this expression, ‘a’, ‘l’ and ‘b’ are Java variables. Usage is same both in mathematics and programming.

Variables In Java – Different Types

How To Create a Variable ? & Different Types

  • Int a; < here ‘int’ is datatype and ‘a’ is variable
  • double b; <  here ‘double’ is a datatype, and ‘b’ is variable
  • char gender; <  here ‘char’ is datatype and ‘gender’ is variable
  • boolean f1,f2,f3; <  here ‘boolean’ is datatype and ‘f1’, ‘f2’ and ‘f3’ are variables

Example – 1: Declaring in java 


Example -2 : Sorting data : 


Example 2: Declaring Multiple environment Variables


Actual Purpose Of Using The Environment Variables ?

When we want to store a data through a program, data is stored in the main memory (RAM). RAM is a memory area that is divided into some bytes.

Suppose, we want to store a value 25, then 25 will be retained by occupying some place in the RAM. Similarly, if we want to store another value, suppose 58, then 58 will fill in some other memory location in the RAM.

Each such value( environment variable ) will occupy some place in RAM, and we need to refer those values for sometimes in the program. When we indicate a location, we may change the value in that location (may be that 25 is updated to 29).

As the value in a location may keep changing, accessing that location with its value is difficult. Sometimes two or more locations may have the same value, and one value has no relation to the other values.

In that case, it may be difficult to identify the value of which location we wanted too. 

 7 Features With Examples

  • An environment variable name is formed with one or more of the following symbols.
  • Lower case alphabets               a b c d e f g h I j k l m n o p q r s t  v w x y z
  • Uppercase Alphabets               A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
  • Digits                                           0 1 2 3 4 5 6 7 8 9
  • Underscore                                  _
  • Dollar signs                                   $

1) Declaring variables in Java Environment

Example #1 : 

2) A Java environment variable name cannot start with a digit.

Example #2 :

3) There is no limit on length (number of symbols) of the Java environment variable name.

Example # 3:

4) A Java Environment variable should be initialized before using it.


4) Keywords cannot be used as environment variables.

Example # 4:

5) When we create a variable/Declaring in a method, we should not create another variable with the same name (even in an inner block) until the first goes out of scope.

Example # 5:

6) We can create a variable anywhere in the program (in C, an environment variable should be declared at beginning of a block only)

Example # 6:


7) Environment Variables can be created at initialization part of a for loop. Such are destroyed automatically when the loop ends. 

Example # 7:

If you have any doubts related to java primitive data types and java variables, do comment here. Our dedicated expert author will be in touch with you 🙂


Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...