These are named boxes that hold something. They have four properties. These are a name, location, type and contents. Variables can contain different values at different times, but only one kind of thing.
The name of a thing in Java is called an identifier. An identifier can be any string of characters, but must start with a letter or an underscore, then be followed by any number of digits, letters or underscores. An identifier cannot be the same as one of the keywords of the language. They are case sensitive, so my_name and My_Name are different. Choose identifiers to describe what the object is for, not what it contains. For example, my_integer is a poor choice, number_of_books would be better.
This is the memory address where the variables contents begin. We usually don't care about the actual value of this, but in some cases,\ like C pointers, we will. Mo often, we just need to be able to find it out.
This is the variables size and kind and dictates what it can hold. While there are exception, the different types of variables cannot hold each others contents.
This is what is in the memory location used by the variable.
Variables have two kinds of value. They are called the Right Hand Side (RHS) and the Left Hand Side (LHS). These refer to the sides of the assignment operator, = . The LHS value is the location, its memory address. The RHS value is the contents.
This sets up a variable and specifies its type and name. There is a difference between the declaration and definition of a variable. The declaration tells the compiler the name and type of the variable. The definition sets up the memory space for the variable. These sometimes happen at the same time, sometimes at different times.
When a variable is created and space is set up for it, the contents are undefined. This means that the contents of the variable is whatever was in that space before it was given to the variable. There is some bit pattern in there, but nothing specific. The variable is not set to zero. So it is very important for you to set the variable as soon as you make it. This setting is called initialization.
In Java, variables are declared by the following syntax:
type_name variable_name [,variable_name];
type_name variable_name=initial_value;
int one;
int two,three;
int one = 2, two = 9;
There are several legal values for the type_name part above. These include float (32 bit), char (16 bit, Unicode), double (64 bit), byte (8 bit), int (32 bit), long (64 bit), short (16 bit), boolean.
These are indicated by the keyword int. These are ordinary integer values.
These are two byte variables that contain the Unicode code for
a character.
For example, if a char variable is assigned the letter A,
char x='A'; // note the single quotes
The integer value would be 65. You can do arithmetic on characters.
So after doing
c=c+1;
C would contain 66, which is the letter 'B'. There are special characters
and here is how you denote them
'\n' | A newline character |
---|---|
'\t' | tab |
'\0' | NUL end of string marker |
'\\' | a backslash |
'\'' | a single quote |
'\nnn' | Any ASCII character, the nnn is the octal number of the character |
Boolean variables are either true or false. There is a convention
in Java that the number 0 is false and all other numbers are true.
But in most cases it is better to use the actual Boolean variables rather
than the integer convention.
So given
boolean flag;
This can be set to true or false;
flag = false;
And used in if statements
if(flag) {do true stuff }
These are used to represent numbers with decimal points.
float f=3.5;
Be careful of mixing floats and ints together. In the expression
int i=12;
float f=3.4;
System.out.println(i/f);
will give 4, rather than 3.52.
This is because, when the types are mixed, everything is made into
an integer. And when a float is converted, it is truncated. So 3.4 becomes
3. To fix this, you have to tell the compiler to make the integers into
floats like this.
System.out.println ( (float) i / f);
The (float) is a cast. This temporarily changes the thing after it into the type in the parentheses. So since i is an integer, this changes the integer value in i (12) into a floating point value (12.0) and then the expression works like you would expect.
Expressions are combinations of variables and operators. Statements
are built out of expressions. A single variable is an expression and an
expression is an expression. So, lets make some variables and then make
some expressions.
int a,b,c,d;
a+b
c - d
b - ( a + b)
Java has a large number of operators. They include arithmetic, Boolean
and logical operators. Operators come in two flavors, binary and
unary. Binary operators involve two expressions, unary operators
involve one.
Operators | Binary | Unary |
---|---|---|
Arithmetic | +,-,*,/,% | +,- |
Boolean | ||,&&,== | ! |
Logical | |,& | ~ |
Other | ->,.,= | * |
Expressions have values. The value of a single variable is its contents. So the value of the variable a is its contents. The value of an expression like a+b is the sum of the values of the two variables. The usual rules about precedence that you learned in algebra class apply. You can use parentheses to group parts of an expression.
The assignment operator is used to copy values from one variable to another. Assignment does not alter the source. The contents of one variable is copied to the contents of the other variable. So, the expression a = 2, sets the contents of the variable a to hold the value 2. If we set a couple of variables like this,
Like other expressions, assignment in Java has a value. The value of an assignment expression is the value of the right hand side. So the value of the expression a = b is the value of b. This allows us to create expressions like a = b = 2; This is interpreted to mean that b is set to 2 and a is set to the value of the expression b=2. The value of this expression is 2, so a is set to 2. The fact that assignment has a value is important when we look at conditional statements.