Chap 2

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1sur 16

!

"
" #
$

%
& # $
'
( )
* +
,
- )

.
/ 0 1
2 (
0
0 0 ,
2 (
/ ) 0
2
,
, (
3 )

4
/
/ 5&5
/

" #
// commentaire sur une seule ligne
$ $
int N=1; // déclaration du compteur
/* commentaires ligne 1
commentaires ligne 2 */
/**
* commentaire de la methode
* @param val la valeur a traiter
* @since 1.0
% * @return Rien
* @deprecated Utiliser la nouvelle methode
XXX
*/

6
6%
-
-
7
,
*
&
&

# &
long nombre;
int compteur;
String chaine;

(
/ ,

# &
int jour, mois, annee ;

)
)
1 ( (
/ (
# ( $ 9
:

# &
MaClasse instance; // déclaration de l'objet

instance = new MaClasse(); // création de l'objet

OU MaClasse instance = new MaClasse(); // déclaration et création de l'objet


# &
int[] nombre = new int[10];

/ )

# &
int i=3, j=4 ;

6'
;
) , ;

" $ ' ( )

% &

* $ + , -+ . -/
$ 0 ,1-/0+ . 1-/0/
$
2 3 0 4 5555 . 4 6666

$ 1- ,- / +10 + . - / +10 /

$ 1- 5 ,5 8 . 1 5-+- 951+
7###/8

- --85/ ,15+ .
$ 0
/:/0: 915+
7###/8
,:--11/-510+8 //8+5+ .
0
:--11/-510+8 //8+5/

6.

, 1
< 1
=
> ,
? @ /

# &
float pi = 3.141f;
double v = 3d
float f = +.1f , d = 1e10f;

1
?

# &
double w = 1.1;

; & float pi = 3.141; // erreur à la compilation

- %A ) - / ,
- ,
<)A66.6

# &
/* test sur les caractères */
class test1 {
public static void main (String args[]) {
char code = 'D';
int index = code - 'A';
System.out.println("index = " + index);
}
}

64
# &
int nombre; // déclaration
nombre = 100; //initialisation
OU int nombre = 100; //déclaration et initialisation

B ) ( # ($

!
" ( $

< < < 5


< 55
4 555

66
C
C )

x = y = z = 0;
/
)

* $ # =
> > 5 #% .& > 5
9> 9> 5 #% .& > 9 5
,> ,> #% .& > ? 5
@> @> #% .& > @ 5
A> A> 5 #% .& > A 5
B> B> 5
C> C> 5 #% .& > C 5
#% . & > DD 5 $ $ !$ .
DD> DD> 5

#% . & > EE 5 $ $ !$ .
EE> EE> 5

; & ' F $ $ $ <


$ $ $ $ G <
F
$ ' F $ $ <
$ F $

6A

* $ # =
E E 5 $
D D 5 $

"
E> E> 5 $ $
D> D> 5 $ $
>> >> 5 # $
H> H> 5 2
I I #"
C C *3
J J *3
#" % $ & F$
II II
F 2 % F
*3 % $ & F$
JJ JJ
F 2 % F
$ &
K& K & F$ F &
L

)
)

# $

C )

A
D# $ ; # $ E# $ F
# $ G # $ / )

# &
nombre += 10;

A%
1

# &
short x = 5, y = 15;
x = x + y ; //erreur à la compilation

Incompatible type for =. Explicit cast needed to convert int to short.

#
x = x + y ; //erreur à la compilation
^
1 error

# &
x = (short) (x + y);

= H B

A'
H =

? II @ I I# $
? 1JK/
L/3B*/
I?/
I/LM @ 1JK/
L/3B*/
I?/
I/LM D ∞
? IBNHL/
3B*/
I?/
I/LM @ IBNHL/
3B*/
I?/
I/LM ; ∞
) /
BBBO64
II

M N MAN MBN
5 9∞ O O

9A, ∞ 5 M
5 5 O O O O
9A, ∞ 9A, ∞ O O

9A, ∞ 9A, ∞ O O O O

# &
/* test sur la division par zero de nombres flottants */

class test2 {
public static void main (String args[]) {
float valeur = 10f;
double résultat = valeur / 0;
System.out.println("index = " + résultat);
}
}

$
A.
n++ ++n n-- --n
K # $
) # $
DD

