Recall:    Java program that prints the area of a circle

Program used to illustrate the syntax of variable definition:

public class ComputeArea
{
   public static void main(String[] args)
   {
      double radius;
      double area;
  
      // (1) Assign a radius
      radius = 20;

      // (2) Compute area
      area = 3.14159 * radius * radius;

      // (3) Print result
      System.out.println(area);
                         
   }                       
}
  

DEMO: demo/02-elem-prog/04-vars/ComputeArea.java

The purpose of variables in a computer program

  • Purpose of variables:

    • Variables are defined (= created) inside a program to store information

    Examples:

      radius      // Stores the radius of a circle
      area        // Stores the area of a circle 


  • Syntax (= "format") to define variables in Java:

      datatype  variableName ;    
    

    Examples:

      double radius;    // "radius" stores a floating point number 

Rules of variable definition

Programming rules on variable definition:

  1. A variable must be defined before you can use it:

      int  radius;   // Omitting variable definition will result in error 
    
      radius = 20;   // Error: variable "radius" is not defined


  2. A variable must be initialized before you can use it in a computation:

      radius = 20;    // Omitting initialization will cause error 
    
      area = 3.14159 * radius * radius; // Error: "radius" not initialized

DEMO: use BleuJ   ---   demo/02-elem-prog/04-vars/ComputeArea.java

Identifiers

  • Identifiers are names you use in a program to tag (or identify) "things".

    Example:

      • A variable name is an identifier used to tag a variable


  • Rules for formulating valid identifiers:

      • Sequence of characters consisting of:

          • Letters, digits, underscore or dollar sign           

      • Must begin with a letter, underscore or dollar sign

      • Cannot be a reserved word (see Appendix A in text book)

      • Can be any length

Invalid identifiers:    hello!, O'Neil, 3CPO       ---   Quiz: R2D2 ??

  Variables and different kinds of informations used in computer programs  

  • Recall:

    • Variables are defined (= created) inside a program to store information

    • Computers can only store (binary) numbers


  • Fact:

    • There are many different kinds of information processed (and stored) by a computer program

    Examples:

    • Integral numeric information (e.g.: 4, 9, 19, etc)
    • Fractional numeric information (e.g.: 3.14, 99.99, 19.0, etc)
    • Symbolic information (e.g.: α, 蓐, ב, 😜, etc)

    Therefore:   programming languages must provide many different data types

Use of data type information in a programming language

  • Use of the data type information:

    • The data type tells the computer program how to interprete the data (= number) stored inside the variable

  • Every variable in a Java program must have a data types


  • Java (and every programming language) has a number of built-in or primitive data types:

    • Integer data types                       represent whole values (e.g.: 19)
    • Floating point data types            represent fractional values (e.g.: 9.1×104)
    • Character data type                    represent symbols (e.g.: β)
    • Boolean data type (for logic operations)       true or false

    Comment:   the are many different integer and floating point data types in Java...

  The primitive data types in Java  

  • The primitive (= built-in) data types in Java are:

    • Integer number ( whole number) types:

       byte       // very small integers (small number of digits)
       short      // small integers
       int        // "regular" integers
       long       // large integers
      

    • Floating point number ( fractional) types:

       float      // "regular" floating point numbers
       double     // large floating point numbers
      

    • Character data type:       char

    • Logical data type:            boolean

Type and explain:    Program to show fundamental data types
public class PrimeTypes
{
    public static void main(String[] args)
    {
        double highPrec   = 123.4567890123456789;  // Put breakpoint here
        float  lowPrec    = 123.4567890123456789f;
//      float  lowPrec    = (float) 123.4567890123456789f;

        int    normalInt  = 1234567890;
        short  shortInt   = 12345;
//      short  shortInt   = 1234567890; // Show error
        byte veryShortInt = 123;
//      byte veryShortInt = 12345; // Show error
        long veryLongInt  = 1234567890123456789L;
        
        char    letter = 'a';
        boolean isPrime = true     
        
    }
}
  

DEMO: demo/02-elem-prog/05-prim-types/PrimeTypes.java
Use BleuJ debug window to show the value stored in variables

Value ranges of Java Primitive types


  double:    -1.7976931348623157E+308   ---  +1.7976931348623157E+308 
  float:     -3.4028235E+38             ---  +3.4028235E+38 

  long:      -9223372036854775808   ---   9223372036854775807
  int:       -2147483648            ---   2147483647
  short:     -32768                 ---   32767
  byte:      -128                   ---   127










  

Reading in numbers from keyboard
import java.util.Scanner; // Scanner can be used to reading any type of numbers

public class ReadNumbers
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a double value:");
        double highPrec     = input.nextDouble(); // Convert to double representation
        System.out.print("Enter a float value:");
        float  lowPrec      = input.nextFloat();  // Convert to float representation
        System.out.print("Enter an int value:");
        int    normalInt    = input.nextInt();    // And so on...
        System.out.print("Enter a short value:");
        short  shortInt     = input.nextShort();
        System.out.print("Enter a byte value:");
        byte veryShortInt   = input.nextByte();
        System.out.print("Enter a long value:");
        long veryLongInt    = input.nextLong();
        System.out.print("Enter a boolean value:");
        boolean isPrime  = input.nextBoolean(); 
        
        System.out.println("Done"); // ** Put a breakpoint here
    }
}

DEMO: demo/02-elem-prog/05-prim-types/ReadNumbers.java
Use BleuJ debug window to show the value stored in variables

Short cut to define/create many variables of the same type and variable initialization

  • You can define many (different) variables of the same data type as follows:

      datatype  var1, var2, var3, .... varN; 
    

    Example:

      double radius, area; // Define multiple variables of type "double"
    


  • Optionally, you can initialize a variable at its definition as follows:

      double radius = 20, area; // Initialize radius at its definition 

DEMO: demo/02-elem-prog/05-prim-types/ComputeArea.java

Advanced topics:    Scope and lifetime of variable (will be covered later in course)

  • Scope:

    • Scope of a variable = the locations/places in a computer program where the variable can be accessed

  • Every variables has a scope


  • Lifetime:

    • Lifetime of a variable = the duration in a computer program where the variable exists

  • Every variables has a lifetime


  • Scoping and lifetime rules are not part of the AP Computer Science A curriculum...