Вы находитесь на странице: 1из 28

Java

3.
20 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( ) <hel@cos.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

Java.
, , ,
() .
( tokens ) - "",
Java.
,
, .
.

Rendered by www.RenderX.com

ii

3. ....................................................................................................1
1. ................................................................................................................ 1
1.1. ..............................................................................................................2
1.2. ............................................................................................... 2
1.2.1. ....................................................................................................... 3
1.2.2. .................................................................................................4
1.2.3. ........................................................................................................8
1.3. ......................................................................................................... 9
1.3.1. ......................................................................................... 9
1.3.2. .......................................................................................... 9
1.3.3. ....................................................................................................10
1.3.3.1. ............................................................... 10
1.3.3.2. ............................................................................ 11
1.3.3.3. ....................................................................... 13
1.3.3.4. .....................................................................13
1.3.3.5. ........................................................................ 14
1.3.3.6. Null .......................................................................................15
1.3.3.7. ......................................................................................15
1.3.3.8. .........................................................................................15
1.3.3.9. ....................................................................................... 15
1.4. : ................................................................. 16
1.4.1. ................................................... 16
1.4.2. .......................................................................17
1.4.3. .............................................................................. 18
1.4.4. ..................................................................................... 19
1.5. ........................................................................................................... 22
1.6. .......................................................................................... 22

Java

Rendered by www.RenderX.com

. 1 26

3.
.
1. ................................................................................................................1
1.1. ....................................................................................................................2
1.2. ..................................................................................................... 2
1.2.1. ............................................................................................................. 3
1.2.2. .......................................................................................................4
1.2.3. ..............................................................................................................8
1.3. ............................................................................................................... 9
1.3.1. ............................................................................................... 9
1.3.2. ................................................................................................ 9
1.3.3. ..........................................................................................................10
1.3.3.1. ..................................................................... 10
1.3.3.2. .................................................................................. 11
1.3.3.3. ............................................................................. 13
1.3.3.4. ...........................................................................13
1.3.3.5. ..............................................................................14
1.3.3.6. Null ............................................................................................ 15
1.3.3.7. ............................................................................................15
1.3.3.8. ...............................................................................................15
1.3.3.9. ............................................................................................. 15
1.4. : .......................................................................16
1.4.1. ......................................................... 16
1.4.2. .............................................................................17
1.4.3. .................................................................................... 18
1.4.4. ........................................................................................... 19
1.5. ................................................................................................................. 22
1.6. ................................................................................................22

1.
, , ,
() .
( tokens ) - "",
Java.
,
, .
.
Java

Rendered by www.RenderX.com

. 2 26

1.1.
Java, ,
, , ASCII (American
Standard Code for Information Interchange,
), ,
(, , .)
.
Unicode.
, Unicode 2 , , ,
65.535 .
. 128 ASCII. , ,
,
Unicode, 65
. Unicode,
ASCII. , 6917,
(1B05) :
\u1B05

u , A, B, C, D, E, F
, , .
Unicode \u0000 \uFFFF.
\u0410 ( \u0401) \u044F ( \u0451).
JDK
SymbolTest,
Unicode. .
native2ascii, JDK.
- Unicode, ASCII, ( -reverse) - Unicode
.
Java 1.1 Unicode 1.1.5, 1.4
3.0. , Java
. JDK Unicode, ,
Character. - ,
- http://www.unicode.org/.
, ASCII,
Unicode . , Unicode
, , ,
: , , .
ASCII .

1.2.
, ,

(white spaces);

(comments);

Java

Rendered by www.RenderX.com

. 3 26

(tokens).

1.2.1.
,
. (space, \u0020, 32),
. ,
, . ,
( ):
double a = 1, b = 1, c = 6;
double D = b * b - 4 * a * c;
if (D >= 0) {
double x1 = (-b + Math.sqrt (D)) / (2 * a);
double x2 = (-b - Math.sqrt (D)) / (2 * a);
}

:
double a=1,b=1,c=6;double D=b*b-4*a*c;if(D>=0){double
x1=(-b+Math.sqrt(D))/(2*a);double x2=(-b-Math.sqrt(D))/(2*a);}

.
, -
.
ASCII - " "
(carriage return, CR, \u000d, 13) (linefeed, LF, \u000a,
10). , Java
.

ASCII- LF, ;

ASCII- CR, " ";

CR, LF.

( ,
),
(. ""),
(
, ).
, Java :

ASCII- SP, space, , \u0020, 32;

ASCII- HT, horizontal tab, , \u0009,


9;

ASCII- FF, form feed, (


), \u000c, 12;

Java

Rendered by www.RenderX.com

