Expressions and Types in Java


Expressions

Expressions in Java are similar to expressions in Cinnameg. There are a few important differences.

  1. A few operators are different. The mod operator is written % in Java, and the not-equals test is written !=. Boolean operators are also different. The and operator is written &&, the or operator is written ||, and the not operator is written !.

  2. The / operator is used for both real division and integer division (`div` in Cinnameg). Because of that, 2/3 is 0, since it is the integer quotient, but 2.0/3.0 is about 0.6666666666666666. Be careful about that.

  3. Functions have longer names. The Java square-root function is called Math.sqrt. (The name has a dot in it.) The absolute-value function is called Math.abs.

Operators and functions
Cinnameg Java Different?
25 25  
16.2 16.2  
false false  
true true  
x + y x + y  
x - y x - y  
- x - x  
x * y x * y  
x / y x / y  
x `div` y x / y yes
x `mod` y x % y yes
x < y x < y  
x <= y x <= y  
x > y x > y  
x >= y x >= y  
x == y x == y  
x =/= y x != y yes
x and y x && y yes
x or y x || y yes
not(x) !(x) yes
sqrt(x) Math.sqrt(x) yes
abs(x) Math.abs(x) yes

Types

Java has types that are similar to Cinnameg's. Here is a table of correspondences.

Type names
Cinnameg Java
Integer int or long
Real double
Boolean boolean
Char char
String String

The types need to be written exactly as shown. All of the types int, long, char, boolean and double start with lower case letters, but String starts with an upper case letter.

Java integers are limited in size. An integer of type int can only be up to about 2 billion in absolute value. An integer of type long can be up to about 8 billion billion. This limit has a strange effect. If you count up and up, eventually you reach the largest integer that you can store of a given type. Adding 1 to that integer actually yields a negative integer!

There are other types of numbers in Java, including short (an integer up to about 32,000 in absolute value), byte (an integer from -128 to 127) and float (a real number with about half the precision of a double integer). We will only use integer types int and long and real number type double in theses notes.


More information

Java expressions are discussed in Chapter 2.1 of Savitch and Carrano.


Problems

  1. [solve] What is the value of Java expression 9/4? Be careful. You can try it in the box below. Before submitting, write the actual value in the box.

  2. [solve] What is the value of Java expression 9 % 4? You can try it in the box below. Before submitting, write the actual value in the box.

  3. [solve] What is the value of Java expression 9 <= 4? You can try it in the box below. Before submitting, write the actual value in the box.

  4. [solve] What type does a real number have in Java?

  5. [solve] What types can in integer have in Java?


Summary

Expressions in Java are similar to expressions in Cinnameg. A few operators are written differently.

Types in Java have different names, but are similar in meaning to those in Cinnameg. An important difference is that, in Java, integers are limited in size. Type int allows integers up to about 2 billion in absolute value, but type long allows them to be up to about 8 billion billion.