Primitive Types and Variables

Tom Kelliher, CS 116

Oct. 6, 2000



Lab 4 write-up and applet due Monday.

PostLab 4 due Wednesday.

Exam next Friday.


Read 5.3.

From Last Time

Lab 4.


  1. Quiz.

  2. Primitive types.

  3. Variables.

Coming Up

Access, operators, and expressions.

Primitive Types

What is a primitive type?

How do classes relate to primitive types?

Families of primitive types:

  1. Integer: byte, short, int, and long.

  2. Floating point: float and double.

    15.0, -12.345e-13f, 10.0e6.

  3. Character: char.

    'a', ' ', '\n', '\t', '\\ ', '\'', '\"'.

  4. Logical: boolean.

    true, false.

Default values, if not explicitly initialized.

Identifiers and Keywords

  1. Valid characters.

  2. Rules for forming.

  3. Keyword examples: class, private.

    Not keywords: Applet, GridLayout.


Entities whose value we can change.

  1. Instance variables.

    Recall: the public instance variables of an object can be accessed thus:


    What are they?

    Where are they?

  2. Local variables.

    What are they?

    Where are they?

  3. Scope and shadowing.

    The region of code in which a variable is visible.

    A local variable can shadow an instance variable.

    Consider this example:

    class Example
       private int i = 1;
       public int k = 20;
       private static int l = 5;
       public void Method1()
          int i = 2;
          i = 3;   // Which i?
          // ...
       private void Method2()
          int j = 11;
          // ...

    Scope of instance variable (or method): entire class.

    Scope of local variable: from point of declaration until end of method.

    Identifier names must be unique within a particular scope level.

  4. Lifetime.

    How long does a variable exist?

  5. static and final.

    A static instance variable becomes a class variable --- it is shared by all class objects. How is this different from an instance variable?

    Consider two Example objects and their relationship to the instance and class variables.

    Applying final to an identifier means that it can not be changed. Example:

    static final double PI = 3.14159;

Thomas P. Kelliher
Thu Oct 5 12:12:42 EDT 2000
Tom Kelliher