Suite101

Java Variables

Declaring, Initializing, and the Simple Global

© Ana Mills

Jan 3, 2009
Java, Ana Mills
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 Variables

Primitive 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.

  • int countNum;
  • char letter;
  • float curTotal;

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 Variables

Storing 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:

  • letter="A";

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:

  • countNum=3;
  • curTotal=5.5;

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:

  • String newString="This is a string.";

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.

Operators

Once 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 Java

Global 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.

Summary

Primitive 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.


Java, Ana Mills
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Jan 4, 2009 8:16 AM
Guest :
There's no such thing as "bool" in Java!!!
Also, primitive types AREN'T necessarily a "typical" component of object-oriented languages: they're included in Java, but some would agrue that a true OO language would not have primitives (I believe this is the case for Ruby, for example).
1 Comment: