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

Java

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

. Java
, ,
.
.
, Java
,
.
, ,
.
, .
.

Rendered by www.RenderX.com

ii

7. ..................................................................................... 1
1. ........................................................................................................................ 1
2. .......................................................................................................... 2
2.1. .........................................................................3
2.2. ( ) ....................... 3
2.3. ( ) ........................... 7
2.4. .....................................................................................9
2.5. .......................................................................... 10
3. ............................................................................................ 10
3.1. ..........................................................................................11
3.2. ......................................................................................................12
3.3. .............................................................................................. 14
3.4. ............................................................................ 15
3.5. ........................................................................................15
3.5.1. ................................................................. 15
3.5.2. ..............................................................16
4. ............................................................................16
5. .................................................................................................................... 18
6. ...................................................................................................18

Java

Rendered by www.RenderX.com

. 1 22

7.
.
1. ....................................................................................................................... 1
2. ..........................................................................................................2
2.1. ...............................................................................3
2.2. ( ) ............................. 3
2.3. ( ) ................................. 7
2.4. ...........................................................................................9
2.5. ................................................................................ 10

3. ............................................................................................10
3.1. ............................................................................................... 11
3.2. ............................................................................................................12
3.3. ....................................................................................................14
3.4. .................................................................................. 15
3.5. ............................................................................................. 15
3.5.1. .......................................................................15
3.5.2. ....................................................................16

4. ........................................................................... 16
5. ................................................................................................................... 18
6. .................................................................................................. 18

1.
, Java , ,

.
, , .
:
long l=3;
l = 5+'A'+l;
print("l="+Math.round(l/2F));

Java

Rendered by www.RenderX.com

. 2 22

, ,
(conversion) :

3 , int.
long .

int char.
, 32
. ,
l 64 .

, long
float, - .
Math.round, ,
int.
, . ,
-
- ( int Integer),
toString(), .

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

,
byte int.
, .
, .

2.
Java :

(identity);

(widening primitive);

(narrowing primitive);

(widening reference);

(narrowing reference);

(String);

(forbidden).

.
Java

Rendered by www.RenderX.com

. 3 22

2.1.
. Java
.
? ,
.
-, , Java
, . ,
boolean , .
. Java
, :
print(getCity().getStreet().getHouse().getFlat().getRoom());

getCity().
, City.
getStreet(). ,
, City.
(getHouse()), .
, .
,
, .
,
. , :
print((MyFlatImpl)(getCity().getStreet().getHouse().getFlat()));

2.2. ( )
,
.

? . ,
. , byte
( 1 ) int ( 4 ). ,
,
, .
, :
byte b=3;
int i=b;

Java

Rendered by www.RenderX.com

. 4 22

b byte
i ( , int) ,
.
19 :

byte short, int, long, float, double

short int, long, float, double

char int, long, float, double

int long, float, double

long float, double

float double

, char
(byte, short) , , short char .
, char .
, , -
. ,
int float long float double.
, ,
.
:
long l=111111111111L;
float f = l;
l = (long) f;
print(l);

:
111111110656

- - ,
. .
, int 127, byte
. Java
, .. ,
.
23 :

byte char

short byte, char

char byte, short

int byte, short, char

long byte, short, char, int

Java

Rendered by www.RenderX.com

( )

float byte, short, char, int, long

double byte, short, char, int, long, float

. 5 22

,
, .
:
print((byte)383);
print((byte)384);
print((byte)-384);

:
127
-128
-128

, ,
- (384 -384) .
, ,
.
char:
char c=40000;
print((short)c);

:
-25536

.
.
long,
long, int ( byte, short, char int).
,
.
, 3.84 3, -3.84 -3.
:

NaN, 0
(.. int long);


,
(.. int long);

, ,
(.. int
long), , ,

Java

Rendered by www.RenderX.com

. 6 22

.
,
.

, ,
int byte, short char.

:
float fmin = Float.NEGATIVE_INFINITY;
float fmax = Float.POSITIVE_INFINITY;
print("long: " + (long)fmin + ".." + (long)fmax);
print("int: " + (int)fmin + ".." + (int)fmax);
print("short: " + (short)fmin + ".." + (short)fmax);
print("char: " + (int)(char)fmin + ".." + (int)(char)fmax);
print("byte: " + (byte)fmin + ".." + (byte)fmax);

:
long: -9223372036854775808..9223372036854775807
int: -2147483648..2147483647
short: 0..-1
char: 0..65535
byte: 0..-1

