Variables, Identifiers and Data Types

Variables are used for data that can change during program execution. All variables have a name, a type, and a scope. The programmer assigns the names to variables, known as identifiers. An Identifier must be unique within a scope of the Java program. Variables have a data type that indicates the kind of value they can store.

 

Data Types

The data type indicates the attributes of the variable, such as the range of values that can be stored and the operators that can be used to manipulate the variable. Java has four main primitive data types built into the language. We can also create our own composite data types.

Java has four main primitive data types built into the language. We can also create our own data types.

  • Integer:  byte, short, int, and long.
  • Floating-point:  float and double.
  • Character:  char.
  • Boolean:  true or false.

 

The following chart summarizes the default values for the Java built in data types.

Table 1: Java Data Types

Data Type

Default Value (for fields)

Range

byte

0

-127 to +128

short

0

-32768 to +32767

int

0

-2,147,483,648 to 2,147,483,647

long

0L

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

0.0f

 According to single-precision 32-bit  IEEE 754 floating point format

double

0.0d

According to double-precision 64-bit IEEE 754 floating point format

char

‘\u0000′

0 to 65535

boolean

false

Is not precisely defined

 

 

Variable Declaration

When we declare a variable we assign it an identifier and a data type.

Syntax

type variable = value;

The term “type” can be a primitive type or reference type (i.e. non-primitive type). Examples of reference types are String or any user-defined type.

For Example,

String message = "hello world";

In the above statement, String is the data type for the identifier named message. If we don’t specify a value when the variable is declared, it will be assigned with the default value for its data type.

 

 

Identifier Naming Rules

  • Can consist of upper and lower case letters, digits, dollar sign ($) and the underscore (_) character.
  • Must begin with a letter, dollar sign, or an underscore
  • Are case sensitive
  • Keywords cannot be used as identifiers
  • Within a given section of our program or scope, each user defined item must have a unique identifier
  • Can be of any length.

 

 

Types of Variables

There are three types of variables in Java:

  1. Local variables are declared in a method, constructor, or block. When a method is entered, an area is pushed onto the call stack. This area contains slots for each local variable and parameter. When the method is called, the parameter slots are initialized to the parameter values. When the method exits, this area is popped off the stack and the memory becomes available for the next called method. Parameters are essentially local variables which are initialized from the actual parameters. Local variables are not visible outside the method.
  2. Instance variables are declared in a class, but outside a method. They are also called member or field variables. When an object is allocated in the heap, there is a slot in it for each instance variable value. Therefore an instance variable is created when an object is created and destroyed when the object is destroyed. Visible in all methods and constructors of the defining class, should generally be declared private, but may be given greater visibility.
  3. Class / static variables are declared with the static keyword in a class, but outside a method. There is only one copy per class, regardless of how many objects are created from it. They are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.

See the program below for demonstration.

VariableExamples.java

public class VariableExamples {
int a = 100; //instance variable
static int b = 50; //class variable

public static void main(String[] args) {
int c = 10; //local variable
System.out.println("c = " + c);
}
}

 

 

Literals

We may have noticed that the new keyword isn’t used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in our code without requiring computation. As shown below, it’s possible to assign a literal to a variable of a primitive type:

boolean result = true;     
char capitalC = 'C';    
byte b = 100;      
short s = 10000;      
int i = 100000;

The integral types (byte, short, int, and long) can be expressed using decimal, octal, or hexadecimal number systems. Decimal is the number system we already use every day; it’s based on 10 digits, numbered 0 through 9. The octal number system is base 8, consisting of the digits 0 through 7. The hexadecimal system is base 16, whose digits are the numbers 0 through 9 and the letters A through F. For general-purpose programming, the decimal system is likely to be the only number system we’ll ever use. However, if we need octal or hexadecimal, the following example shows the correct syntax. The prefix 0 indicates octal, whereas 0x indicates hexadecimal.

int decVal = 26;     // The number 26, in decimal     
int octVal = 032;   // The number 26, in octal    
int hexVal = 0x1a;   // The number 26, in hexadecimal

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;     
double d2 = 1.234e2; // same value as d1 in scientific notation    
float f1  = 123.4f;

Literals of types char and String may contain any Unicode (UTF-16) characters. If our editor and file system allow it, we can use such characters directly in our code. If not, we can use a “Unicode escape” such as ‘\u0108’ (capital C with circumflex), or “S\u00ED se\u00F1or” (Sí Señor in Spanish). Always use ‘single quotes’ for char literals and “double quotes” for String literals. Unicode escape sequences may be used elsewhere in a program (such as in field names, for example), not just in char or String literals.

The Java programming language also supports a few special escape sequences for char and String literals: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \” (double quote), \’ (single quote), and \\ (backslash).

There’s also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There’s little we can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

Finally, there’s also a special kind of literal called a class literal, formed by taking a type name and appending “.class”; for example, String.class. This refers to the object (of type Class) that represents the type itself.