Java Variables

Java Variables

Java Variables

1) What is Variable?
A named memory location to store or hold the data.

There are two types of memory in Computer environment:

i) Primary memory -RAM

ii) Secondary memory -ROM
Ex: CD, DVD, HDD etc…

Note: Variables store in primary memory.
————————-
2) Declaration of Variables in Java.
Java Supports explicit declaration of variables

Syntax:

dataType VariableName;

Ex:

int a;
—————
dataType variable1, variable2, variable3;

Ex:

int a, b, c;
————————
dataType VariableName = value;

Ex:

int a =100;
————————–
3) Assigning values to variables
a) Initialization

int a;
a=10; // Initialization
————–
int a=10; // Initialization
—————-
b) Reading
using Input devices
from files(text/excel)
from databases
from Application objects
————————-
4) Types of Variables
a) Instance Variable
A variable that is declared inside the class but outside the method

b) Local variable

A variable that is declared inside the method

c) Static variables
A Variable that is declared as static, It cannot be local.
———————-
5) Naming Conventions
> Java Variables are case sensitive, monkey is not the as Monkey or MONKEY.

char monkey, Monkey, MONKEY

> Java variable name must start with a letter or $ or _

Ex:
myvar
MYVAR
$myvar
_myvar
myvar1
myvar_1
———-
1myvar – invalid
———————-
> Variable names cannot be equal to java reserved words

Ex:

int
for
import

> Must be unique in the scope of declaration.
—————————————-
6) Java Variables Declaration Example:

package JavaExamples;

public class VariablesExamples {

public static void main (String [] args){
// dataType variableName;
int a;
a = 10;
// dataType VariableName = Value;
int b = 20;
// Declaration of multiple variables in a statement
int c, d, e;
c =30;
d =40;
e = 50;
// Declaration of multiple variables and assigning values
int f=60, g=70, h=90;
int j;
j = a; // Reading
char x =’A’;
double y = 2.3456;
String z = “Selenium”;

System.out.println (a);
System.out.println (b);
System.out.println (c);
System.out.println (d);
System.out.println (e);
System.out.println (f);
System.out.println (g);
System.out.println (h);
System.out.println (x);
System.out.println (y);
System.out.println (z);
System.out.println (j);
}
}
————————————

(37)

You may also like...

Leave a Reply

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