Chapter 9
Data and Variables

   9.1 Background
   9.2 Literals
   9.3 Variables
   9.4 Assignment Instructions
   9.5 Initialization of Variables
   9.6 Assignment #10: Primitive Types and Wrap Classes

9.1 Background

9.2 Literals

Name Constants (Literals)
integer numbers
int0, 10, -12
long0L, 10L, -12L
short
byte
real numbers
double0.0, -5.1, 3e-2
float0.0F, -5.1F, 3e-2F
other types
booleanfalse, true
char'a', '?', '\'' (apostrophe), '\"' (quotation mark), '\n' (newline), '\\' (backslash)
String "abc"
class literals { 
   public static void main (String[] args){ 
    System.out.println( 123 ); 
    System.out.println( 12.3e-5 ); 
    System.out.println( '\'' ); 
    System.out.println( true ); 
    System.out.println( "Hello \"World\"!" ); 
} } 

9.3 Variables

Programs use variables for carrying values through computations.

9.4 Assignment Instructions

9.5 Initialization of Variables

Exercises

9.6 Assignment #10: Primitive Types and Wrap Classes

Due: Mo, Apr 28, midnight

Write a program that satisfies the following specifications.

  1. The program should define exactly one local variable for each primitive data type, and one local variable of type String.
  2. For each available type of literals: choose a literal, assign the literal to a local variable of the same type, and print out the value of the variable.
  3. For each variable in the program: find a static field of the same type in the Java 2 platform packages, assign the field to the variable, and print out the local variable value.
class example { 
  public static void main (String[] args){ 
    // code fragment for the primitive data type int 
    int intVar; 
    intVar = 12; 
    System.out.print( "integer: " ); 
    System.out.print( intVar ); 
    System.out.print( " (literal), " ); 
    intVar = java.lang.Integer.MAX_VALUE; 
    System.out.print( intVar ); 
    System.out.println( " ( java.lang.Integer.MAX_VALUE ) " ); 
    // the String and the other primitive data types  were 
    // not considered. 
} } 
Assume ‘lab10’ for the submit program. The program should be stored in a file named ‘lab10.java’, and assume the name ‘lab10’ for the class.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

Q&A