Chapter 9
Data and
Variables
9.1 Background
- The task of programs is to process information
- Information is presented to programs in raw facts, as data.
- To become information and knowledge, the data need to be organized and evaluated.
- We are interested here in presentational aspects of data.
- Processing aspects of data will be considered later
9.2 Literals
- Character-based representations for data values are said to be literals.
- The Java language offers literals for 6 primitive data types (int, long, double, float, boolean, char), and for the
String class type.
| Name | Constants (Literals) |
| integer numbers |
| int | 0, 10, -12 |
| long | 0L, 10L, -12L |
| short | |
| byte | |
| real numbers |
| double | 0.0, -5.1, 3e-2 |
| float | 0.0F, -5.1F, 3e-2F |
| other types |
| boolean | false, 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.
- Variables are named memory cells.
- Java variables are classified into: fields, parameters, and local variables.
- Local variables are defined within bodies of methods and constructors.
- When no confusion may arise, the different kinds of variables are referred to just as variables.
- Fields, parameters, and local variables are defined with instructions of the form
<optional modifiers> <type> <name>
class Variables{
static int field;
void method(int parameter) {
int variable;
}
}
- In the case of fields and local variables, a single instruction may define more than one variable.
<optional modifiers> <type> <comma-separated list of names>
class example{
int first, second;
}
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.
- The program should define exactly one local variable for each primitive data type, and one local variable of type
String.
- 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.
- 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