java - основы языка
TRANSCRIPT
Java
, , , , ,
, 2017
50 .
, . - Java.
abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while.
: byte, short, int, long, char, float, double, boolean
, : if, else, switch, case, default, while, do, for, break, continue
: try, catch, finally, throw, throws
: private, protected, public
/ : import, package, class, interface, extends, implements, static, final, void, abstract, native
/ / : new, return, this, super
: synchronized, volatile
: instanceof, enum, assert, transient, strictfp
: const, goto
Java 1.2: strictfp
Java 1.4: assert
Java 5: enum
const goto , .
true, false null . .
. , .
Java - . .
- null . null . null, .
: : byte, short, int, long
: float double
: char
: boolean
: , , .
-
boolean---Boolean
char16'\u0000' 0'\uFFFF' 216-1Char
byte8-128127Byte
short16-215215-1Short
int32-231231-1Integer
long64-263263-1Long
float32-1.4e-453.4e+38Float
double64-4.9e-324 1.8+308Double
, . .
, , .
.
byte 1 . .
short 2 . .
int 4 . 2 , . . byte short int.
long 8 .
( ) , , . , .
float ( ) 32 . , . , 32- .
double 64 . , , . .
char . 16- , Unicode. , 128 ASCII, 256
ISO-Latin-1, , , .
char 0 65535.
, 'A' , "A".
() boolean - true () false ().
, , , , if for.
, .
, .
: BigInteger BigDecimal . , . , , .
BigInteger: .
BigDecimal: .
( ). : ( ) ( )
:
__ = (_) ;
- (type casting). , , int byte:
int a = 257;byte b = (byte) a;
257 , b byte (256), 1.
byte>short; short>int; char>int; int>long; float>double; byte,short,char>float; int>double; float>double (strictfp) . .
. int double double.
, , , , , .
boolean . . , 0 == false, !0 == true.
:
byte, short, char int
long
float float
double
.
Java - NullPointerException.
, - , null. null, . , null, null.
null, .
null
. null, :
Math math = null;System.out.println(math.cos(1));
() , NullPointerException.
, x != null, :x.equals(null) == false;
null. , .
JVM null :aconst_null: null; ifnull ifnonnull: null.
null? ?
, - . , (32 64 ).
, .
,
: Byte, Short, Integer, Long, Character, Double, Float Boolean. , .
-
- ( ):
Integer i = new Integer(10);Integer i = 10;Integer i = Integer.valueOf("10");
Boolean "true", , true, false.
Number, , , :
byteValue()
doubleValue()
floatValue()
intValue()
longValue()
shortValue()
-.
.
"" .
.
Integer test = 100;// int i = test//
, .
- , , . , .
Integer itr = null; // int j = itr; //
NullPointerException
null , , null -, , , null pointer exception .
. - - , , .
, :
Integer i1 = 200, i2 = Integer.valueOf(200);System.out.println(i1 == i2);// falsei1 = 10; i2 = Integer.valueOf(10);System.out.println(i1 == i2);// trueSystem.out.println(new Integer(10)==new Integer(10));// false
:
[-128; 127]
Integer i=100 Integer i=Integer.valueOf(100);
valueOf(), , . , .
i1 i2 , . Integer, , .
, Java 6 JVM.
Byte, Short, Long , [-128;127].
Byte Short .
Character , [0;127].
- .
Boolean , , , Byte .
, . . , , .
int
123267890-144
long
12L3267890l-144L
byte, short
:
: 0xFF, 0X32
: 255
: 0377
: 0b11111111, 0B11001 (Java 7+)
Java 7 . .
: 1234_5678__1736
. . 0xFFFFFFFF_F : integer number too large. long.
, :
long l = 100000;
int , , . : incompatible types: possible lossy conversion from int to byte.
:
- double 123.45
D, d double 123.45D
F, f float 123.45F
, hex , , . .
- :
:
1.39e-43f
47e47
POSITIVE_INFINITY = 1.0f / 0.0f;NEGATIVE_INFINITY = -1.0f / 0.0f;NaN = 0.0f / 0.0f;
:
(boolean): true, false
(char)
(String)
null: null
null .
, . , . , .
(Escape-) .
\b (backspace)\u0008
\t (horizontal tab)\u0009
\n (new line)\u000a
\f (form feed)\u000c
\r (carriage return)\u000d
\" (double quote)\u0022
\' (single quote)\u0027
\\ (backslash)\u005c
\uXXXX Unicode\uXXXX
Java 8: Unicode 6.2.0.
U+0000 U+10FFFF.
, U+FFFF - . 16- , U+D800 U+DBFF, U+DC00 U+DFFF.
U+FFFF UTF-16.
Character : 32- .
. '\u000a' '\u000d' : "illegal line end in character literal". '\n' '\r'.
: , . :
"Hello, World!" == "Hello," + " World!"
String. , ( String.intern). , .
new String("Hello, World!") != "Hello, World!"
new String("Hello, World!").intern() == "Hello, World!"
-
: , ,
==
, : - ,
-
.
: [ = ] [, [ = ] ];
, : , . ( ), , .
. , , "_", "$"
- , ( ) .
int x = 100double d;
. - . - .
, , , . , - .
, . Java , , , , .
, , , . . Java , "variable" "Variable" .
UTF-16. .
"_" "$" Java . , , , . , .
( ).
int timeToStart = 1000;
class MyVeryUsefulClass { }
, . , . - .
byte b;// b = 17;// int i = 1000 * 256 b;// + MyFirstClass myFirstClass;// MyClass myClass = new MyClass()// +
. MyClass myClass; ( myClass MyClass).
. : 0, 0.0, '\u0000', false, null.
. : variable XXX might not have been initialized.
. , , .
, "". - .
() (scope) , . , .
: { }. . , . .
, :
-
-
, , , . .
, , .
. , , , . , .
, .
: , . : package.class.name
md.leonis.first.package.FirstClass.classField
.
: . .
import . , .
Java , , .
import java.util.ArrayList
ArrayList . :
import java.util.*
Java . C++, .
(statement) ; . .
: , , =, ;, {}, if, switch, , , break, continue, assert, return.
(operator) , . .
- , .
: >>>, !=, -, *, ...
, . .
44 :
(assignment)
(arithmetic), (unary)
(equality & relational), (type comparision)
(conditional)
(logical)
(bitwise and bit shift)
.
-, , . , , .
45 . . Java, C++ .
, , , 3 :
() (+ab) ;
(a+b) ;
( ) (ab+) .
, , . , .
, , .
+ - * / = .
Java, =, == != . String + +=.
: null NullPointerException.
: ArithmeticException
, : OutOfMemoryError, , .
:
, .. - - .
: , : +, -, , .
: , .
=. . , . .
int x = 144;int y = 12 - x;
. :
x = y = 12;
. .
Object obj1 = obj2;
, . , .
+=, >>>=
obj1
obj2
. Java . , , .
int i = (int) 100;// int. long l = i; // int longi = (int) l; //
, . . , int long, , , byte.
, int (char, byte, short) int int. , , , .
, . , int , long double .
, , .
++, --, % .
+ += . , - .
"+" "-" . , .
, , .
. .
, . , /.
true/false. .
. , , , , , char, , boolean.
. , . . equals().
boolean. .
boolean b = true, b2 = false;boolean b3 = b & b2;
if, while, do. , .
" " " " , , . , :
if (x == 10 & y++ < 100) z = 256;
& .
, . if-else.
if-else .
? 1 : 2
- true - , . , , void.
result = x == 0 ? 0 : x / 5;
. - .
Java , 90- .
.
, , .
, ~ . :
&= - .
( ~) boolean. 1 . , XOR.
. .
, . . . .
>> , . .
>> , , ( ) , .
.
char, byte short int.
int, . long ( 0 61 ).
, >>>= . byte short, int, , . .
. , , . , . , .
, .
, , , , . . , : .
( ) [ ] .
++ --
++ --, + - ~ !
new ()
* / %
+ -
>> >>> = <