|
|
|
Learn about the basic types of Java variables, how to declare those variables, and how to assign values.
Variables are an essential part of any programming language, so the aspiring Java programmer must begin by learning about the types of variables available. Many of these are common or identical concepts in all object-oriented programming languages. Types of VariablesPrimitive variables are those of the primitive datatypes: int, char, boolean, byte, float, short, long, and double. These variables hold data of various lengths and types, yet are all declared in the same way. Declaration is required to warn the interpreter of its presence, much like many governments require citizens to register before they can vote.
The simple structure is as follows: [data type tag] [unique name] [semicolon]; String variables are of the object reference data type -- that is, they store a link to a place in computer memory rather than the data itself. They can store larger groups or strings of characters than primitive variables and are thus useful for communication. Initiating VariablesStoring a value is called initialization in Java and works a lot like algebra. If the char variable named letter needs to store a value, a letter in this case, simply set it equal to the letter:
Note that the letter A is not by itself, but is surrounded by apostrophes and ended by a semicolon. The apostrophes indicate that the A is a literal to be stored. Without them, the variable letter would be set to a variable A, which result in an error because variable A doesn't exist. Assigning numbers is a little different:
The semicolon follows every line of Java as a terminator, like the period at the end of this sentence. The simplest way to initiate a String variable is similar:
Note that it is legal to declare and initiate a variable in the same sentence, as above. Choose the method that suits personal preference and clarity. OperatorsOnce a number has been stored in a variable, operators are used to make calculations with those variables. This is not limited to simple mathematics; operators can be used to create counters for loops, for example. The basic operator is for assignment, the = symbol store a value or a variable into a memory space. Several types of operators are used in Java, such as arithmetic, relational, logical, conditional, bitwise, and compound operators. Globals in JavaGlobal variables are those that can be used throughout a program or programs. If the float variable curTotal is a global, it can be called from anywhere. In order to use globals, they must be declared in a special static class: public class Global{ public static float curTotal=0; } This class is named Global, but any legal name can be used. Now the curTotal can be called in any class in the same program: public class Global{ public static float curTotal=0; } public class total { public static void main(String args[]) { Global.curTotal = Global.curTotal + 100; System.out.println(Global.curTotal); } Calling curTotal follows the standard syntax: Class.variable. Globals can also be imported from an outside class. SummaryPrimitive variables are those that store data directly, often a small amount. Object reference variables store links to data in memory, like strings of text. Variable must be declared before they can be initiated or used, but often both can be done at the same time. Operators are used to assign the data to those variables and strings, and to manipulate and compare the variable data. Java 5 was the first version to handle global variables, or those that can be referenced consistently throughout a program.
The copyright of the article Java Variables in Javascript/Java Programming is owned by Ana Mills. Permission to republish Java Variables in print or online must be granted by the author in writing.
Comments
Jan 4, 2009 8:16 AM
Guest
:
1 Comment:
|
|
|
|
|
|
|
|