next up previous contents
Next: Expressions Up: Basic Java syntax Previous: Basic Java syntax   Contents

Scalar variables

The basic variable types are similar to C. The basic types are:

int (4 bytes), long (8 bytes), short (2 bytes)
float (4 bytes), double (8 bytes)
char (2 bytes)
boolean

Some points to note.

  1. The size of each variable is unambiguously fixed (since all Java programs run on a uniform Java Virtual Machine, independent of the local architecture).

  2. The char type is 2 bytes, not 1 byte. Java uses an encoding called Unicode which allows all international character sets to be written as char. Unicode is a conservative extension of ASCII, so if the first byte is 0, the second byte corresponds to the normal ASCII code. We need not worry about Unicode. As far as we are concerned, what is important is that character constants are enclosed in single quotes, as usual--e.g.,

       char c;
       c = 'a';
       c = 'X';
       if (c != '}') {...}
    

  3. There is an explicit boolean type, unlike C. The two constants of this type are written true and false. We can write code like:

      boolean b, c = false;
      b = true;
      b = (i == 7);
    

    Another point is that a typo like if (x = 7) ... will be caught as syntactically illegal since x = 7 returns 7, not the boolean value true.


next up previous contents
Next: Expressions Up: Basic Java syntax Previous: Basic Java syntax   Contents
Madhavan Mukund 2004-04-29