float double. - 4 8 , . . :
4.2. . | ||
() | ||
float | 3.40282347e+38f; 1.40239846e-45f | |
double | 1.79769313486231570e+308; 4.94065645841246544e-324 |
, . , . , , , overflow. , , underflow.
// float f = 1e40f; // , overflowdouble d = 1e-350; // , underflow, F f, float. double, D d.
:
- ( )
- <, <=, >, >=
- ==,!=
- ( )
- + -
- +, -, *, /, %
- ( ): ++ --
- ?:
- +
, ( % , ++ -- ). , . , .
overflow underflow. , Java . , . , . , .
. IEEE 754 3:
- (positive/negative infinity);
- " ", Not-a-Number, NaN;
- .
float, double.
:
1f/0f // , // float-1d/0d // , // doubleFloat 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 // -, - //. , . +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 floatprint(-1e-300/1e100); // underflow doublefloat f=1e-6f;print(f);f+=0.002f;print(f);f+=3;print(f);f+=4000;print(f);:
0.0-0.0 1.0E-60.0020013.0020014003.002, 6- .
( Java):
double d = 1e-305 * Math.PI;print(d);for (int i = 0; i < 4; i++)print(d /= 100000);:
3.141592653589793E-3053.1415926535898E-3103.141592653E-3153.142E-3200.0, , , (overflow) (underflow) , (. 3). , .
. double, double. 64 .
double , float, float, 32 float.
, . NaN, NaN.
:
print(1/2);print(1/2.);:
|
|
, .
:
int x=3;int y=5;print (x/y);print((double)x/y);print(1.0*x/y);:
00.60.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);:
111111110656float , long float .
- (wrapper classes). float double Float Double. . , .
, Math , , , , . PI E.
boolean, true false. .
:
- ( )
- ==,!=
- ( )
- !
- &, |, ^
- &&, ||
- ?:
- +
&& || . ?: boolean. , .
"true" "false" .
, , if.
. , ( ), x!=0. boolean ref!=null.
, null, , JVM.
(object) , . . , , . , .
new, new ( , ). , . ( ), , , . , Point, new Point (3,5) Point, int. , . , Point:
|
|
, 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"String. , .
Java. . , , , . , , , .
, Java 1.1 reflection, , , . new, , , , . :
Point p = null;try { // , // Point, // newp=(Point)Class.forName("Point").newInstance(); } catch (Exception e) { // System.out.println(e);}"", . , , , . , . , Parent Child:
|
|
- . :
Parent p = new Child();Parent , Child.
:
- instanceof ( )
- == != ( )
- ?:
- +
. . (). .
instanceof, , . . , , . :
Parent p = new Child();// p Parent// Childprint(p instanceof Child);true. , instanceof , , . , . :
// // Childclass ChildOfChild extends Child { }:
Parent p = new ChildOfChild();print(p instanceof Child);Parent, , ChildOfChild. instanceof Parent Child, , . , true, , , Child.
:
class Child2 extends Parent {}Parent:
Parent p=new Child();print(p instanceof Child);print(p instanceof Child2);p Parent, , Child Child2. instanceof :
truefalse, 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);:
truefalse, p2 , p1. , , p3 - . , new.
== null, , false. null, true.
equals, . , :
String s = "abc";s=s+1;print(s.equals("abc1"));?: , . .
, . null, "null". , ( , toString()) , , .
Object
Java . . , , . , , Object. , . , , , Object. , ( Object ), .
|
|
.
getClass()
Class, , . Class . getName(), :
String s = "abc";Class cl=s.getClass();System.out.println(cl.getName());:
java.lang.Stringinstanceof, getClass() , .
equals()
Object boolean. , equals() , . , , .
Point p1=new Point(2,3);Point p2=new Point(2,3);print(p1.equals(p2));false.
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 , - .
:
print(new Object());:
java.lang.Object@92d342, , -, , .
, .
finalize()
(garbage collector). Object , - , , , , .. , . , .
finalize() , . JVM .
String
, String Java . new. String, ( null), .
, , , .
. , , , -, .
String s="a";s="b";, String.
, Java, .
-, , .
String s1 = "abc";String s2 = "abc";String s3 = "a"+"bc";print(s1==s2);print(s1==s3);:
truetrue, , , .
, , :
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().
Class
, , .
Class Java. JVM .class, , Class, .
,
Point p=new Point(1,2);, :
- Point, (1,2);
- Class, Point;
- Class, Object. Point Object, ;
- Class, Class. Java-, .
Class getClass() Object. :
Class cl=p.getClass(); // 2 Class cl2=cl.getClass(); // 4 Class cl3=cl2.getClass(); // 4cl2==cl3 .
Class reflection.
, Java , ( , ..).
. , . , :
- ;
- ;
- ;
- , ;
- ;
- ;
- instanceof;
- ;
- , import - ..
. Java , . .
Java 1.5 (templates), , .
, Java , . , .
Java , , (8 ) (). . . null, . JVM .
. , . . final.
, , . , . , .
, , , instanceof. Java Object, Class, String.