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

Java

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

. , Java
,
.
, Java.
,
, Class, Java.

Rendered by www.RenderX.com

ii

4. ..................................................................................................... 1
1. .......................................................................................................................... 1
2. ...................................................................................................................2
3. ....................................................................3
3.1. ............................................................................................... 5
3.2. ............................................................................................ 5
4. .............................................................................................................. 12
5. .............................................................................................................. 17
6. ..........................................................................................................17
6.1. ................................................................... 17
6.2. Object ......................................................................................................... 22
6.3. String ......................................................................................................... 24
6.4. Class ..........................................................................................................25
7. .................................................................................................................. 26
8. .................................................................................................................... 27
9. ...................................................................................................27

Java

Rendered by www.RenderX.com

. 1 31

4.
.
1. ......................................................................................................................... 1
2. .................................................................................................................. 2
3. ................................................................... 3
4. ............................................................................................................. 12
5. ............................................................................................................. 17
6. ......................................................................................................... 17
7. ................................................................................................................. 26
8. ................................................................................................................... 27
9. .................................................................................................. 27

1.
Java . ,
.
. , ,
, "" (
bugs) (
), , .
Java
.
. 8
( primitive) . :

byte

short

int

long

char ( )

float
Java

Rendered by www.RenderX.com

. 2 31

double

boolean

( reference)
. , .
Java , .
, ,
Java .
.

2.
.
:

.
, . - ,
.
(declaration). ,
. , Java
, .
( ),
.
int
:
int
int
int
int

a;
b = 0, c = 3+2;
d = b+c;
e = a = 5;

, ,
.
( 3+2), .
(, b+c).
,
( ), .
:
. ,
, , , ,
. ,
Java

Rendered by www.RenderX.com

. 3 31

.
.
,
.
, , ,
.
, final.
,
. , final-
- , , ,
, .
final-:
final double pi=3.1415;

3.

. , 2
, , .
?
int:
int a=5; //
int b=a; // ,
a=3; //
print(b); //

, print(...) ( ,
) ( ,
System.out.println(...),
).
, b ,
5. , ,
.
:
byte b=3;
int a=b;

(
). ,
b 3 byte, a - 3 int.
, .
.
. ,
Java

Rendered by www.RenderX.com

. 4 31

. -
, :
class Point {
int x, y;
}

, int-, ,
new Point(3,5) - (3,5).
Point p1 = new Point(3,5);
Point p2=p1;
p1.x=7;
print(p2.x);

,
p1, , ,
p2. , ,
.
"" , .
,
. , .
,
, .
,
.
, ,
:
Point p1 = new Point(3,5);
Point p2=p1;
p1 = new Point(7,9);
print(p2.x);

3, p1 p2
.
null.
. , ,
. ,
(, ) .
null
( ).
Java
, , .
,
.
, .. ,
Java

Rendered by www.RenderX.com

. 5 31

.
- - .
"",
. .
garbage collector -
, .
, , ,
, . ,
- , ,
.
.
.

3.1.
, 8 ,
(integer), (floating-point) (boolean).

3.2.
- byte, short, int, long, char.
1, 2, 4 8 , char - 2 ,
, Java Unicode.
. , Java
, ,
.
4 . char ,
JVM - . ,
, char - .
, .
char - , , ,
, ..
:

()

byte

-128 .. 127

short

-32.768 .. 32.767

int

-2.147.483.648 .. 2.147.483.647

long

-9.223.372.036.854.775.808 ..
9.223.372.036.854.775.807 (
1019)

char

'\u0000' .. '\uffff', 0 .. 65.535

, int 2 ,
, .
long, , Java .
, 1 1970 . ,

Java

Rendered by www.RenderX.com

. 6 31

long (!), ,
.
, int long? ,
int long, L
l. ,
4 8 . .
, :
1
-2147483648
2147483648L
0L
111111111111111111L

( )
-

<, <=, >, >=

==, !=

( )
-

+ -

+, -, *, /, %

( ): ++ --

<< , >>, >>>

~, & , |, ^

? :

.
(true false).
, .
+ -,
( ), ( ).
.
+ , (+x x). -,
x, , 0-x.
,
.
int x=-2147483648; // int
int y=-x;

Java

Rendered by www.RenderX.com

. 7 31

y 2147483648,
int, x!
-x==x !
,
, , Java
. ,
, .
, ,
.
int x= 300000;
print(x*x);

:
-194313216

-2147483648, ,
+231 , , 10.0 ( 31 ).
int ,
-2147483648.
, ,
, (. "").
, ""
.
, -5 .
5 -. ,
(, 2147483648) -.
, Java .
8, 16, 32 64 ,
32- 64- . ,
.
long,
, long.
64 , , ,
.
long , 32 ,
int ( byte, short, char).
int. 32- .
, , .
:
int i=300000;
print(i*i); // 32
long m=i;
print(m*m); // 64