# &
System.out.println(x++); // est équivalent à
System.out.println(x); x = x + 1;

System.out.println(++x); // est équivalent à


x = x + 1; System.out.println(x);

O
#
$

2
99
$ F $
,,
@
$ < <
A
B
9
$ F
,
DD
$ $
EE
D
E
$
D>
E>
>>
$ F$ $
H>
F $ *3 C
F $ #" I
F $ *3 J
F $ #" % II
F $ *3 % JJ
>
$ F 9>
,>

%
,

P !
(

P%

9 # $
Q
F
F
R

K 9
(

R9 # $

&

# & & $Q

# &
for (i = 0 ; i < 10; i++ ) { ....}
for (int i = 0 ; i < 10; i++ ) { ....}
for ( ; ; ) { ... } // boucle infinie

@
)
/
,

# &
for (i = 0, j = 0 ; i * j < 1000;i++, j+= 2) {....}

# &
boolean trouve = false;

&
for (int i = 0 ; !trouve ; i++ ) {
if ( tableau[i] == 1 )
trouve = true;
... //gestion de la fin du parcours du tableau
}

P'

# $Q

R # $Q

R Q

9 # $Q
%
%%&
%'&
>&

'

J 9 .' #
$
K >

/ 9
# $S ; ;

# &
if (niveau == 5)
total = 10;
else total = 5;
// equivalent à total = (niveau == 5) ? 10 : 5;

P.
> -
!
)
T
/ J (
<

T%

# &
int tableau[] = new int[50]; // déclaration et allocation

OU int[] tableau = new int[50];

OU int tab[]; // déclaration


tab = new int[50]; //allocation

# &
float tableau[][] = new float[10][10];

# &
int dim2[][] = new int[3][];
dim2[0] = new int[4];
dim2[1] = new int[9];
dim2[2] = new int[2];

9 <
U<
(

T'
# &
int tableau[] = {10,20,30,40,50};
int tableau[][] = {{5,1},{6,2},{7,3}};

# &
int tableau[] = {10,20,30,40,50};

,
# &
int[][] tabEntiers = {{1,2,3,4,5,6},
{1,2,3,4},
{1,2,3,4,5,6,7,8,9}};

T.
# &
for (int i = 0; i < tableau.length ; i ++) { ... }

( (
-
( H / J JV B

%< "

# &
char touche = '%';

"

# &
String texte = “bonjour”;

K ( 1 7
( K

K
(

# &
String texte = "Java Java Java";
texte = texte.replace('a','o');

( HK /
/ HIK/ -
#
- $ " (
- ' < ) '66
( HK /
/
%<% "
) 2 $ ;
4F ;
4P Q
44
4 "
4 2
4
4
4
45 ) 2 ;=)77
4 2 ;=)77 $
4 2 3 $

%<' "
D
D " /
DC

# &
String texte = “ ”;
texte += “Hello”;
texte += “World3”;

) "
" ( )

# &
System.out.println("La valeur de Pi est : "+Math.PI);
int duree = 121;
System.out.println(" durée = " +duree);

%<. "
/ #
$

# &
String texte1 = "texte 1";
String texte2 = "texte 2";
if ( texte1.equals(texte2) )...
%<4 "
#
$ "

# &
String texte = "texte";
int longueur = texte.length();

%<6
- #
$ 9 #
$
" (

# &
String texte = "texte";
String textemaj = texte.toUpperCase();

%%

# &
int entier = 5;
float flottant = (float) entier;

"
H1/

) RS
= G 2 3
7 G 2
' G $
6 G .
G . $

,
(
1
(

!
# &
String texte;
texte = new String(“test”);

( ( K

%% % K
# & T U
int i = 10;
String texte = new String();
texte = String.valueOf(i);

%% ' " K

# & T U
String texte = new String(“10”);
Integer nombre = new Integer(texte);
int i = nombre.intValue(); //conversion d'Integer en int

Ou

int i = Integer.valueOf(texte).intValue();

Ou

int i = Integer.parseInt(“10”);

@ #
K $ ? #
K $ V #
K $ #
K $

J #
K $ ( V ?
@

%% .
# &
int i = 10;
Integer nombre = new Integer(i);
long j = nombre.longValue();

"

Vous aimerez peut-être aussi