Computing

Statements & Expressions

In the last few articles, we saw how to create an applet and an application in Java. Now lets get into the basics of Java, that is the conditional statements that are used in Java. They form the core of any programming language, because the various conditional loops in a program enable it to work the way we want it to.

A statement is the simplest thing you can do in Java; a statement forms a single Java operation. All the following are simple Java statements:

int i = 1;
import java.awt.Font;
System.out.println("This car is a "+ color + " " + make);
m.engineState = true;

Statements sometimes return values'for example, when you multiply two numbers together or test to see whether one value is equal to another. These kind of statements are called expressions. Always remember that a Java statement always ends with a semicolon. Forget the semicolon and your Java program won't compile.

Java also has compound statements, or blocks, which can be placed wherever a single statement can. Block statements are surrounded by braces ({}).

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. Variables in short are just like the lockers in a bank. Each locker has a number, a size and a key. In order to use a locker to keep your valuables, you have to tell the bank, so that its officially yours and you have the privacy. Same is the case with variables.

After variable is declared, you can then assign values to it. Similarly as soon as the bank assigns us a locker, we store valuables like will, agreement or jewelry in it.

Java actually has three kinds of variables: 

instance variables, 

class variables, and 

local variables.

Instance variables
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 ({}) . 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 same ways, class and instance variables are accessed and assigned in slightly different ways from local variables. Now lets focus on variables as used within method definitions, later lets learn how to deal with instance and class variables.

Unlike other languages, Java does not have global variables (like C)'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. In my next article i will give you a briefer insight into how to declare variables.   

Next: Variable Type and Declarations

29-Jul-2001

More by :  Deepak Chandrasekaran


Top | Computing

Views: 3568      Comments: 0





Name *

Email ID

Comment *
 
 Characters
Verification Code*

Can't read? Reload

Please fill the above code for verification.