(syllabus and calendar)

Chapter 2: Introducing Data Types and Operators, pp. 35-70

zip2 of examples

Session 2 

Data Types


Operators


Quiz

Data types

A data type is the characteristic of a variable that determines what kind of data it can hold. Java is a strongly-typed language because it requires that every variable and expression have a type. The Java compiler checks for type compatibility among the variables to which you assign values. The Java compiler has a parser and debugger that alerts you of any type mismatch, and you have to fix it before your program will compile.

Although javac generally requires you to specify the data type, for convenience, printing out the primitive types is an exception. For example, javac implicitly assigns a datatype at compile-time for the print and println methods:

System.out.println(22/7); // ints
println(22.0/7.0);  // doubles
System.out.println("22/7"); // one string

Variable Types - Primitives

A given variable is either a primitive (basic type) or an object (custom type). 
Each primitive data type has a range of values, which determines its relatively small size in memory. (Objects can grow quite large.)

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html

Each primitive type has a corresponding class. For example, a variable of type byte corresponds to the Byte class. The corresponding class has a static, final field, MAX_VALUE, that represents the maximum value of that type. What must this field be static? Why should it be final?

The output is:


Integers

Whole numbers without decimals (no floating point). The type used for counters in loops is int.

Type Size Range  Example
Integer types
byte 8 bits -128 to 127

-28 to (28-1)

Colors (RGB)
ASCII characters
short 16 bits -32,768 to 32,767

-216 to (216-1)

Unicode (world languages)
int 32 bits -232 to (232-1) counters in loops
long 64 bits -264 to (264-1) big numbers

Floating-point

Can be float or double, but double is the most important because it is larger and more precise.

Floating point types
float 32 bits (6 digits) 3.40282e+38 floating point (currency, temperature, percentage, length)
double 64 bits (15 digits) 1.79769e+308 double (large numbers or high precision, such as for astronomy or subatomic physics)

The java compiler expects you will perform division by using double instead of float.

Char versus String

A single character, char, is primitive, but a String instance is an object with methods on it.

Special types
char 16 bits (2 bytes) Unicode characters
(whereas other languages use 1-byte ASCII text characters)
http://www.unicode.org/unicode/standard/WhatIsUnicode.html
alphabets and numerals

Character Arithmetic

The alphabet is an ordered collection of letters that represent, for the computer, a numerical sequence.

One the primitive data types is char, for character. Each character on the keyboard has a numeric value. Capital "A" is 65 and lower-case "a" is 97. The ASCII table uses 8-bits (2 *2 eight times = 256) to store values adequate for languages uses characters based on those of Latin, such as German, French, and English. The numeric value underlying each character on the keyboard can be used to do arithmetic. For example, adding 32 to "A" results in "a".

The output is the same as that of the program below, which uses the numerical equivalent of the char.

Another example:

Unicode versus ASCII

Note: To support languages that do not use characters based on Latin, such as Sanskrit, Mandarin, Japanese, Korean, Greek, Cyrillic, Arabic, Hebrew, and even ancient Egyptian hieroglyphs, Java uses 16-bit Unicode. Java (the programming language), therefore, supports the Javanese character set, the pre-Colonial writing system of Java (the island). Some people on Java know Java, and Java can work with Javanese.

boolean versus String

A boolean value (either true or false) is not a literal String (neither "true" nor "false") even though it looks like a String without quotation marks.

Special types
boolean Java reserves 8 bits but only uses 1 true
false

Note: These values are keywords.
logic test with if


Variables versus constants

A variable is a name for a piece of memory that stores a value. Each variable must have a name. For example,

int BOILING_POINT = 100;

BOILING_POINT is the variable of type int and 100 is its literal value, a valid integer value.

Unlike JavaScript and Visual Basic, Java is a strongly-typed programming language.

Numbers are represented in memory using bits. A bit is the smallest unit that a computer can work with. A bit has two possible values, 0 or 1.
Everything in a computer is represented by bits.
A number is represented by a series of bits which represent different powers of 2.

An 8 bit-number  is called a byte:

Value of the number in decimal digits Bits (value in binary digits) - to the power of
0 00000000
1 00000001 = 2^0
4 00000100 = 2^2+0^1+0^0
8 00001000 = 2^3+0^2+0^1+0^0
13 = 8 + 4 + 1 00001101 = 2^3+2^2+0^1+2^0
255 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 11111111 = ...

