308-203A Introduction to Computing II Lecture 1: Java Review
description
Transcript of 308-203A Introduction to Computing II Lecture 1: Java Review
![Page 1: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/1.jpg)
308-203AIntroduction to Computing II
Lecture 1: Java Review
Fall Session 2000
![Page 2: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/2.jpg)
Primitive data typesTypebytecharshortintlong
floatdouble
boolean
Values[-128, 127][0, 65,536][-32,768 , 32,767][-2,147,483,648 , 2,147,483,647][-9,223,372,036,854,775,807 , 9,223,372,036,854,775,806][1.4023984e-45 , 3.40282347e+38][4.94065645841246544e-324, 1.79769313486231570e+308]{ false, true}
Size8 bit16 bit16 bit32 bit64 bit
32 bit64 bit
“1 bit”
![Page 3: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/3.jpg)
Variable declarations
<type> <var-name> [ = <initializer>] ;
Examples:
char x = ‘a’;int i, j = 5, k;float pi = 3.14159;
![Page 4: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/4.jpg)
Variable declarations
<type> <var-name> [ = <initializer>] ;
Examples:
char x = ‘a’;int i, j = 5, k;float pi = 3.14159;
‘a’x
![Page 5: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/5.jpg)
Variable declarations
<type> <var-name> [ = <initializer>] ;
Examples:
char x = ‘a’;int i, j = 5, k;float pi = 3.14159;
‘a’x
??i
5j
??k
![Page 6: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/6.jpg)
Variable declarations
<type> <var-name> [ = <initializer>] ;
Examples:
char x = ‘a’;int i, j = 5, k;float pi = 3.14159;
‘a’x
??i
5j
??k
3.14159pi
![Page 7: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/7.jpg)
Assignments and expressions<var-name> = <expr> ;
Examples:
double pi, radius, circ;...circ = 2 * pi * radius ;
int x;boolean xGreaterThanFive;...xGreaterThanFive = (x > 5);
![Page 8: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/8.jpg)
Assignments and expressions<var-name> = <expr> ;
Examples:
double pi, radius, circ;...circ = 2 * pi * radius ;
int x;boolean xGreaterThanFive;...xGreaterThanFive = (x > 5);
3.14159
pi1.0
radius
circ
????
![Page 9: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/9.jpg)
Assignments and expressions<var-name> = <expr> ;
Examples:
double pi, radius, circ;...circ = 2 * pi * radius ;
int x;boolean xGreaterThanFive;...xGreaterThanFive = (x > 5);
3.14159
pi1.0
radius
circ
6.28318
=
![Page 10: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/10.jpg)
Assignments and expressions<var-name> = <expr> ;
Examples:
double pi, radius, circ;...circ = 2 * pi * radius ;
int x;boolean xGreaterThanFive;...xGreaterThanFive = (x > 5);
10x
xGreaterThanFive
????
![Page 11: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/11.jpg)
Assignments and expressions<var-name> = <expr> ;
Examples:
double pi, radius, circ;...circ = 2 * pi * radius ;
int x;boolean xGreaterThanFive;...xGreaterThanFive = (x > 5);
10x
xGreaterThanFive
true
=
![Page 12: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/12.jpg)
Type-casting
(<type>) <expr>
Converts one type to another.
Examples:
float f = 1.5;int j = (int) f; // j == 1
short s = 200;double d = (double) s; // d == 2.0e2
![Page 13: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/13.jpg)
Strings and Arrays
Examples:
String str = “Hi there”;
char myArr[3];
myArr[0] = ‘c’;myArr[1] = ‘a’;myArr[2] = ‘t’;
![Page 14: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/14.jpg)
Strings and Arrays
Examples:
String str = “Hi there”;
char myArr[3];
myArr[0] = ‘c’;myArr[1] = ‘a’;myArr[2] = ‘t’;
str
“Hi there”
![Page 15: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/15.jpg)
Strings and Arrays
Examples:
String str = “Hi there”;
char myArr[3];
myArr[0] = ‘c’;myArr[1] = ‘a’;myArr[2] = ‘t’;
str
“Hi there”
myArr
??????
[0]
[1]
[2]
![Page 16: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/16.jpg)
Strings and Arrays
Examples:
String str = “Hi there”;
char myArr[3];
myArr[0] = ‘c’;myArr[1] = ‘a’;myArr[2] = ‘t’;
str
“Hi there”
myArr
‘c’‘a’‘t’
[0]
[1]
[2]
![Page 17: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/17.jpg)
A simple program
public class hello{ public static void main(String args[ ]) { System.out.println(“Hello world”); }}
![Page 18: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/18.jpg)
Making decisions(If only the only “if ” in “life”were between the “l” and the “e”)
• if (<boolean>) then <statement>;
Example
int x = 10;
if (x > 5){
System.out.println(“x is greater than 5”);}
![Page 19: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/19.jpg)
Making decisions(If only the only “if ” in “life”were between the “l” and the “e”)
• if (<boolean>) then <statement>;
Example
int x = 10;boolean xGreaterThanFive = (x > 5);if (xGreaterThanFive){
System.out.println(“x is greater than 5”);}
![Page 20: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/20.jpg)
Making decisions
• <boolean> ? <expr1> : <expr2>
Shorthand for if
x = (condition ? a : b);
if (condition) x = a ;else x = b ;
![Page 21: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/21.jpg)
Making decisions
• <boolean> ? <expr1> : <expr2>
Example
int x = 10;
System.out.println((x>5) ? “x is greater than 5”
: “x is less than 6”);
![Page 22: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/22.jpg)
Making decisions
• switch (<expr>) { case <constant>: <body_1>; case <constant>: <body_2>; … default: <body_d> }
![Page 23: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/23.jpg)
Making decisionsExample:
char myChar = getAChar();
switch (myChar) { case ‘a’: option_a(); break; case ‘b’: option_b(); break; case ‘c’: option_c(); break;
default: System.out.println(“Unknown option”); }
![Page 24: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/24.jpg)
Loops• while (<condition>) <body>;
Example:
int j = 0; while (j < 3) { System.out.println(j); j++;}
Output:012
![Page 25: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/25.jpg)
Loops• do <body> while (<condition>);
Example:
int j = 0; do{ System.out.println(j); j++;}while (j < 3) ;
Output:012
![Page 26: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/26.jpg)
Loops
• for (<initializer>; <condition> ; <incr>) <body>;
Example:
for (int j = 0; j < 3; j++) System.out.println(j);
Output:012
![Page 27: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/27.jpg)
Another simple program
public class reverse{ public static void main(String args[ ]) { for (int i = args.length-1; i >= 0; i--)
System.out.println(args[i] + “ ”);
System.out.println(“\n”); }}
![Page 28: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/28.jpg)
Reference variables
• The variable “points” to some data somewhere• <type> is the kind of object being referenced• Strings and arrays are treated as references
(as are user-defined types)• Special value: null
<type> <var-name>;
![Page 29: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/29.jpg)
Reference variables
Example:
Thing x = theBlob;Thing y = null;
y = x;
boolean same = (x == y);
nullx y
theBlob
![Page 30: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/30.jpg)
Reference variables
Example:
Thing x = theBlob;Thing y = null;
y = x;
boolean same = (x == y);
x y
theBlob
???same
![Page 31: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/31.jpg)
Reference variables
Example:
Thing x = theBlob;Thing y = null;
y = x;
boolean same = (x == y);
x y
theBlob
truesame
![Page 32: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/32.jpg)
Strings are references, too
Example:
String a = “YO!”;String b = “YO!”;
boolean same = (a == b);
![Page 33: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/33.jpg)
Strings are references, too
Example:
String a = “YO!”;String b = “YO!”;
boolean same = (a == b);
a “YO!”
![Page 34: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/34.jpg)
Strings are references, too
Example:
String a = “YO!”;String b = “YO!”;
boolean same = (a == b);
a “YO!”
b “YO!”
![Page 35: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/35.jpg)
Strings are references, too
Example:
String a = “YO!”;String b = “YO!”;
boolean same = (a == b);
a “YO!”
b “YO!”
false!!same
![Page 36: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/36.jpg)
Strings are references, too
Example:
String a = “YO!”;String b = “YO!”;
boolean same = a.equals(b);
a “YO!”
b “YO!”
truesame
![Page 37: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/37.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
![Page 38: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/38.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo ????
[0][1]
![Page 39: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/39.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo ????
[0][1]
bar
![Page 40: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/40.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo 523??
[0][1]
bar
![Page 41: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/41.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo 325??
[0][1]
bar
![Page 42: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/42.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = foo;
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo 325??
[0][1]
bar
Output: 325!!!
![Page 43: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/43.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = (int[ ]) foo.clone();
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo 523??
[0][1]
bar523??
[0][1]
![Page 44: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/44.jpg)
Arrays are references, too
Example:
int[ ] foo = new int[2];int[ ] bar = (int[ ]) foo.clone();
foo[0] = 523;bar[0] = 325;System.out.println(foo[0]);
foo 523??
[0][1]
bar
Output: 523
325??
[0][1]
![Page 45: 308-203A Introduction to Computing II Lecture 1: Java Review](https://reader035.fdocuments.us/reader035/viewer/2022062517/568139bb550346895da15b95/html5/thumbnails/45.jpg)
Any Questions?