. 4 26

1.2.2.

.
Java 2 :

ASCII- // .
, , :
int y=1970; //
ASCII- /* */,
, :
/*

-
*/
for (int i=1; i<10; i++) {
...
}

(
*):
/*
*
* while
*/
while (x > 0) {
...
}

,
:
float s = 2*Math.PI/*getRadius()*/; //

.
Math.PI PI, Math.
getRadius() , s
2 PI. .
,
( ).
:
// /**/ s
String s = "text/*just text*/";
/*
* ,

Java

Rendered by www.RenderX.com

. 5 26

* getRadius()
*/
circle.get/*comment*/Radius();

:
// :
cirle./*comment*/getRadius();
// :
int/*comment*/x=1;

int x
, , .
. /*, */, //
, , . ,

/* /* // /** : */

. (
)
1. /*
2.
comment
3. /*
4.
more comments
5. */
6.
finish
7. */

. 1
/*. /* 3 ,
. */ 5 , 7
- , .
,
, , .
- ,
, - .
,

int x = 2;
int y = 0;
/*
if (x > 0)
y = y + x*2;
else
y = -y - x*4;
*/

Java

Rendered by www.RenderX.com

. 6 26

y = y*y;// + 2*x;

if-else +2*x.
, Unicode,
, .
, - .
.
JDK, 1.0, javadoc.
, HTML ,
, .
,
(, ,
, ). ,

. .
, .
-
/**. :
/**
* .
*
* x.
*/
int getAbs(int x) {
if (x>=0)
return x;
else
return -x;
}

.
(
,
).
HTML-,
HTML. , <b> <p> . ,
<h1> <h6> <hr> ,
javadoc .
*
. , ,
, , .
/**
* - .
* <p>
* :
* <blockquote>
Java

Rendered by www.RenderX.com

. 7 26

* <pre>
* if (condition==true) {
*
x = getWidht();
*
y = x.getHeight();
* }
* </pre></blockquote>
* HTML-:
* <ul>
* <li> <i></i>,
* <li> <b></b>.
* </ul>
*/
public void calculate (int x, int y) {
...
}

HTML-, :
- .
:
if (condition==true) {
x = getWidht();
y = x.getHeight();
}
HTML-:
,
.

, javadoc . @.
. ,
@see, , ,
-.
/**
* .
*
* .
*
* @see java.lang.String
* @see java.lang.Math#PI
* @see <a href="java.sun.com">Official Java site</a>
*/

String (java.lang - ,
), - PI Math ( #
), Java.
, ,
, . /** */ ,
Java

Rendered by www.RenderX.com

. 8 26

, , javadoc. ,
( , , Java).
package.html, ,
. HTML-, <body>
</body> , ,
.
Java ,
, .
javadoc.
,
. , Java javadoc
.

1.2.3.
, ( , .. ,
) ,
. ,
.
, , ,
- ,
.
, :
// .
int x = 3 ;
//
int
x
=
3
;
//
int x = 3 ;
/*
*
* int x
* .
*/
int/**/x=3;

, , .
.

Java

Rendered by www.RenderX.com

. 9 26

1.3.
Java:

(identifiers);

(key words);

(literals);

(separators);

(operators).

1.3.1.
- ,
. , , , , ,
( ).
Unicode, , .
.
. . Java-,
, ASCII- A-Z (\u0041-\u005a),
a-z (\u0061-\u007a), _ (ASCII underscore, \u005f) $
(\u0024). (
), -
, . Java-
ASCII- 0-9 (\u0030-\u0039).
(
, true false null- null). , 2
, (,
A), .
:
Character, a, b, c, D, x1, x2, Math, sqrt, x, y, i, s, PI, getRadius, circle,
getAbs, calculate, condition, getWidth, getHeight, java, lang, String
,

COLOR_RED, _, aVeryLongNameOfTheMethod

1.3.2.
abstract
default
if
private
this
boolean
do
implements
protected
throw
break
double
import
public
throws
byte
else
instanceof
return
transient
case
extends
int
short
try
catch
final
interface
static
void
char
finally
long
strictfp
volatile
class
float
native
super
while

Java

Rendered by www.RenderX.com

. 10 26

const
continue

for
goto

new
package

switch
synchronized

goto const , . ,

. , true, false null- null
( ,
), .
.

1.3.3.
,
, null-. Java :

(integer);

(floating-point);

(boolean);

(character);

(string);

null- (null-literal).

.
1.3.3.1.
,
.
, .
, , , 8 9 .
0x 0X ( 0 ASCII- X
). ,
:
0
00
0x0

