PPT Version - University of Pittsburgh

Download Report

Transcript PPT Version - University of Pittsburgh

Lec.2:
Java Basics
Jiang (Jen) ZHENG
May 9th, 2005
Outline
 Quick Review
 Lexical Elements
 Operators
 Data Types
 Predefined Methods
 Programming Style
 Program Examples Analysis
 Topics for next lecture
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
2
Quick Review
 Java has Application and Applet
 Java is Object-Oriented
 Java is platform independent
 Java is Garbage Collected
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
3
Lexical Elements
White Space &
Comments
Tokenizing
Source Code
keywords
Further Analysis
identifiers
literals
operators
Early Stage of Compiling
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
4
White Space & Comments
 White Space:


space between tokens and newline character
after each line
Separate adjacent tokens and make program
more readable
 Comments
 A single-line comment begins with //
 A multi-line comments between /* and */
 Comments between /** and */ can be recognized
by javadoc
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
5
Keywords
 47 keywords in Java
 Reserved, has special meaning to the Java
compiler, can’t be used for any other purposes.
 abstract boolean break byte case catch char
class const continue default do double else
extends final finally float for goto if implements
import instanceof int interface long native new
package private protected public return short static
super switch synchronized this throw throws
transient try void volatile while
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
6
Identifiers
 Define class, method or variable names.
 Can be any sequence of Java letters and
digits, the first must be Java letter
 Java Letters: English language uppercase
and lowercase letters, and $, _
 Java Digits: 0 - 9
 Can’t be keywords, true, false and null.
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
7
Identifiers Naming Conventions
 Classes: start with upper case, then start each words
with an upper case latter

For example: HelloWorld, StringBuffer,
BufferReader …
 Methods: start with lower case, then start each word
with an upper case letter

For example: compareTo, drawString, moreTokens()
 Although legal, we should not use $
 Most of Java programmers are following this
convention.
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
8
Some Legal Identifiers
 data
// variable name
 HelloWorld //class name
 getTime
//method name
 itcanbeverylong
i
 _123
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
9
Some Illegal Identifiers
3
 x+y
 some**name
 no Space
 1floor
 class
 public
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
10
Literals
 Representations of values for the primitive types.
 8 primitive types in Java
Java Type
byte
int
short
long
float
double
boolean
char
Examples
…
123, 0, -999,
…
…
3.1415, -0.05, 0.0
…
true, false
‘s’, ‘t’, ‘w’
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
11
Operators & Punctuations
 Operators
 +, -, *, /, %
Ex: 3+5=8, 8%5=3
 =, +=, -=, *=, /=, %= (assignment operator)
Ex: i+=j+k is equivalent to i=i+(j+k)
 ++, -- (increment & decrement operators)
Ex: i=i+1/i=i-1 is equivalent to i++/i- i++ (assignment first) and ++i (store value incremented
first)
Ex: int a, b, c=0;
a = ++c;
b = c++;
System.out.println(“a=” + a + “; b=” + b + “; c=”+ c );
 Punctuations:
 ; {} ()
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
12
Lexical Elements Example
/* Your first Java Application */
class HelloWorldApp {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
White Space &
Comments
Tokenizing
/* Your first Java Application */
keywords
class public static void
identifiers
HelloWorldApp args println String main System out
literals
operators
“Hello World!”
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
13
Precedence and Associativity of
Operators
 Precedence: The order that operations are
evaluated
 Associativity: The order that operators of
equal precedence are to be evaluated.
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
14
Precedence and Associativity of
Operators
Operator
()
++(postfix) --(postfix)
+(unary) -(unary) ++(prefix) --(prefix)
new (type)expr
* / %
+ = += -= *= /= %=
etc.
Associativity
Left to right
Right to left
Right to left
Left to right
Left to right
Right to left
 Examples: Given a=1, b=2, c=3, d=4, what are
the values of following expressions.
Expression
a*b/c
a*b%c+1
++a * b – c-7 - - b * ++d
Equivalent Expression
(a*b)/c
((a*b)%c)+1
( ( ++a ) * b ) – (c --)
7 – ( (-b) * (++d) )
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
Value
0
3
1
17
15
Data Types
 Java is strongly typed languages. Each data item has
a declared type.
 Primitive types:



int, double, char …
Created by using literals: a= 5;
Can apply on built-in operators: 2+3
 Class types -- Objects:
 Standard Java classes: String, Button, Graphics …
1500 classes
 Programmer can define new classes: HelloWorld
 Created by using new keyword: new Button(“Start”);
String is special, String a = “hello”;
 Can have operations/methods. Ex: a.length();
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
16
Variables & Initialization
 Example:



boolean flag = true;
int j=0, k=100;
Button exitButton = new Button(“exit”);
 flag, j, k, exitButton are variables.
 Variables are identifiers used to refer to data
values that are stored in computer’s memory.
 Can be initialized during declaration.
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
17
More On Data Types:
Integer Types & Char Type
 byte, short, int, long are all integer types to
represent integers. Difference is number of
bits and the value range.
 char is to represent characters and is also
integer type. For example, ‘a’=97, ‘A’ =65
 P35 table provides all of the non-printing and
hard to print character. \\ , \t, \n, \’’, \’
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
18
Floating Types
 a sign + a magnitude + an exponent
Ex: 2.17e-27 ( 2.17×10-27)
 float (32 bits) and double (64 bits)
 Number of bits, value range and approximate
precision are different.
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
19
Arithmetic Expressions
 int, long, float, double are the four types
recognized by Java for arithmetic operations.
 Rules for converting operands:
If one is double, all converted to double.
 Otherwise If one is float, all converted to float.
 Otherwise If one is long, all converted to long.
 Otherwise, all are converted to int.
 For example:
6 / 4 = 1; 6 / (-4) = -1 ; 6.0 / 4=1.5;

CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
20
Type Conversions
 Widening primitive conversions automatically.


Automatically during the mixed mode
arithmetic
Convert operators of the left-hand-side to
right-hand-side type.
 Narrowing primitive conversions by cast.
 Cast: (type) expression
int someInt;
float someFloat=3.14159;
someInt = (int) someFloat;
System.out.print (someInt );
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
Output is:
3
21
Predefined Methods
 A method is a group of instructions having a name.
 For example: main, drawLine, length, concat, print,
println …
 Predefined methods: Java provided, can be used
directly by calling
objectName.methodName(parameters)
 For example:
System.out.print(x); System.out.println(x);
Math.min(x, y);
Math.sqrt(x);
string1.concat(string2);
string1.length();
 Many more predefined methods.

(Appendix B, Chap. 8-10, 13)
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
22
Programming Style
 Clear, consistent
 Good documentation


Good for you to read later
Good for other programmers to read
 Follow the conventions by the large party of
the programming community.



Class name convention
Method name convention
Variable name convention
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
23
Program Example 1
import java.io.*;
class HelloWorld2 {
public static void main( String[] args) {
String word1;
String word2, sentence;
word1 = “Hello, ”;
word2 = “world !”;
sentence = word1.concat(word2);
System.out.println ( sentence );
}
}
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
24
Program Example 2
/** some comments here.
*/
import java.awt.*;
import java.swing.*;
Public class OneApplet extends JApplet {
public void paint( Graphics g) {
/* some comments here */
g.drawLine(0, 0, 250, 100);
g.drawOval(250, 100, 200, 100);
}
}
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
25
Topics for next lecture
 Reading Assignment: Chapter 3
You don’t need to know the details but you should
know what are those when I talk about it.
 How to do User Input / Standard Input
CS401/COE401 Summer 2005.Department of
Computer Science.University of
Pittsburgh.Lecture 2
26