To write output to a console window:
String name = "Dr. Seuss";
int age = 65;
System.out.print("My name is " + name +
" and my age is " + age);
Here the symbol + is used as a concatenation operator and not numerical addition. We say + is an overloaded operator. Java knows how to interpret the + symbol by the context.
In Visual Cafe, this program will cause the output to appear in the console window.
Console output is easy but console input is a mess!
Class for reading input: InputBox
Class for writing output: OutputBox (see page 112 for list of methods)
For example, click here.
Suppose you are asked to convert some number of cents into the correct change. Assume you have dollar bills, quarters, dimes, nickels, and pennies. How would you do it?
<variable> = <expression>;
For Example:
int height;
height = 34;
CORRECT: <variable> = <expression>;
int width, height;
width = 476;
height = 23;INCORRECT: <expression> = <variable>;
2 = width;
width + 2 = height;
width + height = 43
An expression is something that has a value.
Literals:
(named) variables:
int width1, width2 = 23;
width1 = width2;
Here width2 can be treated as an expression with value 23.
Objects
Any object is an expression
The value is the location of the Object. Recall:
Player p1, p2;
p2 = new Player();
p1 = p2;
Here p1 is set to the value of p2.
Return value of a message
int myRoll, myCheat;
myRoll = dice.roll(2);
Any mathematical expression (more on syntax later).
width = 34 * 12;
myRoll = 3 + dice.roll(2);
You must always set the value of a variable before using it. This is referred to as initializing the variable. This can be done at declaration time for primitives or objects:
Player p = new Player();
int width = 23;
Or later
int width;
width = 23;
What happens if you forget to initialize?
Player p;
p.takeTurn(); // p not initialized
Here, p doesn't point to anything so the compiler will give a Null pointer exception.
int area, height, width=4;
area = width * height;
Here, height is not initialized. Most compilers (but don't count on it!) will probably initialize height to 0. Some compilers will complain and not even let you compile. Bottom line: you can't predict what will happen - this is bad programming!
Integer Arithmetic: +, -, * , e.g. 7 * 5
Integer Division: / , %
7 / 2 - takes only integer part
7 % 2 - remainder after dividing
27.5 + -27.2
Which Operator Goes First? see page 94 in text
High Precedence (performed first)
() |
unary -, + |
* / % |
+ - |
Low Precedence (performed last)
Examples:
int number = 5 * 7 - 15 % 6 + 3/4*5 + 3/(4*5) ;
Short-Cuts
Assignment Operators:
+=, -=, *=, /=, %=<
Increment, Decrement, ++, -- , Pre and Post
Relational Expressions
Comparision Operators <, >, ==, !=, <=, >=
Have Boolean Value
E.g.
boolean isSmaller = (5 < 8 );
Logical Expressions: AND (&&), OR (||), NOT (!)
These operate on boolean values
AND
x |
y |
x && y |
T |
T |
T |
T |
F |
F |
F |
T |
F |
F |
F |
F |
OR
x |
y |
x || y |
T |
T |
T |
T |
F |
T |
F |
T |
T |
F |
F |
F |
NOT
x |
!x |
T |
F |
F |
T |
This means that if you create variables of different types, you can't always use them together.
Often numbers can be combined but be careful - the results may not always be what you expect!
Example 1:
int age = 45;
char letter = 'k';
letter = age; // WON’T WORK
Example 2: Mixing numerical types
int age = 45; double fraction = 0.4; fraction = age; // WILL WORK age = fraction; // WON’T WORK (info can be lost)
Can force type change by type casting.
age = (int) fraction; // WILL WORK
Example 3: Mixing apples and oranges
int person = new Player(); // WON’T WORK
Dice mydice = new Player(); // WON’T WORK
Value must be set at declaration and can't be changed.
Convention: constants are all upper case
final int ID = 1234;
Why use constants?? Suppose you have two unrelated quantities with the same value:
final int SIDES = 6;
final int RESOLUTION = 6;
final int SIX = 6;
Contains methods for standard math functions.
Note the format for message passing is a little different than what we have seen before because these methods are class methods and not instance methods.
Instance methods:
<object>.<method name>(<arguments>)
Class methods:
<class name>.<method name>(<arguments>)
Why make them class methods?? It is more convenient because we need not create a Math object in order to use these methods.