9DGlU-DYD"
•
•
•
•
+LVWRULN
Java A - 1
-DYD lU HWW JHQHUHOOW REMHNWRULHQWHUDW
SURJUDPPHULQJVVSUnN
-DYD KDU HQ RPIDWWDQGH XSSVlWWQLQJ
DY VWDQGDUGLVHUDGH ELEOLRWHN
3URJUDP XWYHFNODGH L -DYD N|UV YLD HQ
YLUWXHOO MDYDPDVNLQ
-DYD KDU VW|G I|U JUDILVND
DQYlQGDUJUlQVVQLWW
•
-DYD NDQ KDQWHUD SDUDOOHOOD SURFHVVHU
•
-DYD KDU VW|G I|U GLVWULEXHUDGH
V\VWHP
Java A - 2
• -DPHV *RVOLQJ
• 6WDUW L E|UMDQ DY WDOHW Sn 681
• 6Pn LQE\JJGD V\VWHP
• 6SUnNHW KHWWH 2DN LQJHQ YLOOH KD
GHW • :HEEUHYROXWLRQHQ NRP
• 6QDEEW E\WH DY LQULNWQLQJ
• 1\WW PDUNQDGVPlVVLJW QDPQ -DYD
• 6WRU VSULGQLQJ
(JHQVNDSHU
•
Java A - 3
Java A - 4
3ODWWIRUPVREHURHQGH
(QNHOW PHQ RPIDWWDQGH
-DYD lU HWW OLWHW YlO GHILQLHUDW VSUnN 0HQ WLOO
VSUnNHW K|U RPIDWWDQGH ELEOLRWHN
1DPQMDYD
•
2EMHNWRULHQWHUDW
, VSUnNHW ILQQV VW|G I|U NODVVHU RFK REMHNW DUY
-DYD NRPSLODWRU
SRO\PRUILVP RFK LQNDSSVOLQJ DY GDWD
•
1DPQFODVV
3ODWWIRUPVREHURHQGH
'HQ YLUWXHOOD -DYDPDVNLQHQ PHGI|U DWW HWW SURJUDP
NDQ N|UDV L ROLND RPJLYQLQJDU
•
5REXVW
-DYD XQGHUVW|GHU VNDSDQGHW DY UREXVWD SURJUDP
GYV SURJUDP VRP lU I|UXWVlJEDUD RFK VRP LQWH
NUDVFKDU
•
-DYD 90 I|U
-DYD 90 I|U
-DYD 90 I|U
:LQGRZV 17
0DFLQWRVK
81,;
0RGHUQW VSUnN
,QQHKnOOHU VW|G I|U PnQJD DYDQFHUDGH WHNQLNHU WH[
-DYD%HDQV -DYD 'DWD %DVH &RQQHFWLYLW\ RFK 5HPRWH
0HWKRG ,QYRFDWLRQ
90
9LUWXHOO 0DVNLQ
'HWI|UVWD
SURJUDPPHW
Java A - 5
public class Hej
{
public static void main(String[] args)
{
System.out.println("Hej på dig!");
}
}
6NULY
NRGHQ L HQ WH[WHGLWRU WH[ ;HPDFV
'HQ ILO
,
VRP LQQHKnOOHU MDYDNRGHQ PnVWH JHV lQGHOVHQ µMDYDµ
GHW IDOO ILOHQ LQQHKnOOHU HQ SXEOLNW GHNODUHUDG NODVV Vn PnVWH
Java A - 6
'DWDW\SHU
•
-DYD KDU HWW DQWDO I|UGHILQLHUDGH W\SHU
VSUnNHWV SULPLWLYD HOOHU HQNOD GDWDW\SHU
•
'H I|UGHILQLHUDGH GDWDW\SHUQD KDU
UHVHUYHUDGH QDPQ RFK KDQWHUDV Sn HWW VSHFLHOOW
VlWW DY NRPSLODWRUQ
• +HOWDO
byte
short
int
long
ELWDUV KHOWDO PHG WHFNHQ
ELWDUV KHOWDO PHG WHFNHQ
ELWDUV KHOWDO PHG WHFNHQ
ELWDUV KHOWDO PHG WHFNHQ
ILOHQ G|SDV H[DNW WLOO GHW QDPQ VRP GHQ SXEOLND NODVVHQV KDU SOXV
lQGHOVHQ µMDYDµ
,
GHW KlU H[HPSOHW EOLU GHW µ+HMMDYDµ
.RPSLOHUD NRGHQ HQOLJW I|OMDQGH
==> javac Hej.java
)LOHQ
µ+HMFODVVµ VNDSDV DY NRPSLODWRUQ )LOHQ LQQHKnOOHU E\WHNRG
VRP PRWVYDUDU NlOONRGHQ L ILOHQ µ+HMMDYDµ
6WDUWD
SURJUDPPHW YLD GHQ YLUWXHOOD -DYDPDVNLQHQ JHQRP DWW
DQJH QDPQHW Sn GHQ NODVV YDUV PDLQIXQNWLRQ VND N|UDV
==> java Hej
• 'HFLPDOWDO
float
double
ELWDUV IO\WWDO
ELWDUV IO\WWDO
• /RJLVND W\SHU
boolean
(QGDVW YlUGHQD true HOOHU false
• 7HFNHQ
char
(QGDVW ELWDUV 8QLFRGH WHFNHQ
5HIHUHQVYDULDEOHU
•
Java A - 7
, -DYD ILQQV WYn NDWHJRULHU DY YDULDEOHU
UHIHUHQVYDULDEOHU RFK HQNOD YDULDEOHU
•
(QNOD YDULDEOHU lU YDULDEOHU YDUV W\S lU QnJRQ DY GH
SULPLWLYD GDWDW\SHUQD L -DYD
•
Java A - 8
.RQVWDQWHU
0HG KMlOS DY GHW UHVHUYHUDGH RUGHW final
Vn lU GHW P|MOLJW DWW GHILQLHUD NRQVWDQWHU
public class Cirkel
{
public final double pi = 3.14159;
private double radie = 0;
, -DYD NDQ PDQ LQWH NRPPD nW HWW REMHNW GLUHNW XWDQ
public Cirkel(double r)
{
radie = r;
}
PDQ PnVWH DOOWLG Jn YLD HQ UHIHUHQVYDULDEHO
•
'HNODUDWLRQHQ
Kvadrat rutan;
%HW\GHU LQWH DWW REMHNWHW
•
rutan
rutan
public double yta()
{
return radie * radie * pi;
}
VNDSDV
lU HQ UHIHUHQV VRP KDU I|UPnJDQ DWW NXQQD
UHIHUHUD WLOO REMHNW DY NODVVHQ
public static void main(String[] args)
{
Cirkel enCirkel = new Cirkel(10.0);
Kvadrat
rutan = new Kvadrat(12);
System.out.println(enCirkel.yta());
•
0HG QnJUD In XQGDQWDJ Vn NDQ PDQ HQGDVW VNDSD Q\D
QHZµ L -DYD
REMHNW PHG KMlOS DY µ
}
}
(WWSURJUDPPHGIOHUD Java A - 9
NODVVHUGHO
.ODVVGLDJUDP HQOLJW
80/
.YDGUDW
SULYDWH GRXEOH VLGD
SXEOLF GRXEOH \WD
&LUNHO
(WWSURJUDPPHGIOHUD
NODVVHUGHO
Java A - 10
//Fil: Cirkel.java
//Fil: Kvadrat.java
public class Cirkel
public class Kvadrat
{
{
public final double pi = 3.14159;
private double sida = 0;
private double radie = 0;
public Kvadrat(double a)
public Cirkel(double r)
{
{
sida = a;
radie = r;
}
}
public double yta()
public double yta()
{
{
return sida * sida;
return radie * radie * pi;
}
}
}
public static void main(String[] args)
{
Cirkel enCirkel = new Cirkel(10.0);
SXEOLF ILQDO GRXEOH SL
SULYDWH GRXEOH UDGLH
System.out.println(enCirkel.yta());
SXEOLF GRXEOH \WD
}
}
SXEOLF VWDWLF YRLG PDLQ6WULQJ>@ DUJV
2%6 IOHUD NODVVHU VRP VDPYHUNDU L HWW SURJUDP
NDQ LQQHKnOOD YDU VLQD PDLQPHWRGHU
7HVW
SXEOLF VWDWLF YRLG PDLQ6WULQJ>@ DUJV
public class Main //Fil: Main.java
{
public static void main(String[] args)
{
Kvadrat kvadraten = new Kvadrat(3.0);
Cirkel cirkeln = new Cirkel(10.0);
System.out.println(kvadraten.yta());
System.out.println(cirkeln.yta());
}
}
==> javac -g Main.java Kvadrat.java Cirkel.java
==> java Main
9.0
314.159
==>
==> javac -g Main.java
==> java Main
9.0
314.159
==>
Java A - 11
Java A - 12
(QILOPHGIOHUDNODVVHU
//Fil: MinaKlasser.java
WKLV
•
9DUMH REMHNW KDU HQ UHIHUHQV VRP UHIHUHUDU WLOO
WKLVµ
class Cirkel
{
public final double pi = 3.14159;
private double radie = 0;
REMHNWHW 'HQQD UHIHUHQV EHQlPQV µ
public class Kvadrat
{
private double sida = 0;
public Cirkel(double r)
{
radie = r;
}
public Kvadrat(double sida)
{
this.sida = sida;
}
public double yta()
{
return radie * radie * pi;
}
}
public static void main(String[] args)
{
Cirkel enCirkel = new Cirkel(10.0);
•
System.out.println(enCirkel.yta());
(Q NRQVWUXNWRU NDQ DQURSD HQ DQQDQ NRQVWUXNWRU L VLQ
}
WKLVµ
NODVV PHG KMlOS DY GHW UHVHUYHUDGH RUGHW µ
}
class Main
{
public static void main(String[] args)
{
Kvadrat kvadraten = new Kvadrat(3.0);
Cirkel cirkeln = new Cirkel(10.0);
System.out.println(kvadraten.yta());
System.out.println(cirkeln.yta());
}
}
==> javac -O MinaKlasser.java
==> java Main
9.0
314.159
==> ls *.class
Kvadrat.class Cirkel.class Main.class
public class Kvadrat
{
private double sida = 0;
public Kvadrat()
{
this(7.0); // Får endast förekomma på den
}
// första raden
public Kvadrat(double sida)
{
this.sida = sida;
}
2 JHU RSWLPHUDG
E\WHNRG
J
OlJJHU WLOO GHEXJ
LQIRUPDWLRQ
}
Java A - 13
Java A - 14
.RQWUROOVWUXNWXUHU
2SHUDWRUHU
•
-DYD KDU L VWRUW VHWW PRWVYDUDQGH XSSVlWWQLQJ DY RSHUDWRUHU VRP
7\S DY VWUXNWXU
.RQVWUXNWLRQ
9LONRU
LI HOVH
&
•
/lJJ GRFN PlUNH WLOO DWW RSHUDWRUHU I|U DWW PDQLSXOHUD SHNDUH
VDNQDV
•
VZLWFK
2SHUDWRUHUQD lU XSSUlNQDGH L IDOODQGH SULRULWHW
FDVH
2SHUDWRUEHVNULYQLQJ
,WHUDWLRQ
6SUnNV\PERO
ZKLOH
GR ZKLOH
3RVWIL[ RSHUDWRUHU
>@
8QlUD RSHUDWRUHU
XWWU
6NDSDQGH RFK RPW\SQLQJ
QHZ
W\SXWWU
0XOWLSOLNDWLRQ RFK GLYLVLRQ
$GGLWLRQ RFK VXEWUDNWLRQ
6NLIW RSHUDWRUHU
-lPI|UHOVH RSHUDWRUHU
%LWYLV H[NOXVLYWHOOHU
!
XWWU
XWWU
XWWU
a
$YEURWW
EUHDN
FRQWLQXH
!!!
!
LQVWDQFHRI
2EVHUYHUD DWW RP NRQWUROOYDULDEHOQ GHNODUHUDV L IRUVDWVHQ Vn lU
GHVV V\QOLJKHW EHJUlQVDG WLOO IRUVDWVHQV SURJUDPEORFN VH
A
%LWYLV HOOHU
_
/RJLVNW
/RJLVNW HOOHU
__
9LONRUVRSHUDWRUQ
" 7LOOGHOQLQJ
IRU
XWWU
UHWXUQ
!!
/LNKHWVMlPI|UHOVH RSHUDWRU
%LWYLV RFK
XWWU
I|OMDQGH H[HPSHO
int summa = 0;
for (int i=0; i<10; i++)
{
summa += i;
}
!!!
!!
A
_
System.out.println(summa);
System.out.println(i);
// OK, fungerar
// FEL!!! i existerar ej!
Java A - 15
Java A - 16
5HVHUYHUDGHRUG
.ODVVYDULDEOHU
----------------------------------------------------------------abstract
double
int
boolean
else
interface switch
break
extends
long
synchronized
final
native
this
case
finally
new
throw
catch
float
package
throws
char
for
private
transient
class
goto
protected
try
const
public
void
continue
implements
return
volatile
default
import
short
const
RFK
goto DQYlQGV
6NLOM Sn NODVVYDULDEOHU RFK LQVWDQVYDULDEOHU
(Q NODVVYDULDEHO H[LVWHUDU L VLQ NODVV
(Q NODVVYDULDEHOV YlUGH lU JHPHQVDP I|U DOOD REMHNW
VRP lU LQVWDQVHU DY VDPPD NODVV
•
•
•
if
(Q LQVWDQVYDULDEHO H[LVWHUDU L VLWW REMHNW
(Q LQVWDQVYDULDEHOV YlUGH WLOOK|U DOOWLG HWW REMHNW
(Q NODVVYDULDEHO GHNODUHUDV PHG KMlOS DY GHW
UHVHUYHUDGH RUGHW µVWDWLFµ
while
goto
instanceof
static
-----------------------------------------------------------------
'H UHVHUYHUDGH RUGHQ
•
•
•
super
public class Kvadrat
{
private static int antalInstanser = 0;
private double sida = 0;
LQWH L
public Kvadrat(double a)
{
sida = a;
antalInstanser++;
}
VSUnNHW PHQ lU lQGn UHVHUYHUDGH I|U DWW NRPSLODWRUQ
XWWU\FNOLJHQ VNDOO VlJD DWW GHVVD RUG HM ILQQV L -DYD EnGD
ILQQV L &
public static void main(String[] args)
{
System.out.println(antalInstanser);
Kvadrat rutan = new Kvadrat(4.2);
System.out.println(antalInstanser);
}
}
8WVNULIW
==> java Kvadrat
0
1
==>
Java A - 17
Java A - 18
)lOW
.ODVVPHWRGHU
•
•
•
•
•
(Q NODVVPHWRG H[LVWHUDU L VLQ NODVV
(Q NODVVPHWRG NDQ DQURSDV GLUHNW YLD VLQ NODVV
(Q LQVWDQVPHWRG H[LVWHUDU L VLWW REMHNW
•
(WW IlOW lU HQ RUGQDG VDPOLQJ DY HOHPHQW
•
(WW IlOW NDQ L -DYD VHV VRP HQ W\S DY REMHNW
•
(Q GHNODUDWLRQ DY HQ UHIHUHQV WLOO HWW IlOWREMHNW J|UV
JHQRP DWW DQYlQGD KDNSDUDQWHVHUQD µ> @µ HIWHU
(Q LQVWDQVPHWRG PnVWH DQURSDV YLD VLWW REMHNW
W\SVSHFLILNDWLRQHQ HOOHU HIWHU QDPQHW
(Q NODVVPHWRG NDQ HQGDVW RSHUDUD Sn NODVVYDULDEOHU RFK
ORNDOD YDULDEOHU GHNODUHUDGH L NODVVPHWRGHQ
•
(Q NODVVPHWRG GHNODUHUDV PHG KMlOS DY GHW UHVHUYHUDGH
RUGHW µVWDWLFµ
int[] intArray;
•
IlOWREMHNW DY W\SHQ µHWW IlOW DY KHOWDOµ
public class Kvadrat
{
private static int antalInstanser = 0;
private double sida = 0;
•
intArray NDQ
intArray = new int[10];
•
public Kvadrat(double a)
{
sida = a;
antalInstanser++;
}
}
)|U DWW VNDSD HWW IlOW DY KHOWDO VRP
UHIHUHUD WLOO DQYlQGV QHZRSHUDWRUQ
public static int antal()
{
return antalInstanser;
}
public static void main(String[] args)
{
System.out.println(Kvadrat.antal());
Kvadrat rutan = new Kvadrat(4.2);
System.out.println(rutan.antal());
rutan = new Kvadrat(4.2);
System.out.println(antal());
}
2EMHNWUHIHUHQVHQ LQW$UUD\ lU QX HQ UHIHUHQV WLOO HWW
+lU VNDSDV HWW IlOW PHG WUH
double
double[] tal = new double[3];
•
2FK KlU VNDSDV HWW IlOW PHG WUH UHIHUHQVHU WLOO REMHNW DY
W\SHQ .YDGUDW
8WVNULIW
==> java Kvadrat
0
1
2
==>
.YDGUDW[]
rutor = new Kvadrat[];
Java A - 19
Java A - 20
0HUDRPIlOW
(WWH[HPSHOPHG)lOW
public class Kvadrat
{
private double sida = 0;
public Kvadrat(double a)
{
sida = a;
}
8WVNULIW
==> java Kvadrat
20.0
4.41
2
4.0
==>
public double yta()
{
return sida * sida;
}
public static void main(String[] args)
{
double decimalTal[] = new double[2];
decimalTal[0] = 10;
decimalTal[1] = 20;
Kvadrat[] ruta = new Kvadrat[2];
ruta[0] = new Kvadrat(1.5);
ruta[1] = new Kvadrat(2.1);
System.out.println(decimalTal[1]);
System.out.println(ruta[1].yta());
/*************************************************
* Fält kan även initieras på följande sätt!
* Metoden kan dock inte användas för att tilldela
* elementen i ett redan existerande fält andra
* värden
**************************************************/
int[] helTal = {1, 2, 3};
Kvadrat[] fyrkant = {new Kvadrat(1), new Kvadrat(2)};
System.out.println(helTal[1]);
System.out.println(fyrkant[1].yta());
}
}
typ[] f;
'HNODUHUDU HQ UHIHUHQV WLOO HWW IlOW DY W\SHQ typ
typ[] f = new[antal];
'HNODUDWLRQ RFK LQLWLHULQJ
typ[] f = {värde, värde, ... , värde};
'HNODUDWLRQ RFK LQLWLHULQJ
f = new typ[]{värde, värde, ..., värde};
f WLOOGHODV HWW Q\WW IlOW
typ[] g = f;
g UHIHUHUDU WLOO VDPPD
IlOW VRP f
g = (typ[]) f.clone();
g UHIHUHUDU WLOO HQ NRSLD DY f
f.length
*HU DQWDO HOHPHQW L IlOWHW
f
System.arraycopy(f, fStart, g, gStart, antal);
.RSLHUDU antal HOHPHQW IUnQ RFK PHG f[fStart] WLOO
g[gStart] RFK I|OMDQGH HOHPHQW
f == g
8QGHUV|NHU RP
f RFK g
UHIHUHUDU WLOO VDPPD IlOW
Arrays.equals(f, g)
8QGHUV|NHU RP IlOWHQ f RFK g lU OLND
Java A - 21
Java A - 22
6WULQJ
3DUDPHWHU|YHUI|ULQJ
public class Tal
{
private int tal = 0;
•
/* Varför fungerar inte denna funktion?
static void swap(Tal tal1, Tal tal2)
{
Tal temporärReferens = tal1;
•
.ODVVHQ 6WULQJ DQYlQGV I|U DWW UHSUHVHQWHUD HQ
VHNYHQV DY WHFNHQ G Y V HQ VWUlQJ
.ODVVHQ 6WULQJ NDQ L YLVVD IDOO GHNODUHUDV RFK
LQLWLHUDV Sn VDPPD VlWW VRP YDULDEOHU DY HQNOD
GDWDW\SHU
tal1 = tal2;
tal2 = temporärReferens;
}
•
*/
HWW Q\WW REMHNW
static void swap(Tal[] par)
{
Tal temporärReferens = par[0];
String s1 = ”Musse ”;
String s2 = ”Pigg”;
par[0] = par[1];
par[1] = temporärReferens;
// s1 och s2 slås ihop, resultatet sparas i s3
String s3 = s1.concat(s2);
//s3 == ”Musse Pigg”
}
public static void main(String[] args)
{
Tal a = new Tal();
Tal b = new Tal();
a.tal = 1;
b.tal = 2;
//swap(a, b);
//En modifierad kopia av s1 tilldelas s4
String s4 = s1.toLowerCase(); //s4 == ”musse”
•
Fungerar inte!
swap(tal);
a = tal[0];
b = tal[1];
System.out.println(a.tal);
System.out.println(b.tal);
}
(IWHUVRP VWUlQJNRQVWDQWHU L -DYD lU HNYLYDOHQWD
PHG 6WULQJREMHNW NDQ GH DQYlQGDV GLUHNW L XWWU\FN
String s = ”abcde”.substring(1,3);
Tal[] tal = {a, b};
}
(Q LQVWDQV DY 6WULQJ JnU HM PRGLILHUD ,VWlOOHW VNDSDV
•
//s == "bc"
3n VWUlQJDU lU RSHUDWRUQ WLOOnWHQ RFK LQQHElU Gn
VDPPD VDN VRP NRQNDWHQHULQJ DY WYn VWUlQJDU
String s1 = ”Kalle ”;
String s2 = ”Anka”;
String s = s1 + s2 + ”!”;
1nJUDH[HPSHOPHG
VWUlQJDU
public class StringDemo
{
public static void main(String[] argument)
{
String namn = "
Kalle Anka´s lyckotal är 13
Java A - 23
0HWRGHULNODVVHQ6WULQJ
([HPSHO Sn QnJUD NRQVWUXNWRUHU
";
//trim() rensar bort inledande och avslutande blanktecken
namn = namn.trim();
String kalle = "Kalle";
String ankas = "Anka´s";
String kAnka = kalle + ' ' + ankas + " lyckotal är " + 13;
System.out.println((namn == kAnka) ? "Lika" : "Ej lika");
System.out.println((namn.equals(kAnka)) ? "Lika" : "Ej lika");
}
}
public class ByteDemo
{
public static void main(String[] argument)
{
byte[] byteData = "ÅÄÖåäö".getBytes();
for (int i=0; i<byteData.length; i++)
System.out.println(byteData[i]);
String åäöSträngen = new String(byteData);
System.out.println(åäöSträngen);
}
}
public class ArgDemo
{
public static void main(String[] argument)
{
for (int i=0; i<argument.length; i++)
System.out.println(argument[i]);
}
}
Java A - 24
==> java ByteDemo
-59
-60
-42
-27
==> java StringDemo ==> java ArgDemo Grodan Boll -28
-10
Grodan
Ej lika
ÅÄÖåäö
Boll
Lika
==>
==>
==>
String()
String(String value)
String(char[] value)
String(byte[] value)
([HPSHO Sn QnJUD PHWRGHU
char charAt(int index)
int compareTo(String anotherString)
boolean equals(Object anObject)
byte[] getBytes()
int hashCode()
indexOf(int ch)
indexOf(String str)
int length()
char[] toCharArray()
String toLowerCase()
String toUppercase()
String trim()