Lecture1 Intro Java Part1
-
Upload
matei-laura -
Category
Documents
-
view
239 -
download
0
Transcript of Lecture1 Intro Java Part1
-
7/23/2019 Lecture1 Intro Java Part1
1/51
1
Advanced ProgrammingMethodsLecture 1
-
7/23/2019 Lecture1 Intro Java Part1
2/51
2
Lectures OverviewObject-oriented languages:
- Java
- C
!e"t # or $ lectures:
- Lecture 1 and 1%2 Lecture 2: Java basics
- 1%2Lecture 2 and Lecture #: C basics
-
7/23/2019 Lecture1 Intro Java Part1
3/51
#
Java &e'erences
(ruce )c*el+ Thinking in Java
,he Java ,utorials+ 21#.htt/s:%%docs.oracle.com%javase%tutorial%
Java 0 AP+ 21.htt/:%%docs.oracle.com%javase%0%docs%a/i%
J. (loch. )''ective Java+ 2nd )dition. Addison 3esle4+20.htt/s:%%cebul*o.com%Programming-&esources%)''ective52Java52-522nd52)dition./d'
https://docs.oracle.com/javase/tutorial/http://docs.oracle.com/javase/8/docs/api/http://docs.oracle.com/javase/8/docs/api/https://docs.oracle.com/javase/tutorial/ -
7/23/2019 Lecture1 Intro Java Part1
4/51
Java ,echnolog4
-
7/23/2019 Lecture1 Intro Java Part1
5/51
Java language
6ariables7eclaration:
type name_var1[=expr1][, name_var2[=expr2]];
Primitive 7ata ,4/es
,4/e !r. b4te 6alues 7e'ault value
boolean - true+ 'alse 'alse
b4te 1 -120 8 912 ;b4te
-
7/23/2019 Lecture1 Intro Java Part1
6/51
Java Language
)"am/les:boolean gasit=true;
int numar=34, suma;
float eroare=04!;
"#ar litera=$"%;
litera=$f%;
litera=litera&1;
-
7/23/2019 Lecture1 Intro Java Part1
7/51
Java Comments
1.'' entire line
2.'( multiple
lines ('
#.'(( use) by )o"umentation *ava)o" tool
multiple
lines('
Obs: Comments canot be nested into strings.
'( '(
(' (' +- ./
'(
''
''
(' .//
-
7/23/2019 Lecture1 Intro Java Part1
8/51
Java Constants
final type name [=value];
)"am/les:
a< final int =100;
b< final int ;
=100;8
=1!0; ''error
-
7/23/2019 Lecture1 Intro Java Part1
9/51
Arra4
Arra4 with one dimension
type[] name;
type name[];
Arra4 allocation:
array_name=ne type[)im]; ''memory allo"ation
''in)ex 0 )im1
Accessing an arra4 element: arra4Bname[in)ex]
)"am/les:
float[] ve";
ve"=ne float[10];
int[] sir=ne int[3];
float tmp[];
tmp=ve"; ''ve" an) tmp refer to t#e same array
-
7/23/2019 Lecture1 Intro Java Part1
10/51
One dimension Arra4
Built-in length: returns the arra4 dimension.int[] sir=ne int[!];
int lung_sir=sirlengt#; ''lung=!;
sir[0]=sirlengt#;
sirlengt#=2; ''error
int[] y;
int lung_y=ylengt#; ''error, y as not "reate)
)ouble[] t=ne )ouble[0];
int lung_t=tlengt#; ''lung_t=0;
t[0]=23 ''error5 in)ex out of boun)s
t#e s#ort"ut syntax to "reate an) initiali6e an array5
int[] )x=71,0, 18;
-
7/23/2019 Lecture1 Intro Java Part1
11/51
&ectangular multidimensional arra4
7eclaration:type name[][][][];
type[][][][] name;
Creation:
name=ne type[)im1][)im2][)imn];
Accessing an element:
name[in)ex1][in)ex2][in)exn];
)"am/les:
int[][] a;a=ne int[!][!];
a[0][0]=2;
int x=a[2][2]; ''x=9
-
7/23/2019 Lecture1 Intro Java Part1
12/51
!on-&ectangular Multidimensional Arra4
)"am/les:int[][] a=ne int[3][];
for:int i=0;i3;i&&ab"?;
s=s&?123?; ''"on"atenating strings
Atring t=s;''t=>ab"123?
t&=>12?; ''t=9, s=9
Dtring content can not be changed: t[0]=$%;
"#ar "=t"#art:0
-
7/23/2019 Lecture1 Intro Java Part1
14/51
O/eratorsarihtmetic: &, , (, ', F
relational: E, E=, , =, /=, ==
increment%decrement:&&,
prefix5 int a=2;
int b=&&a; ''a=2, b=2
postfix5 int a=2;
int b=a&&; ''a=3, b=2
assignment: =, &=, =, (=, '=
conditional:GG, HH, /
bitise5 shi'tEE, , EEE,
- conditional G, H, I :not
-
7/23/2019 Lecture1 Intro Java Part1
15/51
O/erator /recedence
O/erators Precedence ;higher /recedence on to/+ the same /recedence on the same lineab"MZ?
7estructor: n Java there is no an4 destructor. ,he garbage collector deallocates the memor4 .
-
7/23/2019 Lecture1 Intro Java Part1
41/51
Objects as Parameters
Objects can be 'ormal /arameters 'or the methods
A method can return an object or an arra4 o' objects.
publi" "lass Cational7
private int numarator, numitor;
''Donstru"tors
publi" voi) a)una:Cational r
-
7/23/2019 Lecture1 Intro Java Part1
42/51
Passing arguments
Primitive t4/e arguments ; boolean+ int+ b4te+ long+ double< are /assed b4 value. ,heir
values are co/ied on the stac*. Arguments o' re'erence t4/e are /assed b4 value. A re'erence to them is co/ied on the
stac*+ but their content ;'ields 'or objects+ locations 'or arra4< can be modi'ied i' themethod has the rights to accces them.
1. ,here is not an4 wa4 to change the /assing mode; li*e in C99
-
7/23/2019 Lecture1 Intro Java Part1
43/51
Passing arguments
"lass S7
int val; publi" S:int x
-
7/23/2019 Lecture1 Intro Java Part1
44/51
Passing arguments
"lass S7
int val; publi" S:int x
-
7/23/2019 Lecture1 Intro Java Part1
45/51
Arra4 o' objects )ach arra4 element must be allocated and intialied.
publi" "lass -ablouribie"te 7
stati" voi) generea6a:int nrMlem, Domplex[] t
-
7/23/2019 Lecture1 Intro Java Part1
46/51
Arra4 o' objects stati" Domplex suma:Domplex[] t
-
7/23/2019 Lecture1 Intro Java Part1
47/51
,he methods toDtring and eEuals
publi" "lass Domplex7 private )ouble real, imag;
publi" Domplex:)ouble re, )ouble im
-
7/23/2019 Lecture1 Intro Java Part1
48/51
Dtatic methods
Are declared using the *e4word stati"
-#ey are s#are) by all "lass instan"es
publi" "lass Domplex7
private )ouble real, imag;
publi" Domplex:)ouble re, )ouble im
-
7/23/2019 Lecture1 Intro Java Part1
49/51
Dtatic methods
,he4 are called using the class name:
Domplex a,b;'' initiali6ation a an) b
Domplex "=Domplexa)una:a, b
-
7/23/2019 Lecture1 Intro Java Part1
50/51
Dtatic 'ields
publi" "lass +atural7private long val;
publi" stati" long =232 ''2JN31
''
8
publi" "lass Xro)us 7
private stati" long "ounter;
private final long i)="ounter&&;
publi" Atring toAtring:
-
7/23/2019 Lecture1 Intro Java Part1
51/51
Dtatic 'ields
nitialiation:
At declaration site:publi" stati" long =2000;
n a s/ecial intialiation bloc*
publi" "lass +atural 7
publi" stati" long ;
stati" 7
=2000;
8
' a static 'ield is not intialied+ it will ta*e the de'ault value o' its t4/e:
private stati" long "ounter; ''0