Sunday, August 2, 2009

Variable

Variables are data items that are used to store the object statement. Variable has a data type and name. Type indicates the data type of value that can be formed by the variable itself. Variable name must follow the rules of the identifier.


Variable declaration and initialisation


For the variable declaration is as follows,
[= initial value];
Note: The value is between <> is a required value, while values in
marked [] are optional.
Here is an example program and declare that the several
variables,
public class VariableSamples { public static void main (String [] args) { / / data type declarations with the variable name / / Result and data type boolean boolean result; / / data type declarations with the variable name / / Option data type and char char option; option = 'C'; / / mark 'C' as an option / / data type declarations with the variable name / / grade, double data type and has been in the initialisation / / to 0.0 double grade = 0.0; } }


Displaying Variable Data
To remove the value of a variable that you want, we can use the command as follows,

System.out.println()
System.out.print()

Here is an example program,

public class OutputVariable { public static void main( String[] args ){ int value = 10; char x; x = ‘A’; System.out.println( value ); System.out.println( “The value of x=” + x ); } }

The program will remove the following text on the screen,

10 The value of x=A

System.out.println() vs. System.out.print()
What distinguishes between the System.out.println () and System.out.print ()?
The first to add a new line at the end of the data for the excluded, while
not next.
Note the statement,

System.out.print("Hello "); System.out.print("world!");
The statement will produce the following output on the screen,

Hello world!

Now see the following statement,

System.out.println("Hello "); System.out.printin("world!");
The statement will produce the following output on the screen,

Hello
world!


Reference Variables and Primitive Variables
Now we will distinguish two types of variables that are owned by the Java program. There reference variables and primitive variables.
Primitive variables are variables with primitive data types. They store data in the actual memory location where the variable is located.
Reference variables are variables that store addresses in a memory location. The point to a memory location where data is actually located. When you declare
variables in a particular class, you actually declare a variable in the reference classnya in the form of the object is.
For example, if we have two variables with data types int and String.
int num = 10; String name = "Hello"

eg the illustration shown below is the memory of the computer
You, where you have the address of each memory cell, the variable name and data
form as follows.

As you can see, the primitive variable for num, data in the location where the variables are. To reference a variable name, variable only refers to the address where the data is really there.


No comments:

Post a Comment