, 10-15
A, B, C, D, E, F, . :
0xaBcDeF, 0xCafe, 0xDEC

,
int long 4 8 ( 32 64
). , .. int -231 231-1,
-2.147.483.648 2.147.483.647. int,
, 0 2147483648,
. 2147483648
-:

Java

Rendered by www.RenderX.com

. 11 26

int x = -2147483648; \\
int y = 5-2147483648; \\

, 00
017777777777 (=231-1), - -020000000000 (= -231).
- 0x0 0x7fffffff (= 231-1), 0x80000000 (= -231).
long 64 , , -263 263-1.
, L l,
long.
:
9223372036854775807L
0777777777777777777777L
0x7fffffffffffffffL
// :
-9223372036854775808L
-01000000000000000000000L
-0x8000000000000000L

long:
0L, 123l, 0xC0B0L

1.3.3.2.
.
, .
:
3.14
2.
.5
7e10
3.1E-20

, :

( ASCII- );

( ASCII- E
+ -);

- .

, (
L l long) 2
Java

Rendered by www.RenderX.com

. 12 26

- ASCII- D ( double) F ( float) .


.
:

. , ""
, , :
1.
.1
1e1
1f

Java , ,- float double. - 4 8


32 64 . float,
F .
double D d, double
( ).
// float-:
1f, 3.14F, 0f, 1e+5F
// double-:
0., 3.14d, 1e-4, 31.34E45D

Java 32- float 64- double


, IEEE 754 (
- IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New
York)). ,
, :

(positive/negative infinity);

" ", Not-a-Number, NaN;

.
(,
0.0/0.0 NaN), Float Double,
POSITIVE_INFINITY, NEGATIVE_INFINITY NaN.
.
,
. :

float: 3.40282347e+38f

double: 1.79769313486231570e+308

Java

Rendered by www.RenderX.com

. 13 26

, :

float:1.40239846e-45f

double: 4.94065645841246544e-324

(, 1e40F)
. .
, (, 1e350) . .
, :
// , 0
0.0000000000000000000000000000000000000000000001f
// , 1
1.0000000000000000000000000000000000000000000001f

Java
, Float Double ,
.
1.3.3.3.
truefalse
1.3.3.4.
'a' //
' ' //
'K' //

(.
""). :
'\u0041' // A
'\u0410' //
'\u0391' // A

