Professional Documents
Culture Documents
Note that All text in this blue color should Not be gone over in class.
The students can read it if they like in their copy of this document.
Eclipse Intellisense is used to have Eclipse complete code for you as you
type. It gives you all of the methods that you can use in a class.
You will need this to do the standardized exam at the end of the course.
public
class
static
void
main
String[]
System.out.println (vs System.out.print)
delimiters: ;, }, { (blocks)
capitalization conventions
Comments In Java
boolean has just 2 values: true and false. (Unlike C, not the
same as 1 and 0.)
The char Type
Unicode 1.0 fixed 2 byte (16 bit) width codes. Javas char type is 16
bit.
Now Unicode code values require 20 bits. All the most commonly
used characters (which belong to the basic multilingual plane or
BMP) are represented in the first 16 bits. In Java, the unicode value of
a BMP character is expressed using the character \u followed by 4
hex values.
System.out.println("After waving, he
said \"hello\"");
//output: After waving, he said "hello"
Variables In Java
Variables in Java store values, like strings, numbers, and other data
Variable names consist of digits, letters and underscores, but may not
begin with a digit.
OR
int sum = 0;
Coding Style:
o variable names begin with lower case letter
o variable names composed of multiple words written so that each
new word (except the first) begins with a capital letter, but all
other letters are lower case. For example newInterestRate.
o underscores should not be used typically in variable names
o for constants, capitals and underscores are used (discussed later)
Operators In Java: Arithmetic Operators
5/2 = 2
-5/2 = - ( 5/2 ) = -2
-5%2 = - ( 5%2 ) = -1
Example:
int k = 1;
k++; //new value of k is 2 (postfix
form)
++k; //new value of k is 3 (prefix form)
Example:
int k = 0;
int m = 3 * k++; //m equals 0, k equals 1
int k = 0;
int n = 3 * ++k; //n equals 3, k equals 1
&& T F || T F
T T F T T T
F F F F T F
Example:
minVal = (a < b) ? a : b;
IF(a < b )
a
ELSE
b
Example:
customerStatus =
(income > 100000) ?
PLATINUM : SILVER;
Examples:
char
float double
Example:
double x = 9.997;
int nx = (int) x; //nx has value 9
Example:
double x = 9.997;
int nx = (int)Math.round(x);
//round
//returns a long,
//so cast is
//necessary.
// nx becomes 10.
Operator Precedence And Association Conventions
Examples:
See page 50 of Core Java 7th ed. for a chart detailing the rules
See page 53 of Core Java 8th ed. for a chart detailing the rules
We have examined primitive data types (int, char, boolean, etc) and how
variables are declared using these types. Most of the data types used in
Java, however, are not built into the language, but are created through
class definitions. This will be the topic of Lesson 4. We give an example
now as an illustration, and that can be used in labs for this lesson.
Console input provides the simplest means for a user to interact with a
Java program
* Samples
Scanner (j2se5.0)
//can think of Scanner as a special
data //type
//output
Type your name: Jim Stevens
you wrote: Jim Stevens
Type your age: 36
your age: 36
BufferedReader in = null;
String input = null;
in = new BufferedReader(new
InputStreamReader(System.in));
Example:
String name = "Jennifer";
Page 520 A, C.
Page 63 E.
int countCharacters=0;
String sentence = hello;
char c;
if (Character.isLetter(c)) {
countCharacters++;
}
}
String Functions: substring, indexOf, startsWith,
+, equals
o indexOf
boolean result2 =
name.startsWith("R"); //true
equals
// Why??