AP Comp Sci Notes 3.23.4
3.2: Basic Java Syntax and Semantics
Java Numeric Data
Types:
int à integer à 4 bytes
Can store #s from: -2,147,483,648 to 2,147,483,647
double à decimals à 8 bytes
Can store #s from: -1.79769 € 10308 to 1.79769 € 10308
(approx)
Literals:
Constants
Shortcut:
When declaring variables, you
can do:
int x, y, z; -OR- double p, q = 1.41, pi = 3.14,
t;
Unary Operators: Placed before
operands
Example: -a;
Binary Operators: Placed between operands
Example: a * b;
%: Remainder (modulus)
Precendence Chart:
1.
Grouping: ()
2. Method
Selector: .
3. Unary Plus, Unary Minus, Instantiation, Cast
4. Multiplication, Division, Remainder
5. Addition, Subtraction
6. Assignment (=)
When programming, PEMDAS applies!
Key Concept: All
doubles can contain ints, but ints cannot contain doubles. (Think Venn diagram)
We use casting to deal with mixed-mode arithmetic.
Example: double x, y;
x = (double)5 / 4; ß x becomes 5.0 / 4 = 1.25
y = (double)(5 / 4); ß (5 / 4) becomes the int 1.
Concatenation Operator: +
Concatenation allows the programming
to insert values from within a string.
Example:
String message;
int x = 20, y = 35;
message = ³Jake sold ³ + x +
³ and Joe sold ³ + y + ³ hot dogs.²;
Warning:
Concatenation has the same precedence as addition, thus:
³John ³ + 3 + 4 will display
as: John 34
while
3 + 4 + ³John² will display
as: John 7
and
³John ³ + (3 + 4) will also
display as: John 7
Write down as many as Java¹s reserved words as you can remember in 3 minutes!
Part
of java¹s power is it¹s ability to use reuse code from other people. This is exactly what we¹ve been doing
with TerminalIO and TurtleGraphics.
We access them using the import statement.
Example: import
a.b.c;
a = overall name of the
package
b = name of the subsection
within the package
c = name of a class within
the subsection
Shortcut example: import
java.io.*;
Using ³*² means we want all
the classes in a package.
3.3: TerminalIO for differing data types
Methods in KeyboardReader:
char readChar(); double readDouble(); int readInt();
String readLine(); void pause();
(pg 69)
3.4: Comments
New trick: Use /* and */ for multi-line comments!
Example: /* Mr. Friedland is a superhero and all around great guy. */ ß Note: This should
now appear in all code.