,
, \.
( , ", ', \) :
\b
\t
\n
\f
\r
\"
\'
\\

\u0008
\u0009
\u000a
\u000c
\u000d
\u0022
\u0027
\u005c

backspace BS -
horizontal tab HT -
linefeed LF -
form feed FF -
carriage return CR -
double quote " -
single quote ' -
backslash \ -

Java

Rendered by www.RenderX.com

. 14 26

\ \u0000 to \u00ff .

,
Java-. Unicode-.
. \
.

. ,
'\101' // '\u0041'

\u0000 \u00ff (.. 0


255), Unicode- .
Unicode- (\uhhhh)
, :
'\u000a' //

\u000a ,
, .
:
'\n' //

\u000d ( )
\r.
,
.
1.3.3.5.
.
.
, \ (. "
").
"" //
"\"" //, "
" " // 13

.
, ,
\n / \r. ,
, +.
:
// -, 2
" " +
" "

Java

Rendered by www.RenderX.com

. 15 26

/*
* , :
* Hello, world!
* Hello!
*/
"Hello, world!\r\nHello!"

, ,
\u000a \u000d.
String.
,
.
1.3.3.6. Null
Null : null. ,
, . ,
.
.
1.3.3.7.
( ) [ ] { } ; . ,

1.3.3.8.
- , , ,
, . 37 ( ASCII) Java:
= > < ! ~ ? :
== <= >= != && ||
+ - * / & | ^ %
+= -= *= /= &= |=

++
<<
^=

->>
%=

>>>
<<=

>>=

>>>=


, Java
, .
1.3.3.9.
, Java-.
, Unicode
:

(, , , null-,
) ASCII-.
Unicode-
Java

Rendered by www.RenderX.com

. 16 26

ASCII- ( - \uhhhh, hhhh -


).
( Hello, world!),
:
public class Demo {
/**
* ,
* Java .
*/
public static void main (String args[]) {
System.out.println("Hello, world!");
}
}

, , 7 ,
5 , 1 , 13 .
Demo.java, ( JDK
). ,
:
Hello, world!

1.4. :
Java.
, .
,
(, 1/2 0, 1/2. - 0.5).

1.4.1.
-, , = ==.
x = 1; // x 1
x == 1 // x

.
true false.
, :
//
if (x=0) { // ==
...
}

Java . x=0 (
),
( x==0).
Java

Rendered by www.RenderX.com

. 17 26

" " !=. :


if (x!=0) {
float f = 1./x;
}

- = (.
"") ,
, :
x = x + 1;
x += 1;

1.4.2.
4 +, -, *, /,
%, ,
.
.
a b, (a/b)*b+(a%b) a.
, , / . :
9/5 1
9/(-5) -1
(-9)/5 -1
(-9)/(-5) 1

, .
,
.
9%5 4
9%(-5) 4
(-9)%5 -4
(-9)%(-5) -4

0 .

. ,
%. 9 5 ,
( , ).
9.0%5.0 4.0
9.0%(-5.0) 4.0
(-9.0)%5.0 -4.0
(-9.0)%(-5.0) -4.0

Java

Rendered by www.RenderX.com

. 18 26

IEEE 754 .
Math.IEEEremainder(double f1, double f2). , f1-f2*n, n - , f1/f2,
2 , .
:
Math.IEEEremainder(9.0, 5.0) -1.0
Math.IEEEremainder(9.0, -5.0) -1.0
Math.IEEEremainder(-9.0, 5.0) 1.0
Math.IEEEremainder(-9.0, -5.0) 1.0

++ --, ,
, .
int x=1;
int y=++x;

++ x, ,
, x
y. x y 2.
int x=1;
int y=x++;

x y,
. x 2, y
1.

1.4.3.
"" "" ( & |) .
, , , ,
.
, -
, ,
.
(&, |) , - ( &&, || )
, . :
int x=1;
(x>0) | calculate(x) // calculate
(x>0) || calculate(x) // -

"" !,
. .
int x=1;
x>0 //
!(x>0) //

Java

Rendered by www.RenderX.com

. 19 26

? : - .
( ),
, -
. , x:
x>0 ? x : -x

1.4.4.
,
. ,
:
0 0
1 1
2 10
3 11
4 100
5 101

. ? -,
. , 0
, 1 - . ,
. , 8- :
-1 10000001 // !
-2 10000010 // !
-3 10000011 // !

! , - 00000000
100000000, .
. -1, 0 1:
00000000
-00000001
--------11111111

, -1 11111111.
( 1):
0
-1
-2
-3

00000000
11111111
11111110
11111101

10000000,
. 8-
01111111 (=127), 10000000 (=-128). 8
Java

Rendered by www.RenderX.com

. 20 26

28=256 , , ,

, .
, "", "", " " 2 ,
.
, , , ,
() . , 5&6 :
00000101 // 5
&00000110 // 6
--------00000100 // ""

, 5&6 4.
"" "NOT",
~ ( !).
. , ~(-1)= 0.
:
n - , -n ~(n-1).
, . Java
.
.
<<
, .
2n, ,
.
int, .. 32-
. 20, -21.
// 20
20 << 00 = 00000000000000000000000000010100
20 << 01 = 00000000000000000000000000101000
20 << 02 = 00000000000000000000000001010000
20 << 03 = 00000000000000000000000010100000
20 << 04 = 00000000000000000000000101000000
...
20 << 24 = 00010100000000000000000000000000
20 << 25 = 00101000000000000000000000000000
20 << 26 = 01010000000000000000000000000000
20 << 27 = 10100000000000000000000000000000
20 << 28 = 01000000000000000000000000000000
20 << 29 = 10000000000000000000000000000000
20 << 30 = 00000000000000000000000000000000
20 << 31 = 00000000000000000000000000000000

Java

Rendered by www.RenderX.com

=
=
=
=
=

20
40
80
160
320

=
=
=
=
=
=
=
=

335544320
671088640
1342177280
-1610612736
1073741824
-2147483648
0
0

//
-21
-21
-21
-21
-21
-21
...
-21
-21
-21
-21
-21
-21
-21
-21

. 21 26

<< 00
<< 01
<< 02
<< 03
<< 04
<< 05

-21
= 11111111111111111111111111101011
= 11111111111111111111111111010110
= 11111111111111111111111110101100
= 11111111111111111111111101011000
= 11111111111111111111111010110000
= 11111111111111111111110101100000

=
=
=
=
=
=

-21
-42
-84
-168
-336
-672

<<
<<
<<
<<
<<
<<
<<
<<

=
=
=
=
=
=
=
=

=
=
=
=
=
=
=
=

-352321536
-704643072
-1409286144
1476395008
-1342177280
1610612736
-1073741824
-2147483648

24
25
26
27
28
29
30
31

11101011000000000000000000000000
11010110000000000000000000000000
10101100000000000000000000000000
01011000000000000000000000000000
10110000000000000000000000000000
01100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000

, ,
.
,
, . -
, . .
>> ,
, . >>>
, .
// 20
//
20
20
20
20
20
20

>>
>> 00 = 00000000000000000000000000010100
>> 01 = 00000000000000000000000000001010
>> 02 = 00000000000000000000000000000101
>> 03 = 00000000000000000000000000000010
>> 04 = 00000000000000000000000000000001
>> 05 = 00000000000000000000000000000000

//
20
20
20
20
20
20

>>> 00 =
>>> 01 =
>>> 02 =
>>> 03 =
>>> 04 =
>>> 05 =

=
=
=
=
=
=

>>>
00000000000000000000000000010100
00000000000000000000000000001010
00000000000000000000000000000101
00000000000000000000000000000010
00000000000000000000000000000001
00000000000000000000000000000000

20
10
5
2
1
0

=
=
=
=
=
=

20
10
5
2
1
0

, >> >>>
.
.
// -21

Java

Rendered by www.RenderX.com

. 22 26

//
-21 >> 00 =
-21 >> 01 =
-21 >> 02 =
-21 >> 03 =
-21 >> 04 =
-21 >> 05 =

>>
11111111111111111111111111101011
11111111111111111111111111110101
11111111111111111111111111111010
11111111111111111111111111111101
11111111111111111111111111111110
11111111111111111111111111111111

=
=
=
=
=
=

// >>>
-21 >>> 00 = 11111111111111111111111111101011
-21 >>> 01 = 01111111111111111111111111110101
-21 >>> 02 = 00111111111111111111111111111010
-21 >>> 03 = 00011111111111111111111111111101
-21 >>> 04 = 00001111111111111111111111111110
-21 >>> 05 = 00000111111111111111111111111111
...
-21 >>> 24 = 00000000000000000000000011111111
-21 >>> 25 = 00000000000000000000000001111111
-21 >>> 26 = 00000000000000000000000000111111
-21 >>> 27 = 00000000000000000000000000011111
-21 >>> 28 = 00000000000000000000000000001111
-21 >>> 29 = 00000000000000000000000000000111
-21 >>> 30 = 00000000000000000000000000000011
-21 >>> 31 = 00000000000000000000000000000001

-21
-11
-6
-3
-2
-1

=
=
=
=
=
=

-21
2147483637
1073741818
536870909
268435454
134217727

=
=
=
=
=
=
=
=

255
127
63
31
15
7
3
1

, 2n. ,
n 0,
-1.

1.5.
Java.
Unicode,
.
Unicode ASCII-.
( ,
) ,
( , ).
.
, .
.

1.6.
3-1.

Java- 514?
a.) 514
b.) \u0546

