Chapter 2

primitive data vs. objects

primitive data are common values like numbers or characters

data type – set of values and operations that can be performed on them (+, -, *, /, %)

object is defined by a class, the data type of the object

class contains methods that represent operations for that object

System.out is an object representing an output device or file – object’s name is out, class is System

println is a method in this class

invoking this method on the class is sending a message to the object

println prints information sent to it, then advances cursor to the next line, print doesn’t

Listing 2.1, p. 53

objects use abstraction, hiding details inside of it

String class

use string literal with “ “

string concatenation to split strings over various lines

Listing 2.2, p. 56

blank line by passing println nothing

escape sequences begin with \

Listing 2.4, p. 59

variables

name for location in memory used to hold a data value

declaring a variable tells compiler to reserve part of memory to hold this variable type, and gives it a name

good practice to initialize variables on declaration

Listing 2.5, 2.6 p. 60

assignment statement – assigns value to variable

variable can only hold one value, so assigning a new one overwrites previous value

can only assign variables values of their type (Java is strongly typed)

constants

similar to variables, but hold a value for the duration of the program

cannot reassign a constant – get a compiler error

use final in the declaration

good practice to name constants with all uppercase letters

primitive data types

integers – no decimal part

byte – 8 bits

short – 16 bits

int – 32 bits

long – 64 bits

float – decimal part

float – 32 bits

double – 64 bits

all are signed

choose variable types wisely to not waste memory

Java assumes all int declarations are int, and all float declarations are double

characters

ASCII character set, 7 bits per character

was extended to 8 bits per character

Java uses Unicode character set, 16 bits per character, supporting 65,536 characters

character literals use ‘ ‘

boolean

two values, true and false

can’t be converted to any other data type

arithmetic expressions

if either or both operands are float, result is float

integer division discards any fractional result

operator precedence

unary operator vs. binary operator

widening conversions vs. narrowing conversions – figure 2.5 and 2.6, p. 71

assignment conversion

arithmetic promotion

casting

creating objects

class name can be thought of as the type of an object

variable can hold a reference to an object

declare a string object, then use new to create one – instantiation

new calls a constructor

use dot operator to access methods

String class, figure 2.7, p.75

methods have return value, or return void

once a string is created, size can’t change – it’s immutable

character in string can be referenced by its index into the string (first character is index 0, next is index 1, etc.)

Listing 2.8, p. 77

strings are special in that they can be initialized on declaration, eliminating use of new

class libraries

set of classes that support development of the program

made up of related classes, called Java APIs (Application Programmer Interface)

figure 2.9, p. 79

use import to reference classes of other libraries

java.lang package classes are automatically imported

Random class is used as random number generator, starting at some seed value, then scaling and shifting the result using the modulus operator

listing 2.9, p. 82

some methods ca be invoked without instantiating an object of that class first, using static methods

examples are in Math class, figure 2.11, p. 84

Keyboard class, listing 2.10 and 2.11, p. 86

NumberFormat and DecimalFormat classes, listing 2.12 and 2.13, p. 89

applets vs. applications

applets are intended to be embedded in an HTML document, transported across a network and executed using a Web browser

application is a stand-alone program executed using a Java interpreter

applets can be viewed locally using a Web browser, and Sun’s appletviewer

Java bytecode is linked to an HTML document and sent across the Web

a version of a Java interpreter embedded in a Web browser is used to executer the applet

applets can be thought of as part of a larger program, since the Web browser must also be running – they don’t have a main method, then

must extend the Applet class to create an applet, and declare the class as public

paint method is automatically called

listing 2.14, p. 93

must reference applet in an HTML document

<applet code=”Einstein.class” width=350 height=175></applet>

must first compile Java program into bytecode for this to work

Graphics class

contains methods for drawing lines, rectangles, ovals, etc.

can fill shape or not

Figure 2.16, p. 96

use Java coordinate system

use Color class to set foreground and background colors

Listing 2.15, p. 99