ERROR ON PREV
Variables and Types
  1. "Hello, World!"
  2. Variables and Types
  3. Arrays
  4. While, If, For
  5. ...Problem Set 0
  6. Static Methods
  7. Static Fields
  8. String Conversion
  9. Objects
  10. Threading
  11. Strings
  12. ...Problem Set 1.5
  13. Packages
  14. Complex Numbers
  15. Abstract classes
  16. Interfaces
  17. Autoboxing
  18. ...Problem Set 1
  19. enum
  20. Inner Classes
  21. Polymorphism
  22. Tanks!
  23. Callbacks
  24. Exceptions
  25. File I/O
  26. ...Problem Set 2
  27. Regular Expressions

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
1public 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
1public 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
1public class Int3 {
2    public static void main(String[] args) {
3        int a = 2b = 9c = 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
1public 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:

  1. A signed value means the number can be either positive or negative.
  2. Boolean is a fancy name for a value has only two states: true or false.
  3. A byte consists of eight bits.
  4. 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.
    binarydecimal
    0000
    0011
    0102
    0113
    1004
    1015
    1106
    1117
    4*bit2+2*bit1+bit0decimal 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.
  5. 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.