Version 3.21
Two class days
Handouts:
System.out.print and System.out.println—what's the difference?
println begins a new line after what it printsprintln can have 0 or 1 parameters+- p. 66: Syntax of variable declaration - What does the syntax diagram mean? - Examples? - Reserved words: int, double, char, final - Listing 2.5: PianoKeys.java - Assignment (=): - p. 69: syntax - Listing 2.6: Geometry.java - Java is a strongly typed language: each variable must be declared with a type, and can only store values of compatible types. - Declaring constants with final
byte = 8 bits signedshort = 16 bits signedint = 32 bits signedlong = 64 bits signedfloat = 32 bitsdouble = 64 bitschar = character (Unicode?), 16 bitsboolean = logical, 1 bit (at least conceptually)
true, falsePage 72: Syntax of a decimal integer literal
int and double.F or f (float), L or l (long).Besides primitive types, Java also has reference types (objects).
String+, -, *, /, %()+ and - (unary, as signs)*, /, and % left to right+ and - (binary, meaning add and subtract) left to right9.0 / 5.0 instead of 9 / 5?++, --+=, -=, *=, etc.By casting:
(long) 21
(double) 21
(float) 2.1
Narrowing conversions (to a smaller data type, may lose information) and widening conversions (to a larger data type, usually do not lose information—when might they ever?)
Example: Lossy.java
In the second half of this chapter, the content changes from core Java language features to some of the important Java libraries. In addition, we will begin using classes that somebody has already defined. We will look in greater depth at using classes and objects in Chapter 3, and at writing our own classes in Chapter 4. For the time being, we will write only simple classes with a main method and nothing else.
abc 123 45.67Setting up:
Scanner in0 = new Scanner(System.in); // stdin
Scanner in1 = new Scanner("1 2 3 4"); // "reads" from the string
Scanner in2 = new Scanner("friends.data"); // ditto
Scanner in3 = new Scanner(new File("friends.data")); // file input
nextInt() ⇒ intnextDouble() ⇒ doublenext()
StringnextLine() ⇒ StringhasNext() ⇒ true or false
Some colors have names: Color.blue, etc.
javax.swing.JApplet.
main method
paint method is run automatically whenever the applet needs to be displayed.run method; but at this stage, we are just using the method inherited from JApplet.paint method uses methods of the class java.awt.Graphics (also called a graphics context). We may think of a Graphics object as something that can be drawn or painted on; a "page" for drawing; a graphical output device.java.awt.Graphics (see Figure 2.12 on p. 99):
draw/fill-Rect/Oval/LinedrawStringgetColorsetColor