Java

Rendered by www.RenderX.com

. 8 31

print(1/(m-i)); // int long

:
-194313216
90000000000

, i m
, , .
32 , .
- 64 , .
, ,
.
, ,
. -, ,
.
double x = 1/2;

x 0, 0.5, .
,
0.5 1./2 ( ,
).
, Java . ,
:
print(1000*60*60*24*7); //
print(1000*60*60*24*30); //

(1000), -
(60), - (60), - (24), (7 30
). :
604800000
-1702967296

, .
long:
print(1000*60*60*24*30L); //

:
2592000000

64-
, , .

Java

Rendered by www.RenderX.com

. 9 31

, , Java
. ,
:
//
int x=1;
byte b=x;

, b 1,
byte, x
, , int.
:
//
byte b=1;
byte c=b+1;

.
b int, ,
, byte.
:
//
int x=2;
long y=3;
int z=x+y;

long.
:
//
byte b=5;
byte c=-5;

- 32 .
,
,
:
byte b=1;
byte c=(byte)-b;

, int long.
.
-, .
,
,
Java

Rendered by www.RenderX.com

. 10 31

( ,
). , . (
32 ,
.)
byte x=5;
byte y1=x++;
byte y2=x--;
byte y3=++x;
byte y4=--x;
print(y1);
print(y2);
print(y3);
print(y4);

//
//
//
//

x
x
x
x

5
6
5
6

:
5
6
6
5

++ -- byte.
, :
byte x=-128;
print(-x);
byte y=127;
print(++y);

:
128
-128


.
? :.
, .
byte
byte
byte
byte

x=2;
y=3;
z=(x>y) ? x : y; // , x y
abs=(x>0) ? x : -x; // !

Java

Rendered by www.RenderX.com

. 11 31

, ,
, int, int, .
byte, - short, int.
, . +
. ,
- , , .
int x=1;
print("x="+x);

:
x=1

:
print(1+2+"text");
print("text"+1+2);

:
3text
text12

char.
:
char c1=10;
char c2='A'; // A (\u0041, 65)
int i=c1+c2-'B';

i 9.
:
char c='A';
print(c);
print(c+1);
print("c="+c);
print('c'+'='+);

:
A
66
c=A
225

print char,
. , ,
Java

Rendered by www.RenderX.com

. 12 31

. char
. : 'c' ( 99),
'=' ( 61) c (.. 'A' - 65).
-
(wrapper classes). byte, short, int, long, char Byte, Short, Integer, Long, Character.
.
, . , Math,
,
.
, ,
Java - ( / %).

4.
- float double. - 4 8 . .
:

()

float

3.40282347e+38f ; 1.40239846e-45f

double

1.79769313486231570e+308 ;
4.94065645841246544e-324

,
. , -
. ,
, , overflow.
, ,
underflow.
//
float f = 1e40f; // , overflow
double d = 1e-350; // , underflow

, F f,
float. double,
D d.
:

( )
-

<, <=, >, >=

==, !=

( )
-

+ -

+, -, *, /, %

Java

Rendered by www.RenderX.com

. 13 31

( ): ++ --

? :

,
( % ,
++ -- ).
,
. ,
.
- overflow underflow.
, Java .
, . ,
. ,
.
.
IEEE 754 "":

(positive/negative infinity);

" ", Not-a-Number, NaN;

float, double.
:
1f/0f // , float
-1d/0d // , double

Float Double POSITIVE_INFINITY NEGATIVE_INFINITY. ,


.
NaN , , :
0.0/0.0 //
(1.0/0.0)*0.0 //

NaN Float Double.


:
0.0 //
+0.0 // +, -
-0.0 // -, -

Java

Rendered by www.RenderX.com

. 14 31

.
, - . +0.0 -0.0
, 0.0==-0.0 , 0.0>-0.0 - .
, , 1.0/0.0 , 1.0/0.0 - .
- NaN.
NaN, false ( !=
true). , x,
x!=x , NaN.
.
(overflow),
.
print(1e20f*1e20f);
print(-1e200*1e200);

:
Infinity
-Infinity

, , (underflow),
. ,
:
print(1e-40f/1e10f); // underflow float
print(-1e-300/1e100); // underflow double
float f=1e-6f;
print(f);
f+=0.002f;
print(f);
f+=3;
print(f);
f+=4000;
print(f);

:
0.0
-0.0
1.0E-6
0.002001
3.002001
4003.002

, 6- .
Java

