Java Lecture 2 - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

Java Lecture 2

Description:

Ever have one of those days... 13X11. Books. Thinking in Java -- Bruce Eckel ... char -- typically used for printable characters. Whole Numbers ... – PowerPoint PPT presentation

Number of Views:159
Avg rating:3.0/5.0
Slides: 60
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Java Lecture 2


1
Java Lecture 2
  • CS 1311
  • Beginning Java
  • (Sort of Boring
  • but Oh So Necessary Syntax Stuff)

13X11
2
Ever have one of those days...
3
Books
  • Thinking in Java -- Bruce Eckel
  • Free on the web search for Eckel
  • Also available in paper
  • Java in a Nutshell -- David Flanagan (O'Reilly)
  • Mostly reference
  • Actually 3 books
  • Kurt
  • Just Java 2 (4th Edition) Peter van der Linden

4
Online
  • http//www.javasoft.com
  • Tutorial is excellent

5
Beginning Java
  • Identifiers
  • Comments
  • Keywords
  • Data types
  • Expressions and operators
  • Operator precedence
  • The assignment statement
  • Organizing code
  • Selection statements
  • Iteration statements

6
Structure
  • Java "programs" consist of files containing
    classes.
  • The classes will consist of variable declarations
    and modules which will be called methods (and
    constructors)
  • All of this is written in a language quite
    different in appearance from Scheme.
  • If Scheme is your first programming experience
    then things may seem backwards at first
  • If you have prior experience with other languages
    it may seem familiar but watch out!

7
Identifiers
  • Used to name things
  • Variables
  • Methods
  • Classes
  • CaSe SeNsItIvE
  • Rules
  • Underscore or letter
  • 0 or more underscores, letters, numbers
  • Any length!

8
Identifiers
  • Style
  • Class names capitalized
  • Variables and method names start with lower case
    letter. If multi-word capitalize all but first.
  • Constants in all caps
  • Examples
  • class Widget
  • class LinkedList
  • Variables x, n, pressure, isEmpty,
  • hasMore, steamTemp
  • Constants FEETPERMILE, HOURSPERWEEK

9
Comments
  • // until end of line
  • / This is a multiline comment
  • it continues for
  • as many lines as I want
  • these stars mean nothing!
  • and now for the end --gtgtgt /

10
Careful
  • /
  • Comments about this section
  • yada - yada - yada
  • /
  • x 3 // Here is some
  • y 4 // info about
  • z 5 // these lines

11
Careful
  • /
  • Comments about this section
  • yada - yada - yada
  • // (note) /
  • x 3 // Here is some
  • y 4 // info about
  • z 5 // these lines

12
Careful
  • /
  • Comments about this section
  • yada - yada - yada
  • x 3 // Here is some
  • y 4 // info about
  • z 5 // these lines

13
Keywords
  • abstract default if private throw
  • boolean do implements protected
    throws
  • break double import public transient
  • byte else instanceof return try
  • case extends int short void
  • catch final interface static
    volatile
  • char finally long super
    while
  • class float native switch
  • const for new synchronized
  • continue goto package this

14
Data types
  • What is data typing?
  • Why is it used?
  • What's good about it?
  • What's bad about it?
  • Java is considered a strongly typed language.

15
Java Primitive Data Types
  • boolean -- true/false
  • byte
  • short
  • int
  • long
  • float
  • double
  • char -- typically used for printable characters

Whole Numbers
Fractional Numbers (with decimal digits)
16
Java Primitive Data Types
  • boolean -- true/false
  • byte
  • short
  • int
  • long
  • float
  • double
  • char -- typically used for printable characters

Whole Numbers
Fractional Numbers (with decimal digits)
17
Primitive Data Type Ranges
Type
Size
Min
Default
Max
boolean
false
1
false
true
char
'\u0000' (null)
16
byte
(byte) 0
8
-128
127
short
(short) 0
16
-32,768
32,767
int
0
32
-2,147,483,648
2,147,483,647
long
0L
64
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
float
0.0F
32
Approx 3.4E38 with 7 significant digits
double
0.0D
64
Approx 1.7E308 with 15 significant digits
void
Not truly min and max.
18
Declarations
  • int i
  • int a, b, c
  • double d 42.0
  • char ch1
  • char ch2 's'
  • boolean isFull true
  • boolean isFull
  • isFull true

Equivalent?
19
Constants
  • public static final int FEETPERMILE 5280
  • public static final String MYGTNUM"gt1234x"
  • public static final boolean DEBUG true
  • public static final int JAN 1
  • public static final int FEB 2
  • public static final int MAR 3
  • etc.

if(DEBUG) / Do Something /
20
Mysterious Strings
  • Strings are objects
  • They are different from every other language!!!
  • Don't use strings to figure out how Java works
  • They have all kinds of special rules
  • String someStr "This is a string"
  • someStr "Strings can be " "concatenated."
  • someStr "If they won't fit on one line "
  • "you need to do it like this"
  • someStr "They can be printed"
  • System.out.println(someStr)

21
References
  • Variables which hold a "reference" or "pointer"
    to an object.
  • Can be thought of as the address of an object in
    memory
  • Normally don't manipulate (like in C)
  • So no need for address of/dereferencing operators
  • ltType i.e. name of some classgt ltidentifiergt
  • String name
  • Queue myQueue
  • CokeMachine cc
  • More later...

Can also initialize in same statement
22
Expressions
  • Unlike Scheme, expressions in Java don't have to
    return a value.
  • Java uses infix notation so
  • (and (lt 25 ( ( x x) ( y y))) (gt x 0))
  • becomes
  • (x x y y lt 25) (x gt 0)
  • Note means "AND" not the same as

23
Operators
  • Arithmetic - /
  • Relational gt lt lt gt !
  • Logical !
  • Assignment
  • - /
  • ltlt gtgt gtgtgt
  • Conditional ?
  • Bit manipulation ltlt gtgt gtgtgt Funky
    (Inc/Dec) --

Scheme Predicates
24
Operators
  • Arithmetic - /
  • Relational gt lt lt gt !
  • Logical !
  • Assignment
  • - /
  • ltlt gtgt gtgtgt
  • Conditional ?
  • Bit manipulation ltlt gtgt gtgtgt Funky
    (Inc/Dec) --

25
Operator Precedence
  • 1 2 3
  • When in doubt use parentheses
  • (Don't tell Gosling)

26
The following table shows the precedence assigned
to the operators. The operators in this table are
listed in precedence order the higher in the
table an operator appears, the higher its
precedence. Operators with higher precedence are
evaluated before operators with a relatively
lower precedence. Operators on the same line have
equal precedence. postfix operators .
(params) expr expr-- unary operators
expr --expr expr -expr ! creation or cast
new (type)expr multiplicative /
additive - shift
ltlt gtgt gtgtgt relational lt gt lt gt
instanceof equality ! bitwise
AND bitwise exclusive OR bitwise
inclusive OR logical AND
logical OR conditional
? assignment - /
ltlt gtgt gtgtgt
When operators of equal precedence appear in the
same expression, a rule must govern which is
evaluated first. All binary operators except for
the assignment operators are evaluated in
left-to-right order. Assignment operators are
evaluated right to left.
27
The assignment statementx 1 1
  • Evaluate expression on right side
  • Place result in variable on left side
  • Left side must be capable of storing a value
  • 3 x // Bad idea!
  • End assignment statements with a
  • Legal?
  • a b c 0
  • Good idea?
  • not

28
The Classic Duality
  • Programming languages have always wrestled with
    the difference between assigning a value and the
    equality relational operator
  • Equality (Boolean Result)
  • BASIC A B
  • Pascal A B
  • FORTRAN A .EQ. B
  • C A B
  • Pseudocode A B
  • Scheme ( A B)
  • Java A B
  • Assignment
  • BASIC LET A B
  • Pascal A B
  • FORTRAN A B
  • C A B
  • Pseudocode A ? B
  • Scheme (set! A B)
  • Java A B

29
Common Errors
  • Misuse of or --
  • Confusing and
  • Using
  • instead of
  • instead of

30
Warning!
  • Just because a language will let you do something
    doesn't mean it's a good idea
  • _ _-- - _
  • / Clear? /

31
Organizing code
  • Block statements
  • Surround statements with curly braces
  • int i
  • x 1
  • y 2
  • i x y
  • Can be used in place of a single statement
  • Note variables declared in a block have scope
    only in the block!

32
Organizing code
  • Null statement
  • Just a lone semicolon
  • Not necessarily your friend!

33
Selection statements
  • Used to control flow of program
  • If
  • If with Else
  • Complex if constructions
  • Switch

34
if
  • if(ltboolean_valuegt)
  • ltExecuted if boolean is truegt
  • ltExecuted in any casegt
  • if(x gt 0)
  • System.out.println("X greater than zero!")

35
if
  • if(y gt 0)
  • System.out.println("Always gets printed!")
  • if(z gt 0 isEmpty)
  • x y
  • System.out.println("Hello")

36
if/else
  • if(ltboolean_valuegt)
  • ltExecuted if boolean is truegt
  • else
  • ltExecuted if boolean is truegt
  • ltExecuted in all casesgt
  • if(a b)
  • System.out.println("Always use curlies")
  • else
  • System.out.println("What do you think?")

37
if/else -- Another style
  • if(a b)
  • System.out.println("Always use curlies")
  • else
  • System.out.println("What do you think?")

38
Complex
  • if(a b)
  • System.out.println("A equals B")
  • else
  • if(c d)
  • System.out.println("C equals D")
  • else
  • if(e f)
  • System.out.println("E equals F")
  • else
  • System.out.println("None")

39
Complex
  • if(a b)
  • System.out.println("A equals B")
  • else if(c d)
  • System.out.println("C equals D")
  • else if(e f)
  • System.out.println("E equals F")
  • else
  • System.out.println("None")

40
Complex
  • if(a b)
  • System.out.println("A equals B")
  • else if(c d)
  • System.out.println("C equals D")
  • else if(e f)
  • System.out.println("E equals F")
  • else
  • System.out.println("None")

41
Complex
  • if(a b)
  • System.out.println("A equals B")
  • else if(c d)
  • System.out.println("C equals D")
  • else if(e f)
  • System.out.println("E equals F")
  • else
  • System.out.println("None")

42
Complex
  • if(a b)
  • System.out.println("A equals B")
  • else if(c d)
  • System.out.println("C equals D")
  • else if(e f)
  • System.out.println("E equals F")
  • else
  • System.out.println("None")

43
Nesting
  • if(a b)
  • if(c d)
  • / do something here /
  • else
  • / do something else /
  • else
  • if(x y)
  • / what to do? /
  • else
  • / another option /

44
Nesting
  • if(a b)
  • if(c d)
  • / do something here /
  • else
  • / do something else /
  • else
  • if(x y)
  • / what to do? /
  • else
  • / another option /

45
Switch
  • Used when there is a single value to test
  • basically an int
  • and multiple "cases"
  • Common error forgetting break

46
Switch
  • switch (month)
  • case APR
  • case JUN
  • case SEP
  • case NOV
  • numDays 30
  • break
  • case FEB
  • if(year 4 0)
  • numdays 29
  • else
  • numdays 28
  • break
  • default
  • numdays 31

Any potential problems?
47
Just because you could do it,
  • numDays 31
  • switch (month)
  • case FEB
  • if(year 4 ! 0)
  • numDays-- // 28
  • numDays-- // 29
  • case APR
  • case JUN
  • case SEP
  • case NOV
  • numDays-- // 30
  • default
  • break

48
Iteration statements
  • while loop
  • Tests at beginning of loop
  • Executed 0 or more times
  • do while loop
  • Tests at end of loop
  • Executed at least once
  • for loop
  • Equivalent to while loop
  • Very popular!

49
While Loop
  • while(ltboolean_Valuegt)
  • ltstatementgt
  • Example
  • i 0
  • while(i lt 10)
  • i

50
While Loop
  • int i 1
  • int total 0
  • while(i lt 10)
  • total i
  • i

Note No
51
Do While Loop
  • do
  • ltstatementgt
  • while(ltbooleangt)

52
Do While Loop
  • int choice
  • do
  • choice menu()
  • switch(choice)
  • / details omitted /
  • while(choice ! 0)

Note
53
For Loop
  • for(ltinitgt lttestExprgt ltincrementgt)
  • ltstatementgt
  • ltinitgt and ltincrementgt can be multiple statements
    separated by commas
  • ltinitgt, lttestExprgt and ltincrementgt are all
    optional!
  • for() // Legal statment

54
For Loop
  • for(i0 i lt MAX i)
  • System.out.println(i)
  • for(i0 i lt MAX i)
  • System.out.println(i)
  • System.out.println(i)
  • for(int i0 i lt MAX i)
  • System.out.println(i)
  • System.out.println(i)

55
For Loop
  • for(i0, total0 i lt MAX i)
  • System.out.println(i)
  • for(i0 i lt MAX something i)
  • System.out.println(i)
  • for(int i0, j10 i lt MAX i,j--)
  • System.out.println("Sum " i j)

56
while -- for equivalence
  • total 0
  • i 0
  • while(i lt MAX)
  • / Work /
  • total i
  • i
  • total 0
  • for(i 0 i lt MAX i)
  • / Work /
  • total i

57
Questions?
58
Code used in class
  • public class Calendar
  • public static final int JAN 1
  • public static final int FEB 2
  • public static final int MAR 3
  • public static final int APR 4
  • public static final int MAY 5
  • public static final int JUN 6
  • public static final int JUL 7
  • public static final int AUG 8
  • public static final int SEP 9
  • public static final int OCT 10
  • public static final int NOV 11
  • public static final int DEC 12
  • public static void main(String argv)
  • String monthName "January", "Febru
    ary",
  • "March",

59
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com