long int -
.
(short, char, byte) ,
int, . ,
, . ,
1000..000 ( 32 int,
31 ). - 1111..111 (32 ).
0, 3
. ,
1. byte short -1,
char - , 65.535.
, char . ,
-
. 2 :
float f=2e9f;
print((int)(char)f);
print((int)(char)-f);

Java

Rendered by www.RenderX.com

( )

. 7 22

:
37888
27648

char
. , char int ,
, , .
, boolean
.

2.3. ( )
.
.
:
// Parent
class Parent {
int x;
}
// Child,
// Parent
class Child extends Parent {
int y;
}
//
// Parent - Child2
class Child2 extends Parent {
int z;
}

.
, .
3 3 . Parent
x, Parent
. Child y x,
Parent. , Child
Parent. :
Parent p = new Child();

, p x
. y , ,
p.y, , p Child
. ,
Java

Rendered by www.RenderX.com

. 8 22

Parent, y,
.
, Child2 z x,
Parent. , Child2
Parent.
, Parent
, Child Child2 -
.
.
, ..
. (Child,
Child2) (Parent) , .
, JVM
, ,
: , ,
.
Parent p1=new Child();
Parent p2=new Child2();

Parent , ,
. ,
.
,
. , , , y Child ,
, .
. Child, .
, .
.
,
, , , .
:

A B, A B (
Object);

null- .

:
Parent p=null;

null - ,
null-. ,
.
( )
.

Java

Rendered by www.RenderX.com

. 9 22

, , ,
. ,
Parent, , Child,
, , .
. Parent
Parent Child2, Child ,
y, Child.
,
. JVM .
, . - .
Parent p=new Child();
Child c=(Child)p; // .
Parent p2=new Child2();
Child c2=(Child)p2; // !

, ,
instanceof:
Parent p=new Child();
if (p instanceof Child) {
Child c = (Child)p;
}
Parent p2=new Child2();
if (p2 instanceof Child) {
Child c = (Child)p2;
}
Parent p3=new Parent();
if (p3 instanceof Child) {
Child c = (Child)p3;
}

. ,
. if ,
.
:

A B, B A (
Object );

( )
.

2.4.
. ,
.. String. ,
, boolean, ,
, .

Java

Rendered by www.RenderX.com

. 10 22

, :

.
, .
-,
toString().
, JVM
.

"true" "false" .

toString(). null,
"null".

null- "null".

2.5.
. ,
: ,
- ( ).
- boolean , boolean (
- ). , ,
, . ,
, Child
Child2 . , Child ,
Child .
, Child2.
. ,
, ,
.
.
,
.

3.
, , ,
.
:

(assignment).
- ,
.

.
. , .
.
.

Java

Rendered by www.RenderX.com

. 11 22

. ,
.
. .

(numeric promotion).
(). -
(promotion), ,
.

3.1.

. ,
. .
, .
, ,
:
//
//
//
Parent p = 3;
// -
Long l=5L;
//
// +
String s=true;

, (
), , :
int i=10;
long l=i;
Child c = new Child();
Parent p=c;

, ,
:
//
int i=10;
short s=i; // ! !
Java

Rendered by www.RenderX.com

. 12 22

Parent p = new Child();


Child c=p; // ! !

, :
int i=10;
short s=(int)i;
Parent p = new Child();
Child c=(Child)p;

.
,
:
byte b=1;
short s=2+3;
char c=(byte)5+'a';

byte
int, . short
int, int. ,
char 5,
byte, .
, .
byte, short char.
byte, short, char int,
, ,
, .
, :
byte b=(byte)1; //
short s=(short)(2+3); //
char c=(char)((byte)5+'a'); //
// ,
// 200 byte
byte b2=(byte)200;

3.2.
,
, .
, :
// long
void calculate(long l) {
...
}
Java

Rendered by www.RenderX.com

. 13 22

void main() {
calculate(5);
}

, int, long,
.
,
. , .
//
void calculate(long l) {
...
}
void main() {
calculate(new Long(5)); //
}

, ,
.
void calculate(int l) {
...
}
void main() {
long l=5;
// calculate(l); // ! .
calculate((int)l); //
}

, , ,
.
, ,
byte, short, char int byte,
short char. .
:
//
//
// (byte, int) (short, short)
int m(byte a, int b) { return a+b; }
int m(short a, short b) { return a-b; }
void main() {

Java

Rendered by www.RenderX.com

. 14 22

print(m(12, 2)); // !
}

, (int,
int), .
, ,
, , ,
.
, ,
.
long get() {
return 5;
}

