blog

Home / DeveloperSection / Blogs / Language basics

Language basics

ANkita gupta 1646 03-May-2015

Variables and Data Types Variables are locations in memory in which values can be stored. They have a name, a type, and a value. Before you can use a variable, you have to declare it. After it is declared, you can then assign values to it. Java actually has three kinds of variables: instance variables, class variables, and local variables. Instance variables, as you learned yesterday, are used to define attributes or the state for a particular object. Class variables are similar to instance variables, except their values apply to all that class’s instances (and to the class itself) rather than having different values for each object. Local variables are declared and used inside method definitions, for example, for index counters in loops, as temporary variables, or to hold values that you need only inside the method definition itself. They can also be used inside blocks ({}), which you’ll learn about later this week. Once the method (or block) finishes executing, the variable definition and its value cease to exist. Use local variables to store information needed by a single method and instance variables to store information needed by multiple methods in the object. Although all three kinds of variables are declared in much the same ways, class and instance variables are accessed and assigned in slightly different ways from local variables. Today, you’ll focus on variables as used within method definitions; tomorrow, you’ll learn how to deal with the instance and class variables.

Note: Unlike other languages, Java does not have global variables—that is, variables that are global to all parts of a program. Instance and class variables can be used to communicate global information between and among objects. Remember, Java is an object-oriented language, so you should think in terms of objects and how they interact, rather than in terms of programs.

Declaring Variables To use any variable in a Java program, you must first declare it. Variable declarations consist of a type and a variable name:
int myAge;
String myName;
boolean isTired; 

Variable definitions can go anywhere in a method definition (that is, anywhere a regular Java statement can go), although they are most commonly declared at the beginning of the definition before they are used: 
public static void main (String args÷])
 { int count; String title; boolean isAsleep; ... } 

You can string together variable names with the same type: 
int x, y, z; 
String firstName, LastName;

You can also give each variable an initial value when you declare it: 
int myAge, mySize, numShoes = 28; String myName = “Laura”; boolean isTired = true;
int a = 4, b = 5, c = 6; 

If there are multiple variables on the same line with only one initializer (as in the first of the previous examples), the initial value applies to only the last variable in a declaration. You can also group individual variables and initializers on the same line using commas, as with the last example, above. Local variables must be given values before they can be used (your Java program will not compile if you try to use an unassigned local variable). For this reason, it’s a good idea always to give local variables initial values. Instance and class variable definitions do not have this restriction (their initial value depends on the type of the variable: null for instances of classes, 0 for numeric variables, ‘\0’ for characters, and false for booleans).


Updated 23-Feb-2018

Leave Comment

Comments

Liked By