Variables and Types
Types are containers for data -- you can think of them as small
packing crates. The following code declares a single variable named "a".
Int1.java |
1 | public class Int1 { |
2 | public static void main(String[] args) { |
3 | int a; // Declare a |
4 | a = 3; // Assign a. |
5 | int b = 4; // Declare and assign b |
6 | a = a + b; // Updates a, adding b |
7 | System.out.print("a="); |
8 | System.out.print(a); |
9 | System.out.println(); |
10 | } |
11 | } |
$ javac Int1.java
| $ java Int1
a=7
|
The text "int a" creates a box capable of holding one integer. We use the "=" character
to fill it (line 4) or update it (line 6).
We have also shown a variation on the print function. print() simply writes to the screen
without appending a newline. If you want the newline, use println().
Int2.java |
1 | public class Int2 { |
2 | public static void main(String[] args) { |
3 | int a = 3; // The type of the variable is "int", the name is "a" |
4 | a += 4; // Updates a, adding 4 |
5 | System.out.print("a="+a); |
6 | } |
7 | } |
$ javac Int2.java
| $ java Int2
a=7 |
Here we've introduced a shorthand "a += 4" means the same as "a = a + 4". There are
a number of other bits of shorthand that work similarly: /=, -=, *=, and %=.
Int3.java |
1 | public class Int3 { |
2 | public static void main(String[] args) { |
3 | int a = 2, b = 9, c = a; |
4 | System.out.print("a="+a+", b="+b+", c="+c); |
5 | } |
6 | } |
$ javac Int3.java
| $ java Int3
a=2, b=9, c=2 |
You can declare and initialize several variables on one line if you like. Int3 shows you how.
Prim.java |
1 | public class Prim { |
2 | // The eight primitive types: int, short, float, double, char, long, |
3 | // boolean, and byte. There is also a type called "void" and it is a way of saying "nothing" or "no value." |
4 | public static void main(String[] args) { |
5 | int a = 3; // a signed 4 byte value, |
6 | short b = 2; // a signed 2 byte value |
7 | float c = 1.34e2f; // put a f at the end to make this a float, 4 bytes |
8 | double d = 1.96e1; // like a float, but with 8 bytes for greater precision |
9 | // and larger possible absolute value. |
10 | char e = 'E'; // single quotes for character constants, 2 byte value |
11 | long f = 9; // an 8 byte value |
12 | boolean g = true; // the other possible value is 'false' |
13 | byte h = 3; // a signed 1 byte value |
14 | System.out.println("nothing to do!"); |
15 | } |
16 | } |
$ javac Prim.java
| $ java Prim
nothing to do!
|
Definitions:
- A signed value means the number can be either positive or negative.
- Boolean is a fancy name for a value has only two states: true or false.
- A byte consists of eight bits.
- A bit is a value that can either be 1 or 0. Using one bit you can have
an integer with a maximum value of 1 and a minimum value of zero. Using three
bits, for example, you can create a value from zero to seven.
binary | decimal |
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |
4*bit2+2*bit1+bit0 | decimal value |
For signed numbers we can use one bit to identify whether the number is positive
or negative. A Java integer is a signed 32-bit number and can have a value anywhere
from -2147483648 to 2147483647.
- Primitive types are types that are built-in to Java are handled specially. They
are distinguished from Objects, which are types that programmers are free to invent.
There are, however, some special objects that are built-in to java. Two good
examples are strings and arrays.
|