Note: Unlike C and C++, Java does not have explicit pointers or direct access to memory locations. Instead, a variable references a named place in memory that is internally managed by the Java Virtual Machine (JVM).

Constant

A constant variable is a variable that is assigned a value once and the constant value cannot be changed afterward (during execution of the program).
The compiler optimizes a constant for memory storage and performance. The keyword final signals that the value is fixed.

Examples of possible constants

Literals and escape sequences

A variable is a name (or reference) for a storage area in memory for a value that can change.
A literal is the direct representation of a value.

Boolean Boolean myBool = true;
Boolean defaultValue = false;

(no quotation marks because a Boolean is not a String)
character any character in the 16-bit Unicode character set
a character literal is enclosed within single quotation marks
char myChar = 'z';
String a string object (strings are a special object type in Java that is implemented as an array of chars, the primitive type for a character). A String literal has double quotation marks:
String greeting = "Hello!";
number int yearsOnTheJob = 17;
double inflationRate = .0306;
double sum = 382.36; // sum is a variable, not a literal
String firstName = "Luke"; // "Luke" is a string literal inside quotation marks
boolean isExpensive = false; // booleans are not string literals

character escape sequences (p. 45)

\n	new line
\t	tab
System.out.println("Let's make a \nnew line and a \ttab here.");

Variables

 Declarations and assignments

Declaring a variable

            http://java.sun.com/docs/books/tutorial/java/javaOO/variables.html

Initializing a variable

The expression (an expression is a part of a statement that returns a single value) that initializes a variable (that assigns it a value for initial use) can be an expression with:

In this example, the declaration statement on line 6 is also an expression whose return value initializes the newly declared variable.

Using Variables to Demonstrate Integer Truncation for Division


Code blocks and scope

