Documente Academic
Documente Profesional
Documente Cultură
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
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