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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Example { public static void main(String args[]) { byte b; short s; int i; float f; char c; double d; boolean boo; long l; System.out.println("Examples created succesfully"); } } |
Output:
1 |
Examples created successfully |
Example -2 : Sorting data :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Example { public static void main(String args[]) { int i=5; float f=5.5f; char c= 'a'; double d=123456.789; boolean b=true; System.out.println("Data stored in path successfully"); } } |
Output:
1 |
Data stored in path successfully |
Example 2: Declaring Multiple environment Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Example { public static void main(String args[]) { int i1; int i2; int i3; int i4; // Or int i1,if,i3,i4;.. float f1;float f2;float f3;float f4; // Or float f1,f2,f3,f4;..... char c1;char c2;char c3;char c4; // Or char c1,c2,c3,c4;..... double d1; double d2; double d3; double d4; // Or double d1,d2,d3,d4; .... boolean b1;boolean b2; boolean b3;boolean b4; // boolean b1,b2,b3,b4 .... System.out.println("more than one example created succesfully"); } } |
Output:
1 |
more than one example created succesfully |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Example { public static void main(String args[]) { int examples; int a; int A; int xyz; int abc; double a1; double ABc; char java; char c1; boolean C$_; System.out.println("variables names using lower,uppercase,digits,Symbols($,_) succesfully"); } } |
2) A Java environment variable name cannot start with a digit.
Example #2 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Example { public static void main(String args[]) { int 1a; //error char 2c; //error double 3d; //error int 1java; //error int j1ava; //ok int ja1va; //ok int jav1a; //ok int a1; //ok char c2; //ok double d3; //ok } } |
3) There is no limit on length (number of symbols) of the Java environment variable name.
Example # 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Example { public static void main(String args[]) { int a1; int hello; int javaprogramshub; char object_oriented_programming; float qwertyuiopax; int abcdefghijklmnop123$_; double subject_is_more_important_than_marks; } } |
4) A Java Environment variable should be initialized before using it.
Example:
1 2 3 4 5 6 7 8 9 10 |
class Example { public static void main(String args[]) { int a; System.out.println(a);// error: variable a might not have been initialized } } |
4) Keywords cannot be used as environment variables.
Example # 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Variables { public static void main(String args[]) { int char; // error becuase char is keyword int class; // error becuase class is keyword int int; // error becuase int is keyword int if; // error becuase if is keyword int while ; // error becuase while is keyword int CHAR; // ok becuase Char it is not a keyword because keywords are in " lowercase " } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Example { public static void main(String args[]) { int a; float a; //error: a is already defined in method main(String[]) int b; int b; //error: b is already defined in method main(String[]) float b; //error: b is already defined in method main(String[]) } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Example { public static void main(String args[]) { int a=10; System.out.println(a); int b=20; System.out.println(b); int c=a+b; System.out.println(c); int d=40; System.out.println(d); int x=22; } } |
Output:
1 2 3 4 |
<span style="font-family: helvetica, arial, sans-serif">10 20 30 40</span> |
7) Environment Variables can be created at initialization part of a for loop. Such are destroyed automatically when the loop ends.
Example # 7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Examples { public static void main(String args[]) { for(int i=1;i<4;i++) { System.out.println(i);// 1 2 3 } System.out.println(i);//error because variable "i" is vanished after forloop } } |
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 🙂