Rendered by www.RenderX.com

. 15 31

( Java):
double d = 1e-305 * Math.PI;
print(d);
for (int i = 0; i < 4; i++)
print(d /= 100000);

:
3.141592653589793E-305
3.1415926535898E-310
3.141592653E-315
3.142E-320
0.0

, , ,
(overflow) (underflow) ,
(. "").
, .
. double,
,
double. 64 .
double , float,
float, 32 ,
float.
, .
NaN, NaN.
:
print(1/2);
print(1/2.);

:
0
0.5

,
.
:
int x=3;
int y=5;
print (x/y);

Java

Rendered by www.RenderX.com

. 16 31

print((double)x/y);
print(1.0*x/y);

:
0
0.6
0.6

, . ,
,
, .
( ),
( ).
,
.
-, ,
. , 3.84 3, -3.84
-3.
Math.round().
-, int float long
float double , ,
, .
:
long l=111111111111L;
float f = l;
l = (long) f;
print(l);

:
111111110656

float , long float


.
-
(wrapper classes). float double Float Double.
. ,
.
, Math
, , ,
, . PI
E.

Java

Rendered by www.RenderX.com

. 17 31

5.
boolean,
- true false.
.
:

( )
-

==, !=

( )
-

&, |, ^

&&, ||

? :

&& || .
? : boolean. ,
.
"true"
"false" .
, ,
if.
.
, ( ),
x!= 0. boolean
ref!=null.

6.
, null, ,
JVM.

6.1.
(object) - .
. -
, ,
. ,
.
new,
new ( , ).
, .
Java

Rendered by www.RenderX.com

. 18 31

(
), ,
, . ,
Point, new Point(3,5)
Point, 2 int. ,
.
,
Point:
class Point {
int x, y;
/**
* 2 ,
* .
*/
Point (int newx, int newy){
x=newx;
y=newy;
}
}

, new
. , . JVM
. , ,
(garbage collector).
"" .
Point p=new Point(1,2); // ,
Point p1=p; // 2 (1,2)
p=new Point(3,4); // (1,2)
p1=null;

- (1,2) ,
.
new.
- String. ,
. +,
,
new.
:
"abc"+"def"

Java

Rendered by www.RenderX.com

. 19 31

3 String.
,
.
- Java.
.
, , , .
,
, ,
.
Java 1.1 reflection,
, , .
new,
, , , , ,
. ,
:
Point p = null;
try {
// ,
// Point,
// new
p = (Point)Class.forName("Point").newInstance();
} catch (Exception e) { //
System.out.println(e);
}

"", . ,
, , .
, . , Parent
Child:
// Parent
class Parent {
}
// Child,
// Parent
class Child extends Parent {
}

- .
, :
Parent p = new Child();

Parent , Child.
Java

Rendered by www.RenderX.com

. 20 31

instanceof ( )

== != ( )

? :


. . ().
.
instanceof, , .
. , - ,
. :
Parent p = new Child();
// p Parent
// Child
print(p instanceof Child);

true. , instanceof ,
, .
, .
:
//
// Child
class ChildOfChild extends Child {
}

:
Parent p = new ChildOfChild();
print(p instanceof Child);

Parent,
, ChildOfChild. instanceof
Parent Child,
, . , true,
, , Child.

Java

Rendered by www.RenderX.com

. 21 31

:
class Child2 extends Parent {
}

Parent:
Parent p=new Child();
print(p instanceof Child);
print(p instanceof Child2);

p Parent, , Child
Child2. instanceof :
true
false

null instanceof false.


Java,
instanceof.
= != ( )
. - .
.
, Java,
. ,
, true ,
.
Point p1=new Point(2,3);
Point p2=p1;
Point p3=new Point(2,3);
print(p1==p2);
print(p1==p3);

:
true
false

, p2
, p1. , , p3
- . ,
new.
= null, - ,
false. null, true.
equals,
. , :
Java

Rendered by www.RenderX.com

. 22 31

String s = "abc";
s=s+1;
print(s.equals("abc1"));

? : ,
, .
.
, .
null, "null". ,
( , toString()),
, , .

6.2. Object
Java .
. , ,
. , ,
Object. ,
. ,
Object. ,
( Object ),
.
:

getClass()

Class, ,
. Class .
getName()
, :
String s = "abc";
Class cl=s.getClass();
print(cl.getName());

:
java.lang.String

instanceof, getClass() ,
.

equals()

Object boolean. ,
equals() , .
, , .
Point p1=new Point(2,3);
Java

Rendered by www.RenderX.com

. 23 31

Point p2=new Point(2,3);


print(p1.equals(p2));

