Variable is name of reserved area allocated in memory.
Exactly how a variable is declared depends on what type of variable it is (non-static, static, local, parameter). However, there are certain similarities that
In Java you declare a variable like this:
type name ;
Instead of the word type
, you write the data type of the variable. Similarly, instead of the word name
you write the name you want the variable to have.
Here is an example declaring a variable named myVariable
of type int
.
int myVariable;
Here are examples of how to declare variables of all the primitive data types in Java:
byte myByte;
short myShort;
char myChar;
int myInt;
long myLong;
float myFloat;
double myDouble;
Here are examples of how to declare variables of the object types in Java:
Byte myByte;
Short myShort;
Character myChar;
Integer myInt;
Long myLong;
Float myFloat;
Double myDouble;
String myString;
Notice the uppercase first letter of the object types.
When a variable points to an object the variable is called a "reference" to an object. I will get back to the difference between primitive variable values and object references in a later text.
The rules and conventions for choosing variable names are covered later in this text.
Java Variable Assignment
Assigning a value to a variable in Java follows this pattern:
variableName = value ;
Here are three concrete examples which assign values to three different variables with different data types
myByte = 127;
myFloat = 199.99;
myString = "This is a text";
The first line assigns the byte value 127 to the byte variable named myByte
. The second line assigns the floating point value 199.99 to the floating point variable named myFloat
. The third line assigns the String value (text) this is a text
to the String variable named myString
.
You can also assign a value to a variable already when it is declared. Here is how that is done:
byte myByte = 127;
float myFloat = 199.99;
String myString = "string value";
Java Variable Reading
You can read the value of a Java variable by writing its name anywhere a variable or constant variable can be used in the code. For instance, as the right side of a variable assignment, as parameter to a method call, or inside a arithmetic expression. For instance:
float myFloat1 = 199.99;
float myFloat2 = myFloat1; // right hand side value in assignment
float myFloat3 = myFloat2 + 123.45; // as part of arithmetic expression
System.out.println(myFloat3); // as parameter in method call.
Java Variable Naming Conventions
There are a few rules and conventions related to the naming of variables.
The rules are:
- Java variable names are case sensitive. The variable name
money
is not the same as Money
orMONEY
.
- Java variable names must start with a letter, or the $ or _ character.
- After the first character in a Java variable name, the name can also contain numbers (in addition to letters, the $, and the _ character).
- Variable names cannot be equal to reserved key words in Java. For instance, the words
int
or for
are reserved words in Java. Therefore you cannot name your variables int
or for
.
Here are a few valid Java variable name examples:
myvar
myVar
MYVAR
_myVar
$myVar
myVar1
myVar_1
There are also a few Java variable naming conventions. These conventions are not necessary to follow. The compiler to not enforce them. However, many Java developers are used to these naming conventions. Therefore it will be easier for them to read your Java code if you follow them too, and easier for you to read the code of other Java developers if you are used to these naming conventions. The conventions are:
- Variable names are written in lowercase. For instance,
variable
or apple
.
- If variable names consist of multiple words, each word after the first word has its first letter written in uppercase. For instance,
variableName
or bigApple
.
- Even though it is allowed, you do not normally start a Java variable name with $ or _ .
- Static final fields (constants) are named in all uppercase, typically using an _ to separate the words in the name. For instance
EXCHANGE_RATE
or COEFFICIENT
.