How to declare and initialize a variable

A variable in Java has a type, name and a value. A variable may store of value of either:
- a primitive data type such as int, boolean, char.
or
- a reference to an object, also called reference variable.


for example:
int i; //variable i that can store an int value.
boolean isDone; //Variable isDone that can store a boolean value.
//Variables a, f and r that can store a char value each:
char a,f,r;

//Equivalent to the three separate declarations:
char a;
char f;
char r;

What we just deed above is called variable declaration. By declaring variable we are implicitly allocating memory for these variables and determining the value types that can be stored in them.
So, in the example above, we named a variable: isDone that can store a boolean value, but not initialized yet.

Giving a variable a value when declared is called initialization. Further, we can declare and initialize a variable in one go. For example, we could have declared and initialized our variables of the previous example:

int i = 101; //variable i initialized with the value 101:
//variable isDone initialized with the boolean value true.
boolean isDone = true;
// variables a,f and r initialized with the values 'a','f' and 'r' //respectively:
char a = 'a', f='f', r='r';
//
equivalent to:
char a = 'a';
char f = 'f';
char r = 'r';


Analogous to declaring variables to denote primitive values, we can declare a variable denoting an object reference, that's; a variable having one of the following reference types: a class, an array, or an interface.

For instance (see also Class Instantiation ):
//declaring a redCar and a blueCar of class Car.
Car redCar, blueCar;

Please note that declarations above do not create any object of type Car. These are just variables that can store references to objects of class Car but no reference is created yet.

A reference variable has to be instantiated before being used. So:
// declaring and initializing the redCar reference variable
Car redCar=new Car("red");
An object of class Car is created using the keyword new together with the Constructor call Car("red"), then stored in the variable redCar which is now ready to be manipulated.

What are Java primitive data types?

In Java, primitive data types can be categorized within three types:
  1. Integral types:
    • signed integers : byte,short,int and long.
    • unsigned character values, char,denoting all the 65536 characters in the 16-bit Unicode character set.
  2. Floating-point types: float and double, representing fractional signed numbers.
  3. Boolean type: boolean, representing logical values, the two literals, true or false.

A primitive data value can't act as an object in a Java program unless wrapped. Therefore, each primitive data type has a corresponding wrapper class that can be used to represent its value as an object.

What's a Java Literal?

A constant value in a program is denoted by a literal. Literals represent numerical (integer or floating-point), character, boolean or string values.

Example of literals:

Integer literals:
33 0 -9

Floating-point literals:
.3 0.3 3.14

Character literals:
'(' 'R' 'r' '{'

Boolean literals:(predefined values)
true false

String literals:
"language" "0.2" "r" ""

Note: Three reserved identifiers are used as predefined literals:
true and false representing boolean values.
null representing the null reference.



Let's have a closer look at our literals and type of data they can represent:

Integer Literals


Integer data types consist of the following primitive data types: int,long, byte, and short.
int is the default data type of an integer literal.
An integer literal, let's say 3000, can be specified as long by appending the suffix L (or l) to the integer value: so 3000L (or 3000l) is interpreted as a long literal.

There is no suffix to specify short and byte literals directly; 3000S , 3000B,
3000s, 3000b


Floating-point Literals

Floating-point data consist of float and double types.
The default data type of floating-point literals is double, but you can designate it explicitly by appending the D (or d) suffix. However, the suffix F (or f) is appended to designate the data type of a floating-point literal as float.

We can also specify a floating-point literal in scientific notation using Exponent (short E or e), for instance: the double literal 0.0314E2 is interpreted as
0.0314 *10
² (i.e 3.14).

Examples of double literals:

0.0 0.0D 0d
0.7 7D .7d
9.0 9. 9D

6.3E-2 6.3E-2D 63e-1

Examples of float literals:

0.0f 0f 7F .7f
9.0f 9.F 9f
6.3E-2f 6.3E-2F 63e-1f



Note: The decimal point and the exponent are both optional and at least one digit must be specified.


Boolean Literals

As mentioned before, true and false are reserved literals representing the truth-values true and false respectively. Boolean literals fall under the primitive data type: boolean.

Character Literals

Character literals have the primitive data type character. A character is quoted in single quote (').

Note: Characters in Java are represented by the 16-bit Unicode character set.

String Literals

A string literal is a sequence of characters which has to be double-quoted (") and occur on a single line.

Examples of string literals:

"false or true"
"result = 0.01"

"a"

"Java is an artificial language"


String literals are objects of the class String, so all string literals have the type String.



What's are Java keywords?

keywords are reserved identifiers. Predefined identifiers in Java language, that is, they are used in a program exclusively the way already defined by Java. All the keywords are in lowercase. Java compiler enforces this predefined usage. Incorrect usage of keywords if flagged by the compiler and results in compilation errors.

These are the keywords currently defined and used in Java:

abstract default if private this
assert do implements protected throw
boolean double import public throws
break else instanceof return transient
byte enum int short try
case extends interface static void
catch final long strictfp volatile
char finally native super while
class float new switch
continue for package synchronised



Additionally, Java has two reserved keywords which are currently not in use:
const goto

Note: you can't use keywords either used or just reserved by Java.


What's a Java identifier

An identifier is any name in a Java program. Identifiers are used to denote classes, methods, variables and labels.
An identifier in Java can be composed of a sequence of characters, where a character can be either a letter, a digit, a connecting punctuation (such as underscore _ ) or currency symbol (such as $). Anyway, the first character in an identifier cannot be a digit.
Identifiers in Java are case sensitive. For example; car and Car are two different identifiers.

Examples of legal and illegal identifiers in Java:

car is a legal identifier

Car is a legal identifier

be@home contains the character @ which is not legal in an identifier, so the whole identifier is illegal too.By the way @ is not an operator in Java

deja_vu9 is a legal identifier

total_£ is a legal identifier

total-amount contains the character - which is not legal in an identifier, so the whole identifier is illegal too.(however total-amount could be interpreted as legal expression with two operands.)

$_149 is a legal identifier

names99 is a legal identifier

99names is an illegal identifier, it starts with a digit.

لغة is a legal identifier (In Arabic, means "language" if you are too curious :)


Note: Java programs are written in the Unicode character set, so a letter or digit definition is interpreted accordingly.