Sunteți pe pagina 1din 25

1

School of something
FACULTY OF OTHER
School of Computing
FACULTY OF ENGINEERING
Introuction to !"#"
$"r% &"l%le'
($)A)&"l%le'*lees)"c)u%+
Nic% Effor
(N),)Effor*lees)"c)u%+
!-. Fun"ment"ls of !"#"
/
To"'0s O12ecti#es
Lecture

To recognise ho3 !"#" iffers from 4'thon

To le"rn the fun"ment"ls of !"#" s'nt"5


Lab

To 3rite some simple !"#" progr"ms "n g"in some


e5perience of using "n I,E
6
!"#" #s) 4'thon. Sc"ffoling
// Traditional first program, in Java
class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
re7uire
re7uire
Hello.java
(see http.88ocs)or"cle)com82"#"se8tutori"l8getSt"rte8"pplic"tion8+
9
!"#" #s) 4'thon. Use of Cl"sses

!"#" coe has to be organised as classes


: strongl' encour"ging "n o12ect;oriente "ppro"ch

Cl"sses "re " mech"nism for ph'sic"l coe org"nis"tion<


too= e"ch compiles to " sep"r"te file of bytecode
00000000 ca fe ba be 00 00 00 32 00 1d 0a 00 06 00 0f 09
00000010 00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07
00000020 00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
00000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
00000040 75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69
00000050 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67
...
Hello.class (9-> 1'tes+
?
!"#" #s) 4'thon. @er1osit'
Comp"re
System.out.println("Hello, World!");
3ith
print("Hello, World!")
A
!"#" #s) 4'thon. @er1osit'
Comp"re
BufferedReader inputFile = new BufferedReader(
new FileReader("foo.txt"));
String line = inputFile.readLine();
while (line != null) {
System.out.println(line);
line = inputFile.readLine();
}
3ith
input_file = open('foo.txt')
for line in input_file:
print(line[:-1])
!"#" I,Es c"n help 1'
gener"ting 1oilerpl"te
"utom"tic"ll'< oing
coe completion< etc)))
>
!"#" #s) 4'thon.
Running From Comm"n Line
python hello.py
javac Hello.java
java Hello
hello.py is compile to
4'thon 1'tecoe< 3hich
is then interprete
e5plicit compile step<
gener"ting !"#" 1'tecoe
in .class file(s+
sep"r"te comm"n runs
the !"#" interpreter on
1'tecoe in Hello.class
B
!"#" #s) 4'thon. St"tic T'ping
4'thon uses dynamic typing.
x = 42
x = "Hello"
!"#" uses static typing.
int x = 42;
x = "Hello";
x is 2ust " l"1el= 3e "re
free to re1in it to " #"lue
of " ifferent t'pe
this ecl"res x to 1e of t'pe
int ("n gi#es it " #"lue+)))
)))so this wont compileC
D
!"#" #s) 4'thon.
Gre"ter Reun"nc'
D #"ri"tions in loop s'nt"5 for iter"ting o#er ch"r"cters
in " string< #"lues in " collection< lines in " file
(comp"re 3ith onl' one o1#ious s'nt"5 in 4'thonC+
9 integer t'pes #s) one in 4'thon
/ flo"ting;point t'pes #s) one in 4'thon)))
Le"rning !"#" properl' t"%es
longer< 1ec"use there is "
lot more of it to le"rnC
-E
4rimiti#e @"lues #s) O12ects
4'thon h"nles "ll #"lues in " uniform 3"'
!"#" m"n"ges the stor"ge for Fprimiti#e #"lues0
"n o12ects ifferentl'.
Inst"nces of the primiti#e t'pes (simple integers<
flo"ting;point num1ers< 1oole"ns< ch"r"cters+ "re
store on the stack
Arr"'s "n o12ects "re store on the heap
--
St"c% G He"p Stor"ge
Stack

Simple C4U instruction mo#es pointer to "lloc"te or


recl"im memor'

Nee to %no3 "t compile time the siHe "n lifetime of


the "t" 1eing store
Heap

new oper"tor "lloc"tes stor"ge "t run time

No nee to %no3 siHe8lifetime of "t" "t compile time<


1ut he"p m"n"gement more complic"te

Stor"ge recl"ime "utom"tic"ll' (garbage collection+


-/
4rimiti#e Numeric T'pes
Integer

byte

short (/ 1'tes+

int (9 1'tes+

long (B 1'tes+
Floating-point

float (9 1'tes+

double (B 1'tes+
(see Ranges.java+
not the s"me "s 4'thon0s int
(3hich h"s unlimite r"nge+
e7ui#"lent to 4'thon0s float
-6
Cre"ting Numeric @"lues
short answer = 42;
int n;
System.out.println(n);
double root2 = 1.414214;
float g = 9.81f;
Type specifier
is required
!"#" st"tements must
en in " semi;colonC
Initi"l #"lue c"n 1e gi#en
referencing "n
uniniti"lise loc"l
#"ri"1le is "n error
f suffi5 re7uire.
#"ri"1le t'pe is float
(see Init.java+
-9
Ioole"ns
boolean active = true;
boolean finished = false;
Usu"ll' encountere "s the result of e#"lu"ting
boolean epressions th"t comp"re t3o #"lues.
answer == 42
n != 0
x < 0.0
x >= 0.0 && x <= 1.0
size < 0 || size > 100
not c"pit"lise
&& inste" of and
|| inste" of or
-?
Ch"r"cters G Strings
4rimiti#e t'pe char represents " Unicoe ch"r"cter.
char c = 'x';
char newline = '\n';
char copyright = '\u00a9';
String cl"ss represents strings of char.
String s = "Hello, World!";
(see http.88ocs)or"cle)com82"#"se8tutori"l82"#"8"t"8strings)html+
no nee for new
(speci"l c"se+
-A
String $ethos
C"n 'ou figure out 3h"t is printe 1' this coeJ
String s = "Hello, World!";
System.out.println(s.length());
System.out.println(s.isEmpty());
System.out.println(s.charAt(1));
System.out.println(s.indexOf('W'));
System.out.println(s.startsWith("H"));
System.out.println(s.endsWith("."));
System.out.println(s.substring(1, 4));
System.out.println(s.replace('o', '*'));
System.out.println(s);
(see http.88ocs)or"cle)com82"#"se8>8ocs8"pi82"#"8l"ng8String)html+
->
Arithmetic G String
E5pressions
&h"t "re the fin"l #"lues of remainder< q< x< message J
int x = 2, y = 5;
int quotient = y / x;
int remainder = y % x;
float p = (3*x + y) / 2.0f;
double q = (double)(3*x + y) / 2;
x = x + 1;
x += 1;
++x;
x++;
String greeting = "Hello";
String name = "Nick";
String message = greeting + " " + name;
-B
Const"nts
final int MAX_SIZE = 100;
final float PI = 3.1415927f;
final double LIGHT_SPEED = 2.9979246e+8
custom"r' (not re7uire+
to use c"ps "n unerscores
(see Const.java+
turns #"ri"1le
into " const"nt
-D
Conition"l E5ecution
if (number < 0) {
System.out.println("Negative number");
System.out.println(number);
}
else if (number > 0) {
System.out.println("Positive number");
System.out.println(number);
}
else
System.out.println("Zero");
Li%e 4'thon< e5cept for else if r"ther th"n elif "n
brackets ("roun e"ch test+
braces ("roun e"ch 1loc% of st"tements+
single st"tement< 1r"ces option"l
/E
Conition"l E5ecution.
switch St"tements
int selection = ...
switch (selection) {
case 1:
System.out.println("Option 1 selected");
break;
case 2:
System.out.println("Option 2 selected");
break;
case 3:
System.out.println("Option 3 selected");
break;
default:
System.out.println("Error - invalid selection");
break;
}
/-
Repetiti#e E5ecution
while
do...while
for
Ffor e"ch0
(see next lecture)
#er' simil"r to 4'thon0s while<
1ut remem1er.
brackets ("roun loop test+
braces ("roun loop 1o'+
#er' ifferent from 4'thon0s forC
(nicer 3"' of 3riting " while loop+
Four t'pes of loop.
//
while Loop E5"mple
class WhileExample
{
public static void main(String[] args)
{
int n = 10;
while (n > 0) {
System.out.println(n);
--n;
}
System.out.println("Bang!");
}
}
loop control #"ri"1le
"lter"tion of loop
#"ri"1le (import"ntC+
(see WhileExample.java+
/6
for Loop E5"mple
class ForExample
{
public static void main(String[] args)
{
for (int n = 10; n > 0; --n) {
System.out.println(n);
}
System.out.println("Bang!");
}
}
1r"ces option"l
3hen loop 1o' is
one st"tement
A for loop is Fs'nt"ctic sug"r0 for " while loop)))
(see ForExample.java+
/9
Summ"r'
&e h"#e

E5"mine the origins of !"#" "n iscusse the %e'


3"'s in 3hich it iffers from 4'thon

Loo%e "t !"#"0s primiti#e "t" t'pes "n "t ho3


inst"nces of these t'pes "re h"nle

Seen ho3 strings "re cre"te "n m"nipul"te

Consiere ho3 !"#" uses if "n switch st"tements for


conition"l e5ecution

Comp"re !"#"0s while "n for loops


/?
L"1 3or%
The @LE lin%s to e5ercises on cre"ting
"n running simple !"#" progr"ms
You shoul 1e "1le to 3or% through
E5ercises -;6 "lre"'
!"#" coe reference on the slies
is "#"il"1le on the @LE "s " zip file
L"1 this "fternoon -;6pm "n 9;Apm

S-ar putea să vă placă și