true.
Object , , , equals
.
(
). Point
:
public boolean equals(Object o) {
// ,
// Point
if (o instanceof Point) {
// ,
Point p = (Point)o;
//
return p.x==x && p.y==y;
}
// Point, false
return false;
}

hashCode()

int. hashCode() -
. - ( Java
, ). ,
, -, ,
, , ( equals()
true) -.
Object JVM.
-, .

toString()

.
, .
Object , toString(),
:
getClass().getName()+"@"+hashCode()

getName() Class , -
.

Java

Rendered by www.RenderX.com

. 24 31

:
print(new Object());

:
java.lang.Object@92d342

,
, , -, ,
.
,
.

finalize()


(garbage collector). Object , -
, ,
, , ..
, .
, .
finalize() ,
. , JVM
.

6.3. String
, String Java .
new.
String, ( null),
.
,
, , ,
.
. , ,
, -,
, .
String s="a";
s="b";

,
String.
,
Java,
.

Java

Rendered by www.RenderX.com

. 25 31

-, ,
.
String s1 = "abc";
String s2 = "abc";
String s3 = "a"+"bc";
print(s1==s2);
print(s1==s3);

:
true
true

, , ,
, .
,
, :
String s1="abc";
String s2="ab";
print(s1==(s2+"c"));

false,
.
String intern(), -,
, . , s1 s2
s1.equals(s2), s1.intern()==s2.intern().
, equals() hashCode(). toString()
, -, s
String, null, s==s.toString().

6.4. Class
, , .
Class Java. JVM
.class, , Class,
.
, :
Point p=new Point(1,2);

, :
1. , Point, (1,2)
2. Class, Point

Java

Rendered by www.RenderX.com

. 26 31

3. Class, Object. Point


Object, .
4. Class, Class. Java-,
.
Class - getClass()
Object. :
Class cl=p.getClass(); // 2
Class cl2=cl.getClass(); // 4
Class cl3=cl2.getClass(); // 4

cl2==cl3 .
Class
reflection.
, Java
, (
, ..).

7.
- . ,
. , :

- ;

- , ;

- ;

instanceof;

- , import- ..


. Java
- ,
.
.
Java 1.5 (templates),
, .

Java

Rendered by www.RenderX.com

. 27 31

8.
, Java ,
.
,
.
Java , ,
(8 ) ().
.
.
null, . JVM
,
.
.
, .
. final.
5 , , 2 1
. , .
,
.
, , ,
, instanceof.
Java Object, Class, String.
.

9.
4-1.

?
byte b=3;
int c=b;
c++;
print(++b==c);

a.) 3.
,
,
. true.
4-2.

?
Point p = new Point(1, 2);
int a=p.x;
p = new Point(3, 4);
print(a);

Java

Rendered by www.RenderX.com

. 28 31

a.)
. 1.
4-3.

?
Point p1 = new Point(3, 4);
Point p2 = p1;
p1.x=5;
p1 = new Point(4, 4);
print(p2.x-p1.x);

a.) ,
x 5. p1
, x 4.
5-4=1.
4-4.

?
1/2 1./2 1/2. 1./2.

a.) ,
, .. 0.
, , 0.5.
4-5.

,
+.
a.) , , (
) .
(+=).

4-6.

x,
:
a) x*30/30==x
b) x/30*30==x

a.) a) , Integer.MAX_VALUE/30, .. 71582789. b)


, 30, , 15.
4-7.

(
33)?
print(Hello+!);

a.) Hello!
4-8.

Java

Rendered by www.RenderX.com

. 29 31

double x=2./0;
double y=-1/0.;
print(x+y);

a.) x POSITIVE_INFINITY, y
NEGATIVE_INFINITY. NaN.
4-9.

Java.
a.) new,
,
.
String .
, , newInstance()
Class.

4-10. ?
Point p1 = new Point(2, 3);
Point p2 = new Point(2, 3);
print(p1==p2);
p2=p1;
p1.x=3;
print(p1==p2);

a.) false.
, p1 p2
.
,
true.
4-11. x1 x2
(SomeClass2 x2)?
x1 instanceof SomeClass2
x1.getClass().getName().equals(x2.getClass().getName())

a.) . ( ) ,
,
x1 x2. instanceof
. s ,
, , . , x1
, - SomeClass2, x2 ,
SomeClass2, instanceof true,
false.
4-12. false?

Java

Rendered by www.RenderX.com

. 30 31

a) x.toString() instanceof String


b) (x+) instanceof String

a.) a) , x
, toString() ,
null.
. b) true.
4-13. x
?
x.getClass()==x

a.) , x
Class, Class.
y, null:
x=y.getClass().getClass()

Java

Rendered by www.RenderX.com