In computer science, a variable is a named location to store a value. (Mathematicians and statisticians use the word in slightly different ways.)
A variable must first be declared:
int x;
In this statement, int
is the variable's type and x
is its name.
It is useful to think of a variable as a box to hold something:
Before the variable can be used, it must be initialized, that is, assigned an initial value:
x = 5;
Now there's something in the box:
Once the variable is initialized, the box always contains exactly one value. It can't be empty and it can't contain more than one value. (Arrays, Strings, and other non-primitive objects get around this limitation by putting a reference in the box.) The value must be of the specified type.
It is common to declare and initialize a variable on the same line:
int x = 5;
The variable, as an expression, now stands for its stored value, so we can do things like:
System.out.println(x);
The variable can be given a different value, replacing what was in the box, with an assignment statement:
x = -17;
The scope of a variable is the region of code where the variable can be accessed. This extends from the point where the variable is declared until the next closing curly brace at the end of an if / else statement, loop, or method. The scope of a method parameter is the entire method.
- Sedgewick and Wayne, Introduction to Programming in Java, Section 1.2
- Horstmann, Core Java, Volume I: Fundamentals, 11th Edition, Section 3.4
- ⭐ Explain the difference between
x = y;
andx == y
. - ⭐ Is the following code legal?
int x = 2; x = 3;
- ⭐ Is the following code legal?
int x = 2; int x = 3;
- ⭐ Is the following code legal?
int variable = 2; int VARIABLE = 3;
- ⭐ What is the scope of variable
n
in the following code snippet?int fac(int n) { int p = 1; for (int i = 1; i <= n; i++) { p = p * i; } return p; }
- ⭐ What is the scope of variable
i
in the following code snippet?int fac(int n) { int p = 1; for (int i = 1; i <= n; i++) { p = p * i; } return p; }
- ⭐⭐ If
alive
is a boolean variable, how canalive == true
be replaced with a shorter expression? - ⭐⭐ Do
x = y;
andy = x;
mean the same thing? - ⭐⭐ Is
3 = x;
a legal statement? - ⭐⭐ How would you swap the values of two variables
x
andy
? - ⭐⭐⭐ How would you swap the values of two variables
x
andy
without using a third variable? - ⭐⭐⭐ Is
a = b = c;
a legal statement?
x = y;
is a statement that causesx
to contain a copy of the value ofy
.x == y
is a boolean expression that is true if and only ifx
andy
contain the same value.- Yes. It doesn't make sense as algebra, where the
=
symbol is used to state that two expressions are equal, but it's fine in Java. It creates a variablex
, assigns it the value 2, and then replaces that value with 3. - No. Once a variable has been declared, you cannot declare a second variable with the same name in the same scope.
- Yes. Since Java is case-sensitive,
variable
andVARIABLE
are different variables. - The scope of
n
is the entire methodfac()
. - The scope of
i
is thefor
loop. - If
alive == true
,alive
is true; if not,alive
is false. In other words,alive
andalive == true
always have exactly the same value. The shorter version is always preferable as it is clearer, is more concise, and avoids the risk of accidentally typingalive = true
(which is an assignment, not a comparison). - No. If
x
was 1 andy
was 2, thenx = y;
would changex
's value to 2, buty = x;
would changey
's value to 1. - No. The value on the left side of an assignment statement must be a variable (or some other place where a value can be stored, like an array element).
-
To remember this, imagine that you have an object (say, a can of beans) in each hand. To swap them, you put one down on the table, move the other one to your newly-empty hand, and then pick up the one on the table. This analogy is slightly strained because
int temp = x; x = y; y = temp;
int temp = x;
doesn't empty outx
, it just copies the value ofx
intotemp
. - Surprisingly, you can accomplish this (for integer types) using the bitwise xor operator:
For boolean variables, you could do the same thing with the logical xor operator
x = x ^ y; y = x ^ y; x = x ^ y;
!=
. - Yes. This copies the value of
c
intob
, and then copies that value intoa
. This works becauseb = c
is also an expression, whose value is the value ofc
. Furthermore, multiple assignments are handled from right to left to allow for exactly this sort of thing.a = b = c;
is therefore equivalent toa = (b = c);
.