Lesson_02

advertisement
Lesson 02 – Identifiers, Data Types, Operators, and Sequence Statements
1. Rules for creating a valid identifier in Java
An identifier is anything you create a name for: variable, named constant, method name, class name
a. valid characters: $, _ , letters, digits
b. must begin with a letter or special character ( $ or _ )
c. cannot contain any spaces
d. identifiers are case sensitive
e. most Java tetbooks use camel casing by convention
2. Java data types [ Java has value and reference types ]
a. value
//stores a value of one of the 8 primitive data types (PDT's) in Java
 PDT's are: (integral) byte, short, int, long (floating-point) float, double boolean

int x;
double r;
boolean found;
char
char grade;
b. reference
//stores a reference to an object (a reference is an address)
 created using the name of a class
Scanner input = new Scanner(new File(System.in)); PrintWriter output = new PrintWriter(“myfile.txt”);
3. Declaring and initializing variables [ very similar to CIS 103 Quick Reference guide ]
a. A variable is a named location in memory that temporarily stores a value.
<type> name [ = <initial-value> ] [, name [ = <initial-value> ] …
b. You create a named constant in Java using the keyword final. Example: final double PI = 3.1415;
4. Java operators [ see Java Quick Reference Guide ]
a. arithmetic + - * / %
b. relational < <= > >=
c. equality
== !=
d. logical
! (not) && (and) || (or)
e. precedence and associativity
[ we already know this. See the Java Quick Reference Guide ]
5. Sequence control structure statements
a. declaration
int x;
final String Company = “Ajax Airlines”;
b. input/output statement
x = input.nextInt();
System.out.println("x is " + x);
c. method invocation
houseKeeping(); processRecord();
6. interactive input [ getting input using an input dialog box ]
 JOptionPane.showInputDialog( )
String temperatureString = JOptionPane.showInputDialog("Enter a temperature");
double temperature = Double.parseDouble(temperatureString);
7. Input dialog box properties
a.
b.
c.
displays a prompt
accepts as input whatever the user types in
returns a String
import javax.swing.JOptionPane;
//required before beginning of class declaration
//for both input and output
8. String concatenation
 Java allows us to build a String by concatenating Strings, numbers, etc. [ like RAPTOR ]
System.out.println("Your score is " + score + ", your grade is " + grade);
Download