A block of code is whatever is within a set of curly braces.
{ // open curly brace
{ // close curly brace

{ // start of code block
  // declarations, assignments, control statements, and so on
} // end of code block

Think of a code block as a unit of code. Just as an English sentence can have many clauses and commas, but still be a single unit (the sentence), so many lines of code can belong to a single code block. A class definition resides a code block. We have seen how within a class definition code block, we can have code blocks for a for loop. An if statement can also have multiple statements within its code block.

if (s.length() > s2.length()  { // if true, execute both statements in the block
    System.out.println("s is longer than s2.");
    System.out.println("s2 is shorter than s.");
}

TIP: When you begin a code block, type both the beginning curly brace and the ending curly brace before you fill in the block with code. That way you will not forget to close your code block.

The "scope" of a variable is defined by the code block in which it resides. Scope defines the visibility of variables.

Scope depends on code block

We say, "seeing is believing". Java can only compute with variables that it can see. 
Scope is the block of code in which a variable exists and is available for use.
The scope (or visibility) of a variable depends upon WHERE you declare it. 

You might declare a variable within a block of code that is a:

Another example:

local variable

A data item known within a block, but inaccessible to code outside the block. For example, any variable defined within a method is a local variable and can't be used outside the method.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html

The output is:

QUESTION: Which line numbers define the scope of x? of y?

Sun's tutorial exposes a common error involving the concept of scope:
if (...) {
    int myInteger = 17;
    ...
}
System.out.println("The value of myInteger  = " + myInteger);    // error

The final line won't compile because the local variable myInteger is out of scope. The scope of myInteger  is the block of code between the { and }.

The myInteger variable does not exist after the closing curly brace (}) ends that block of code.

QUESTION: Can you use have an instance variable with exactly the same name as a local variable?

Scope in a for loop example

Let's discuss this example:

and its output:

Value of myInteger is: 1
Value of myInteger is: 2
3 is a multiple of 3
Value of myInteger is: 3
Value of myInteger is: 4
Value of myInteger is: 5
6 is a multiple of 3
Value of myInteger is: 6
Value of myInteger is: 7
Value of myInteger is: 8
9 is a multiple of 3
Value of myInteger is: 9

Operators

Arithmetic operators

Operator Name Use Description
+ addition op1 + op2 Adds op1 and op2
- subtraction op1 - op2 Subtracts op2 from op1
* multiplication op1 * op2 Multiplies op1 by op2
/ division op1 / op2 Divides op1 by op2
% modulus op1 % op2 Computes the remainder of dividing op1 by op2

The program finds the integers between 1 and 100 that are a multiple of 13.

Note that I decrement instead of increment by using -- instead of ++.

The output is:

 

Modulus and Truncation

++ for Post-increment

The output is:

count: 0
count after prefix: 1
count after postfix: 2
count after prefix: 3
count after postfix and end of first loop: 4
Prefix and postfix are the same here because there is no operator, such as assig
nment
count: 5
count after prefix: 6
count after postfix: 7
count after prefix: 8
count after postfix and end of first loop: 9
count at beginnng of 2nd loop: 0
Prefix and postfix are different here because of assignment
pre shows increment now: 1
post shows increment later: 1
pre show increment now: 3
post shows increment later: 3
count at beginnng of 2nd loop: 5
Prefix and postfix are different here because of assignment
pre shows increment now: 6
post shows increment later: 6
pre show increment now: 8
post shows increment later: 8

Relational and Logical Operators

The relational operators compare the value of two operands:

The logical operators (p. 55) are used when evaluating whether a condition is true or false.
These operators are:

Short-circuit Operators

In certain situations, performance improves if the evaluation process is more simple. If you are checking for both A and B must be true, then if A is false, why spend time checking whether B is false? If all you care about is whether A or B is true, and A is true, why spend time checking whether B is true? However, short-circuit evaluation is not always faster (http://en.wikipedia.org/wiki/Short-circuit_evaluation#Possible_performance_penalty) and both kinds of evaluation are supported.

Whereas & is the normal AND operator, && is the short-circuit AND operator.

Similarly, whereas | is the normal OR operator, || is the short-circuit OR operator.

Another example:

Logical Operators Truth Table

Assignment operator

Do not confuse the assignment operator = with the equals to comparison operator ==

int myInt = 7;  // assignment
if (yourInt == myInt)
{
    System.out.println("Two variables represent the same literal value"):
}

Shorthand assignments

x += 10; means x = x + 10;
x *= 10; means x = x * 10;
x %= 10; means x = x % 10;


Statements and Expressions

What is the difference between a comment, a statement, and an expression?

Statements

A statement is a command that you write in your program.
Syntax: a statement must end with a semicolon (;) so that the parser in the Java compiler (javac) knows this is the end of your statement.

System.out.println("Hello Luke"); // pass a string argument to a method
double bodyTemperature = 98.6; // assign a value to a primitive variable
HumanBeing.inalienableRight = pursuitOfHappiness; // assign a value to a property of an object

Comments

Comments are for human code readers. The compiler ignores comments.
Insert comments in your code so that you, and others who will use your code, know what the code is about.
Good comments make code more maintainable, even for the original programmer.
It is more common for code to have too few comments than too many comments.

In the preceding statements, a comment follows each statement. 
Two slash characters (//) define the comment through the end of the current line.

/* A slash followed by an asterisk
defined the beginning of a multi-line comment,
which ends with the reverse, an asterisk, followed by a slash */ 

Expressions

An expression is a statement that returns a value. 
In both examples below, the Java virtual machine will:

  1. Perform a calculation or an evaluation.
  2. Return a return value.
sum = priceOfItem1 + priceOfItem2; // the return value is stored in the variable sum
selfEvidentTruth = HumanBeing.IsCreatedEqual();  // boolean return value must be true or false

Declarations and Assignments

What is the difference between a declaration and an assignment?

A variable is a named location in memory. A declaration is a statement that lets the compiler know that we are going to use a variable to refer to a value of a certain type. Syntax requires that we declare the type before we declare the variable's name.

Declarations without assignments

int temperature;
float drunk;
final int BOILING_POINT;
char[] daysOfTheWeek;
String[] myDays;

Declarations followed by assignments

The declaration statement can be separate from the assignment statement.
The assignment operator is  =
The operand to the left of the assignment operator is assigned (or gets) the value of the operand to the right of the assignment operator.

int temperature;
temperature = 100; // the temperature variable "gets" the value of 100
double temperature;
temperature = 100.0001;

Declarations and assignments

An assignment gives (assigns) a value to a variable. Typically, in Java, we assign a value to a variable when we declare that variable.
However, we can assign a value later. And, we can re-assign a value still later, unless the variable is a constant (keyword final).

int temperature = 70; // Fahrenheit
float drunk = .01; // in percent
final int BOILING_POINT = 212; // a constant is final: no reassignment
// We will learn about arrays in detail later.
// What follows is an array of characters 
// for the 7 days of the week.
// (Single quotes are for chars.)
// Each element has a unique index. 
// The first 's' (Sunday) has the index of zero.
// The second 's' (Saturday) has the index of 6.

char[] daysOfTheWeek = {'s', 'm', 't', 'w', 't', 'f', 's'};
// Use double quotes for strings.
String[] myDays = {"Sunday", "Monday", "Tuesday"};


Precedence of operators

Page 64 has the table of Operator Precedence. Key details include: multiplication (*), division (/), and modulus (%) before addition (+) and subtraction (-). TIP: To ensure addition and subtraction precede multiplication, put the operation in parentheses.


Conversion and Casting

You can cast an int as a double to allow for more precision with division, that is, to allow for fractions..

Automatic Conversion of Long to Double

Type Conversion - automatic

Automatic conversion occurs when your expression involves two data types that are compatible with each other, and the conversion is widening, that is, the destination type accommodates larger values the the source type.

For example,

double myInteger = 1; // widen an integer to a double
double myFloat = 1.0; // widen a float to a double

I do not need to use all the memory it takes to store a double (64 bits) if my value is just an integer (32 bits), but Java allows me to make an assignment that involves this explicit conversion.

Java can "promote" data types when it evaluates an expression.

As the comments state, a byte can be promoted to a float, a char to an integer, a short to a double, an int to a float, and a float to a double.


Explicit Casting Syntax

Type Casting

Casting is the explicit translation of:

In a cast, the Java runtime knows enough about the types and how they are related to do this conversion.

Casting primitives

byte, short, int, long, float, double cast to/from byte, short, int, long, float, double

A widening cast is from a smaller type to a larger type.
A cast from a larger type to a smaller type must be explicit.
The syntax is (targetTypename)valueToCast

Line 9 casts the product of two doubles as an integer.

The output is:

The product as double is 3.75
The product as integer is = 3

Another explicit cast is of integer into byte:

b = (byte) (50 * 2);

Booleans cannot be cast.

LOSS OF PRECISION

When you cast a floating point type to an integer type, or a double to a float, loss of precision might occur.
For example, (int)3.2  truncates the .2 and returns 3.
 
Also, when you cast from a larger integer type to a smaller one, if the number is very large, it might be truncated and return a smaller number, or even wrap around (as byte does) to its beginning.

The output is:

Conversion of int to byte.
When int i = 258 is cast as a byte, the byte value is: 1

Conversion of double to int.
Truncation when double 323.142 is cast as int: 323

Conversion of double to byte.
Casting double 323.142 into byte causes wraparound after 255: 67

Note: Just imagine the porting issue if different platforms dealt differently with the issue of overstepping the limit of the byte data type. One platform might convert the byte back to a double. Another might assign a value of zero. Another platform might crash the program. Still another might hang.


Promotion

An operation involving two byte value causes the resulting byte value to be promoted to int. (p. 67)

Widening primitive conversion

To avoid integer overflow during integer multiplication, if an int is multiplied by a double, Java automatically promotes the int to double for the calculation. This is safe because no loss of precision can occur. In 1996, had the European spaceship, Ariane, been using Java code instead of Ada code, the control software would not have crashed from an overflow of the integer type, and the spaceship itself would not have crashed, a mere 37 seconds after its initial launch. http://en.wikipedia.org/wiki/Ariane_5_Flight_501 (It might, however, have landed in Lake Merritt.)



Quiz - Session 2

  1. Which floating point type is the most important in Java?

  2. Write the following: first the "equal to" operator, then the "assignment" operator.

  3. Write the symbol for the operator that returns the remainder of a division operation.

  4. What are the data types of the following?: first 10.0 and then 10

  5. Write the operators for (a) OR and (b) short-circuit AND

  6. True or false: the two boolean types are the string literals "true" and "false".

  7. Write the escape sequence for a new line.

  8. The ___________ of a variable is the code block in which the variable is declared.

  9. If the compiler error is "possible loss of _____________", you need to cast.

  10. Assume we have declared int BOILING_POINT = 100;
    Write the code that casts BOILING_POINT to float.