Home » Java Basics » 02 - Java Concepts
2

Variables

Words that hold a value of a data type or class

A ‘variable’ is a word in a Java program that holds a value of a specific data type or object class. Variables may be initially set to a value and modified later on. All the variables used by a class or method should be declared within the method or class. Java is case sensitive - 'myVar' and 'myvar' are taken to be two variables. Instance variables are conceptually similar to the properties of an object. They are declared in the beginning of the class and each new object of the class' type has its own unique value of every instance variable. Sometimes, a variable may contain the word 'static' in front of it. Even if a thousand objects of the class' type are created, only one copy of a static variable exists. For this reason, such variables are called Class variables. Instance and Class variables may also be referred to as 'fields'. Local Variables are used within methods to store temporary data. The type of a variable may be construed from its location - any variable declared within the code of a method is local. Local variables cannot be accessed outside the method in which they are declared. Sometimes, input values are passed into a class or method from the outside. Such values are referred to as parameters. An object class' fields, methods, and nested classes and objects are called its members.

Variable names may be of any length and may contain any character. However, the first character of a variable name should be a letter, a '$' symbol, or an underscore '_'. It is best to use letters as its first character. Variable names that contain multiple words (like gotopage, for instance) should use a lowercase letter for the first character in the first word and uppercase letter for the first character in subsequent words (goToPage). Also, Java's keywords can not be used as variable names - names like 'class' or 'implements' that are part of Java's syntax are not allowed. Since Java is strongly typed, no undeclared variable name may be used. Following are Java's primitive data types:

Type

Description

Default Value

Example

short

16-bit signed integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive)

0

short myShort = 0;

int

The int data type is a 32-bit integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)

0

inMyInt = 25;

long

The long data type is a 64-bit integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive)

0L

myLong = 86500505030405050040560405;

float

The float data type is a single-precision 32-bit floating point number used for decimal values

0.0d

float x = 12.5;

double

The double data type is a double-precision 64-bit floating point used for decimal values

0.0d

double xx = 12.35;

boolean

The boolean data type has only two possible values: true and false

false

boolean myCondition = true;

char

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). Each code represents a character.

\u0000

char b = 'b';

Object

Other than the above primitive data types, variables that hold objects may be created. Also, objects are useful in representing important types like strings (java.lang.String) and currency (java.matd.BigDecimal)

null

String myString = new String();

  1. Local variables are never assigned default values; Compile time errors occur if local variables are not initialized.
  2. The 'new' keyword is never used with primitive data types - this keyword creates a new instance of an object
  3. Any variable assigned to a constant value (e.g.. x=1.0) is called a 'literal'