Tutorial Outline
- Java
Variables
- Naming
Conventions: Java Variables
- Types
of Java Variables
- Type casting in Java
- Variables Vs Default Values
Java Variables
- Variable is the name of the reserved area allocated in the memory.
- In
other words, it is the name of memory location.
- Variable’ is a combination of ‘very’ + ‘able’, that means its
values can be changed.
- Variable is like a container, which holds the values,
while the Java program is executed.
- Variable is assigned with a data type.
- Variable can take different values, but one value at a time.
- Values
can be changed during the execution of the program.
- For different
types of values, we have different types of variables.
- Variables names can be anything, but it can be logical.
For every variable, we need three things:
i.
Type
of variable
ii.
Name
of the variable
iii.
Data/Information
Example: int num = 10;
Note: Where ‘num’ is a variable
of the Integer data type.
Naming Conventions: Java Variables
ü It should start with a lowercase
letter, such as: id, name, num, etc.
ü It should not start with the special
characters, like & (ampersand), $ (dollar), _ (underscore), etc.
ü If the name contains multiple words,
start it with the lowercase letter, followed by an uppercase letter,
such as: firstName, lastName, etc.
ü Avoid using one-character
variables, like a, b, c, x, y, z, etc.
Example: int a = 5; // valid; not recommanded
int num = 5; // valid
int num; // valid
int 123abc; // not valid
int abc123; // valid
int
abc$; // valid
int ch; // valid
int $_num; // not valid
· int num = 8_00_00_00 // valid (ver 1.7)
· double abc = 5; // implicit conversion
Example:
public class Test1{
public static void main(String[] args){
int num = 5;
System.out.println(num);
}
}
ð Save this file as ‘Test1.java’
ð To Compile: $javac Test1.java
ð To Execute: $java Test1
ð Output: 5
Example:
public class Test2{
public static void main(String[] args){
int num = 5;
// System.out.printf(“%d”, num);
System.out.println(num);
num = 8; // changing the value of variable
‘num’.
System.out.println(num);
}
}
ð Save this file as ‘Test2.java’
ð To Compile: $javac Test2.java
ð To Execute; $java Test2
ð Output:
5
8
Types of Java Variables
- Local variables
- Instance variables
- Static variables
Local variables
The variables which are
declared inside the method, those variables are called local
variables.
Example:
public class LocalVarExample{
public static void main(String[] args){
int num = 10; // local variable
System.out.println(num);
}
}
ð
Save
this file as ‘LocalVarExample.java’
ð
To
Compile: $javac
LocalVarExample.java
ð
To
run/execute: $ java
LocalVarExample
ð Output: 10
Instance variables
· The variables which are
declared inside the class, but outside of the method, those
variables are called instance variables.
· We can access instance variables
in the method by creating an object.
Example:
public class InstanceVarExample{
int
num1 = 100;
int
num2 = 200; // instance variables
public static void main(String[] args){
// creating new object
InstanceVarExample obj = new InstanceVarExample();
// accessing instance variables
System.ou.println(obj.num1);
System.out.println(obj.num2);
}
}
ð Save this file as ‘InstanceVarExample.java’
ð To Compile: $javac InstanceVarExample.java
ð To execute: $java InstanceVarExample
ð Output:
100
200
Static variables
Static variables are declared inside the class
and outside of the method with a ‘static’ modifier.
Methods to access static variables
1. Using class name // recommanded
Example: System.out.println(Test.varName);
2. Direct access // not recommanded
Example: System.out.println(varName);
3. By creating object // not recommanded
Example:
Test obj = new Test();
System.out.println(obj.varName);
Example:
public class StaticVarExample{
static
int num1 = 100;
static int num2 = 200; // static variables
public static void main(String[] args){
//
direct access
System.out.println(num1); // not recommanded
System.out.println(num2);
//
creating new object
StaticVarExample obj = new StaticVarExample();
System.out.println(obj.num1);
// not recommanded
System.out.println(obj.num2);
//
access using class name
System.out.println(StaticVarExample.num1);
//
recommanded
System.out.println(StaticVarExample.num2);
}
}
ð Save this file as ‘StaticVarExample.java’
ð To Compile: $javac StaticVarExample.java
ð To Execute: $java StaticVarExample
ð Output:
100
200
100
200
100
200
Type casting in Java
double num = 5;
int num1 = (int)num;
System.out.println(num1);
Example:
public class Test3{
public
static void main(String[] args){
int
num1 = 10; // local variable
int
num2 = 20;
System.out.println(num1);
System.out.println(num2);
num2
= 30; // changing the value of variable
System.out.println(num2);
double
num3 = 15.06;
System.out.println(num3);
//
type casting
int
num4 = (int)num3;
System.out.println(num4);
}
}
ð Save this file as ‘Test3.java’
ð To Compile: $javac Test3.java
ð To Execute: $java Test3
ð Output:
10
20
30
15.06
15
Variables Vs Default Values
èDefault value of class à null
S. No. Type JVM Default Value
1. Premitive byte 0
short 0
int 0
long 0
2. Floating-point float 0.0
double 0.0
3. Character char single space
4. Boolean boolean (true/false) false
Note:
· Instance variable à JVM will assign the default
value.
· Static variable à JVM will assign the default
value.
· Local variable à JVM will not assign the default
value, we must initialized local variables before using.
Example:
// instance variables; default value
public class Test4{
int
num;
boolean
b; // instance variables
public
static void main(String[] args){
//
creating new object
Test4
t = new Test4();
//
accessing the instance variables
System.out.println(t.num);
// 0
System.out.println(t.b); // false
}
}
ð Save this file as ‘Test4.java’
ð To Compile: $javac Test4.java
ð To Execute: $java Test4
ð Output:
0
false
Example:
// static variable; default value
public class Test5{
static
int a;
static
double b;
static
boolean c; // static variables
public
static void main(String[] args){
//
accessing static variables using class name
System.out.println(Test5.a);
// 0
System.out.println(Test5.b); // 0.0
System.out.println(Test5.c); // false
}
}
ð Save this file as ‘Test5.java’
ð To Compile: $javac Test5.java
ð To Execute: $java Test5
ð Output:
0
0.0
false
Example:
// local variable; default value
public class Test6{
public
static void main(String[] args){
int
a;
double b;
boolean c; // local variables
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
ð Save this file as ‘Test6.java’
ð To compile: $javac Test6.java
ð To Execute: $java Test6
ð Output: Error!! message appears
-----------------------------------------------------------------------------
Previous: Java Tokens and Data Types
Next: Java Operators
Previous: Java Tokens and Data Types
Next: Java Operators


0 Comments