return int, ,
, long.
, .
,
:
short get(Parent p) {
return 5+'A'; //
}
void main() {
long l = //
get(new Child()); //
}

3.3.
.
, ,
. ,
. :
(byte)5
(Parent)new Child()
(Flat)getCity().getStreet().getHouse().getFlat()

,
. ,
, .
. ,
. , null
.
- .
Java

Rendered by www.RenderX.com

. 15 22

Child c=new Child();


// Child2 c2=(Child2)c; //
Parent p=c; //
Child2 c2=(Child2)p; //

, , ,
. "" .

3.4.
.
, . String
, . ,
String.
, String .
,
" ".
:
int i=1;
double d=i/2.;
String s="text";
print("i="+i+", d="+d+", s="+s);

:
i=1, d=0.5, s=text

3.5.
, ,
() 32 64 .
,
.
.

3.5.1.
byte, short char int
.
:

+ -;

~;

<<, >>, >>>.

Java

Rendered by www.RenderX.com

. 16 22

, ,
. ,
5<<3L int. , int long.

.

3.5.2.
double
int, long, float, double .
, ,
:

double, double;

, float, float;

, long, long;

int.

+, -, *, /, %;

<, <=, >, >=, ==, !=;

&, |, ^;

? :.

4.
, ,
.
, : ,
, .
. ,
.
, .
.
, .
:
byte b=3;
char c='A'+3;
long m=b+c;
double d=m-3F;

Java

Rendered by www.RenderX.com

. 17 22

b byte
int. c char ,
,
int. l int
long. , d double,
, float.
. -, , , ,
. (
, ).
, null.
, , ,
.
, ?
- , , ,
, . ,
. , -
. . ,
,
- . .
: A ,
A .
Point p = new Point();

,
.
Parent p = new Child();

, Child Parent.
p, , ,
Child, ,
Parent. , Child newChildMethod(),
p.newChildMethod() .
, ,
- Parent.
, ,
:
((Child)p).newChildMethod();

Child.
JVM , , p
Child. . Child,
newChildMethod(), ,
.
Java

Rendered by www.RenderX.com

. 18 22

- Object
.
, ( , , )
, ,
.

null

-

Object

null

5.

Java.
, ,
, , .
Java,
. 7 ,
. 4
. ,
, ,
. ,

. .
Java .
,
. -, ,
. .
,
.
.
.

6.
7-1.

, ,
?
byte b=1;
long m=-b;
Object o==;

Java

Rendered by www.RenderX.com

. 19 22

print(m+o+m);

a.) . :

7-2.

: , int byte

:
byte int, int long

: String Object

: : Object String long String


( Long).

?
float f = -16777217;

a.) . 16777217 1000000000000000000000001


, 25 . float 24
( ), .

1000000000000000000000000, 16777216 .
, . (

.)
7-3.

? ,
?
byte b=100-100;
byte b=100+100;
byte b=100*100;

a.) 0,
byte, . 2 3
byte ( 127),
.
7-4.

x ?
x==-x

a.) :

int long. ,
int 2147483648

0.0 0.0
Java

Rendered by www.RenderX.com

. 20 22

7-5.

d ?
(short)d==(short)(int)d
(int)d==(int)(long)d

a.) , short,
int.
, ,
d, , int.
231-1,
, 32:
double d=3e9;
System.out.println((int)d);
System.out.println((int)(long)d);

:
2147483647
-1294967296

7-6.

?
Object o = (String)null;
String s = o;

a.) . null , 2
(Object) String.
:
String s = (String)o;

7-7.

Wolf Rabbit Animal.


?
Wolf w = new Wolf();
Animal a = (Animal)w;
Rabbit r = (Rabbit)a;

a.) ,
. ,
Wolf, .
Wolf
Rabbit ,
.
7-8.

Java

Rendered by www.RenderX.com

. 21 22

a.) .
7-9.

?
public int add(byte a, byte b) {
short x=(short)a;
char y=(char)b;
return x+y;
}

a.) :

: byte short

: byte char

:
: short char int

7-10. ?
char c=65;
print(c);
print(+c);
print(=+c);

a.) :
A
65
=A

char,
. ,
int.
char String,
.
7-11. ?
byte b=1+2;

a.) ,
, , byte.
int.
7-12. ?
Object o = 123;
Child c = new Child();
Parent p=(Parent)new Child();

Java

Rendered by www.RenderX.com

. 22 22

Child x=null;

a.) :

String

Child

Child

null

Java

Rendered by www.RenderX.com