Java

Rendered by www.RenderX.com

. 23 26

c.) \u222
d.) \u0222
a) ,
, , .
\u, 4
, c) . b)
0x0546=1350.
3-2.

:
int x = 3; int y=1;
int z = x+y;

a.) 7
b.) 8
c.) 9
d.) 11
9: 5 , 1 , 3 .
3-3.

:
int x = 0; /* text // text /*
int y=1; // text */ // text */

a.) 1 , 0
b.) 1 , 1
c.) 1 , 2
d.) 2 , 3
b).
x, */
text. .
3-4.

?
a.) , ,
/**. javadoc
HTML,
, , , , .
HTML-,
, <hr> <h1>..<h6>,
javadoc. , ,
@, .

Java

Rendered by www.RenderX.com

. 24 26

3-5.

?
a.) abc
b.)

1ab

c.)

_bc

d.) _1c $ac


e.)

$ac

f.)

for_

g.)

Int

h.)

byte

1ab , . byte
, byte.
. ($ac)
. Int ,
int .
3-6.

:
a.) true
b.) goto
c.) null
d.) const
e.) false
goto const , .
,
, Java.
true, false, null , .

3-7.

:
a.) 5 05
b.) 9 09
c.) 10 010
d.) 0x5A 90L

Java

Rendered by www.RenderX.com

. 25 26

5 05 . 09 , . 010
8 10. 0x5A 90, 90L.
3-8.

?
1/0 1./0 1/0. 1./0.
\n+\r

a.) 1/0 . 3
POSITIVE_INFINITY.
23.
3-9.

x
?
int x=0;
print(++x==x++);

a.) . 1, x
1 .
1, x
. 2.
3-10. x y
?
int x=0, y=0;
print((++x==1) || (y++==1));

a.) , .
,
. x 1, y 0.

Java

Rendered by www.RenderX.com

Java ()

Java

Rendered by www.RenderX.com

Оценить