| | >> | ||
|= | ( ) | >>= | ( ) |
& | >>> | ||
&= | (c ) | >>>= | |
^ | << | ||
^= | (c ) | <<= | |
~ |
< | > | ||
<= | >= | ||
== | != |
, , .
|| | && | ||
! |
instanceof ?: (if-then-else).
boolean Boolean (true false).
// # 2: %: DemoOperators.java
package chapt02;
public class DemoOperators {
public static void main(String[] args) {
System. out. println("5%1=" + 5%1 + " 5%2=" + 5%2);
int b1 = 0xe;//14 1110
int b2 = 0x9;//9 1001
int i = 0;
System. out. println(b1 + "|" + b2 + " = " + (b1|b2));
System. out. println(b1 + "&" + b2 + " = " + (b1&b2));
System. out. println(b1 + "^" + b2 + " = " + (b1^b2));
System. out. println("~" + b2 + " = " + ~b2);
System. out. println(b1 + ">>" + ++i + " = " + (b1>>i));
System. out. println(b1 + "<<" + i + " = " + (b1<<i++));
System. out. println(b1 + ">>>" + i +" = " + (b1>>>i));
}
}
:
5%1=0 5%2=1
14|9 = 15
14&9 = 8
14^9 = 7
~9 = -10
14>>1 = 7
14<<1 = 28
14>>>2 = 3
"? " :
boolean? :
boolean true, , .
instanceof true, . , :
class Course extends Object {}
class BaseCourse extends Course {}
class FreeCourse extends BaseCourse {}
BaseCourse |
Course |
FreeCourse |
OptionalCourse |
class OptionalCourse extends Course {}
. 2.2.
instanceof doLogic(Course c):
void doLogic(Course c) {
if (c instanceof BaseCourse) { /* BaseCourse
FreeCourse*/
|
|
} else if (c instanceof OptionalCourse) { /*
OptionalCourse*/
} else { /* Course*/ }
}
instanceof , , . Object , Object. null , null - . null . null , instanceof.
-
, Java - (wrapper-) java.lang: Boolean, Character, Integer, Byte, Short, Long, Float, Double. , .
Float |
Number |
+ |
byteValue(): byte |
+ |
doubleValue(): double |
+ |
floatValue(): float |
+ |
intValue(): int |
+ |
longValue(): long |
+ |
shortValue(): short |
interface |
Comparable |
Integer |
Byte |
Double |
Long |
Short |
Character |
Void |
Boolean |
+ |
booleanValue(): boolean |
+ |
equals(Object): boolean |
+ |
getBoolean(String): boolean |
+ |
compareTo(Boolean): int |
+ |
parseBoolean(String): boolean |
+ |
valueOf(): Boolean |
. 2.3. -
, , - . , , java.lang, Number Comparable, . - null.
, . Value() .
Character Number, , . Character . Character, , String.
/* # 3: : CastTypes.java */
package chapt02;
public class CastTypes {
public static void main(String[] args) {
Float f1 = new Float(10.71); // double Float
String s1 = Float. toString (0f); // float String
String s2 = String. valueOf (f1); // Float String
Byte b = Byte. valueOf ("120"); // String Byte
double d = b.doubleValue(); // Byte double
byte b0=(byte)(float)f1; // Float byte
//b2 = (byte)f1; // !!!
/*f1 , */
|
|
short s = (short) d; // double short
/* Character int */
Character ch = new Character('3');
int i = Character. digit (ch.charValue(), 10);
System. out. printf("f1=%1.2e s1=%s s2=%s%n", f1, s1, s2);
System. out. printf("b=%o d=%.1f b0=%d s=%d i=%d",
b, d, b0, s, i);
}
}
:
f1=1.07e+01 s1=0.0 s2=10.71
b=170 d=120,0 b0=10 s=120 i=3
valueOf(String str) -, , , , .
Java
java.math.BigInteger java.math.BigDecimal, .
Java String. String " + " . '\0', ASCII-, .
5.0 (). new. :
Integer iob = 71;
- . , intValue(), doubleValue() .
, , / :
// # 4: autoboxing & unboxing: NewProperties.java
package chapt02;
public class NewProperties {
public static void main(String[] args) {
Integer j = 71; // +
Integer k = ++j; //++
int i = 2;
k = i + j + k;
}
}
NullPointerException null Integer:
Integer j = null; // !
int i = j; //
, -, .
int i = 128; // 127!!!
Integer oa = i; // +
Integer ob = i;
System. out. println("oa==i " + (oa == i)); // true
System. out. println("ob==i " + (ob == i)); // true
System. out. println("oa==ob " + (oa == ob)); // false( )
System. out. println("equals ->" + oa.equals(i)
+ ob.equals(i)
+ oa.equals(ob)); // true
equals() , , . oa.equals(ob) true.
equals(). :
boolean b = i.equals(oa); //
- ,
Float f = 7; // (float)7 7F 7
.
, , , Number, :
Number n1 = 1;
Number n2 = 7.1;
Number array[] = {71, 7.1, 7L};
.
, - .
if :
if (boolexp) { /**/ } //1
|
|
else { /**/ } //2
boolexp true, 1, 2. else , if ( 2), if. - if {} else if {}.
C++ switch:
switch (exp) {
case exp1:{ /**/ }
Break;
case expN:{ /**/ }
Break;
default: { /**/ }
}
exp==exp1 , break, exp1,, expN int, byte, short, char enum.
, . if , , else . switch case ( ), default.
Java , ++:
while (boolexp) { /**/ } //
do { /**/ } while (boolexp); //
for (exp1; boolexp; exp3){ /**/ } //
exp1 , boolexp , exp3 , ( , ). , boolexp true.
:
- , for while , do/while .
- for . while , , , . while(true).
- for .
- , .
- .
- . .
- .
5.0 , :
for ( : ){ /**/ }
.
int [] array = {1, 3, 5};
for (int i: array) //
System. out. printf("%d ", i); //
. , Iterable Iterator.
Java break continue, , :
int j = -3;
OUT: while (true) {
for (;;)
while (j < 10) {
if (j == 0)
break OUT;
else {
j++;
System. out. printf("%d ", j);
|
|
}
}
}
System. out. print("end");
break , OUT. goto .
, . . . Java , new . , , null . , : int a[]. int[] a.
/* # 5: : ArrRef.java */
package chapt02;
public class ArrRef {
public static void main(String[] args) {
int myRef[], my; //
float [] myRefFloat, myFloat; // !
//
int myDyn[] = new int [10];
/* */
int myInt[] = {5, 7, 9, -5, 6, -2}; //6
byte myByte[] = {1, 3, 5}; //3
/* Object */
Object myObj = new float [5];
//
myRef = myDyn;
myDyn = myInt;
myRefFloat = (float [])myObj;
myObj = myByte; //
myRefFloat = (float [])myObj; //
// ()
// myInt = myByte;
//myInt = (int [])myByte;
}
}
Object . , . , . .
myDyn=myInt , myDyn myInt, .
, . length.
Java , . , :
int arr[][] = { { 1 },
{ 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9, 0 }
};
, arr[2][0] , 4.
Matrix |
- |
a: int[][] |
+ |
Matrix(int, int) |
+ |
getHorizontalSize(): int |
+ |
getVerticalSize(): int |
+ |
getElement(int, int): int |
+ |
setElement(int, int, int): void |
+ |
toString(): String |
MatrixFactory |
+ |
createRandomized(int, int): Matrix |
Multiplicator |
+ |
multiply(Matrix, Matrix): void |
() .
. 2.4.
/* # 6: : Matrix.java */
package chapt02;
public class Matrix {
private int [][] a;
public Matrix(int n, int m) {
//
a = new int [n][m];
}
public int getVerticalSize() {
return a.length;
}
public int getHorizontalSize() {
return a[0].length;
}
public int getElement(int i, int j) {
return a[i][j];
}
public void setElement(int i, int j, int value) {
a[i][j] = value;
}
public String toString() {
String s = "\nMatrix: " + a.length +
"x" + a[0].length + "\n";
for (int [] vector: a) {
for (int value: vector)
s+= value+" ";
s += "\n";
}
return s;
}
}
/* # 7: - : MatrixFactory.java */
package chapt02;
public class MatrixFactory {
public static Matrix createRandomized(int n, int m) {
Matrix matrix = new Matrix(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
|
|
matrix.setElement(i, j, (int)(Math. random ()*m*m));
}
}
return matrix;
}
}
/* # 8: : Multiplicator.java */
package chapt02;
public class Multiplicator {
public static Matrix multiply(Matrix p, Matrix q)
throws MultipleException {
int v = p.getVerticalSize();
int h = q.getHorizontalSize();
int temp = p.getHorizontalSize();
//
if (temp!= q.getVerticalSize())
throw new MultipleException();
//
Matrix result = new Matrix(v, h);
//
for (int i = 0; i < v; i++)
for (int j = 0; j < h; j++) {
int value = 0;
for (int k = 0; k < temp; k++) {
value += p.getElement(i, k) * q.getElement(k, j);
}
result.setElement(i, j, value);
}
return result;
}
}
/* # 9: : MultipleException.java */
package chapt02;
public class MultipleException extends Exception {}
/* # 10: , : Runner.java */
package chapt02;
public class Runner {
public static void main(String[] args) {
int n = 2, m = 3, l = 4;
Matrix p = MatrixFactory. createRandomized (n, m);
Matrix q = MatrixFactory. createRandomized (m, l);
System. out. println("Matrix first is: " + p);
System. out. println("Matrix second is: " + q);
try {
Matrix result = Multiplicator. multiply (p, q);
System. out. println("Matrix product is: "
+ result);
} catch (MultipleException e){
System. err. println("Matrices could"
+ " not be multiplied: ");
}
}
}
random(), :
Matrix first is:
Matrix: 2x3
6 4 2
0 8 4
Matrix second is:
Matrix: 3x4
8 0 2 7
6 1 0 0
1 2 4 5
Matrix product is:
Matrix: 2x4
74 8 20 52
52 16 16 20
. , null. .
th
java.lang.Math , E PI.
( Math ). , , floor(), ceil(), rint(), round(), max(), min(), , , .. , random() Math.
/* # 11: Math: MathMethods.java */
package chapt02;
public class MathMethods {
public static void main(String[] args) {
final int MAX_VALUE = 10;
double d;
d = Math. random () * MAX_VALUE;
System. out. println("d = " + d);
System. out. println(" ="
+ Math. round (d));
System. out. println(" , "
+ " <= ="
+ Math. floor (d));
System. out. println(" , "
+ " >= ="
+ Math. ceil (d));
System. out. println(" "
+ " =" + Math. rint (d));
}
}
:
d = 0.08439575016076173
=0
, <= =0.0
, >= =1.0
=0.0
java.lang. java.lang.System, . .
, System.in, System.out System.err, / , :
void gc() ;
void exit(int status) java- (JVM);
void setIn(InputStream in), void setOut(PrintStream out),
void setErr(PrintStream err) /;
Properties getProperties() ;
String getProperty(String key) ;
void setSecurityManager(SecurityManager s), SecurityManager getSecurityManager() ;
void load(String filename) ;
void loadLibrary(String libname) ;
void arrayCopy( ) .
java.lang.Runtime. Runtime getRuntime(), Runtime, . exit(int status) halt(int status). : gc(),
runFinalization() . totalMemory() freeMemory().
/* # 12: : RuntimeDemo.java*/
package chapt02;
public class RuntimeDemo {
public static void main(String[] args) {
Runtime rt = Runtime. getRuntime ();
System. out. println(" : "
+ rt.totalMemory());
System. out. println(" : "
+ rt.freeMemory());
double d[] = new double [10000];
System. out. println(" " +
" : " + rt.freeMemory());
//
ProcessBuilder pb =
new ProcessBuilder("mspaint","c:\\temp\\cow.gif");
try {
pb.start(); // mspaint.exe
} catch (java.io.IOException e) {
System. err. println(e.getMessage());
}
System. out. println(" "
+ " mspaint.exe: " + rt.freeMemory());
System. out. println(" : "
+ pb.command());
}
}
, , :
: 2031616
: 1903632
: 1823336
mspaint.exe: 1819680
: [mspaint, c:\temp\cow.gif]
java.lang.ProcessBuilder, start(), . .
arraycopy() System, , .
/* # 13: : ArrayCopyDemo.java */
package chapt02;
public class ArrayCopyDemo {
public static void main(String[] args) {
int mas1[] = { 1, 2, 3 },
mas2[] = { 4, 5, 6, 7, 8, 9 };
show ("mas1[]: ", mas1);
show ("mas2[]: ", mas2);
// mas1[] mas2[]
System. arraycopy (mas1, 0, mas2, 2, 3);
/*
0 mas1[] ,
2 , ,
3
*/
System. out. printf("%n arraycopy(): ");
show ("mas1[]: ", mas1);
show ("mas2[]: ", mas2);
}
private static void show(String s, int [] mas) {
System. out. printf("%n%s", s);
for (int i: mas) System. out. printf("%d ", i);
}
}
:
mas1[]: 1 2 3
mas2[]: 4 5 6 7 8 9
arraycopy():
mas1[]: 1 2 3
mas2[]: 4 5 1 2 3 9
2
A
, , . Date. /** */, HTML- Web-.
1. n , . .
2. n . () .
3. n . , () , .
4. n . , . , .
5. n . , , .
6. n . , . , .
7. n . , . , .
8. n . , , -. , .
9. 18, .
10. switch, , k (-10k, 0], (0, 5], (5, 10], (10, 10k].
11. switch, , k (-10k, 5], [0, 10], [5, 15], [10, 10k].
12. , 1 25 5x5 .
13. , . .
14. 1 12. , . ( ).
n a [ n ][ n ]. - n n .
1. () k - ().
2. k (, , ).
3. () , .
4. , .
5. .
6. .
7. 90 (180, 270) .
8. .
9. , .
10. () , .
11. , , .
12. .
13. , , , .
14. .
15. . ( i,j, i,j i - j - ).
16. , , .
17. . ( , . , .)
18. . ( , .)
19. , , . ( ).
20. , , (2,2), (3,3) . ., .
2
2.1.
?
1) float f = 7.0;
2) char c = "z";
3) byte b = 255;
4) boolean n = null;
5) int i = 32565;
6) int j = .
2.2.
?
1) if (i<j) { System.out.print("-1-"); }
2) if (i<j) then System.out.print("-2-");
3) if i<j { System.out.print("-3-"); }
4) if [i<j] System.out.print("-4-");
5) if (i<j) System.out.print("-5-");
6) if {i<j} then System.out.print("-6-");.
2.3.
?
1) 2int;
2) int_#;
3) _int;
4) _2_;
5) $int;
6) #int.
2.4.
?
int a1[] = {};
int a2[] = new int []{1,2,3};
int a3[] = new int [](1,2,3);
int a4[] = new int [3];
int a5[] = new int [3]{1,2,3};
1) a1;
2) a2;
3) a3;
4) a4;
5) a5.
, , .
Java, , , . . , , , .
. , , .
. / , .
:
class {
{} //
//
private //
protected //
public //
}
. :
c ;
Java , static () , , . public, private, protected . , . , , .
final . final , , .
, :
/* # 1: : Second.java */
package chapt03;
import java.util.*;
class Second {
private int x; //
private int y = 71; //
public final int CURRENT_YEAR = 2007; //
protected static int bonus; //
static String version = "Java SE 6"; //
protected Calendar now;
public int method(int z) { //
z++;
int a; //
//a++; // ,
a = 4; //
a++;
now = Calendar. getInstance (); //
return a + x + y + z;
}
}
, , ( String). , new.
Java , . - Java :
, ;
private ;
protected , , ;
public .
( ), public, . , private, . , . . , , , , protected. , .
public (enclosing) . , .
- , . , ; , new . , .
Java , ( ). finalize(). Java , , .
/* # 2: : Quest.java */
package chapt03;
public class Quest {
private int id;
private String text;
// ( )
public Quest() {
super (); /* ,
*/
}
//
public Quest(int idc, String txt) {
super (); /*
, */
id = idc;
text = txt;
}
}
Quest , :
Quest a = new Quest(); //
Quest b = new Quest(71, " boolean?");
new , , .
, Java , , : 0, false, null. , . . . , :
super( );
Point c (), .
/* # 3: : Point.java: LocateLogic.java: Runner.java */
package chapt03;
public class Point {
/* */
private final double x;
private final double y;
public Point(final double xx, final double yy) {
super ();
x = xx;
y = yy;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
package chapt03;
public class LocateLogic {
public double calculateDistance(
Point t1, Point t2) {
/* */
double dx = t1.getX() - t2.getX();
double dy = t1.getY() - t2.getY();
return Math. hypot (dx, dy);
}
}
package chapt03;
public class Runner {
public static void main(String[] args) {
//
Point t1 = new Point(5, 10);
Point t2 = new Point(2, 6);
System. out. print(" : "
+ new LocateLogic().calculateDistance(t1, t2));
}
}
:
: 5.0
public, . private , . protected .
. .
Java . :
returnType methodName(_) {
//
return value; // (returnType void)
}
, return , void. void , . ( ):
ObjectName.methodName();
- new.
, . ( void) . , , .
[] [static] [abstract] [final] [native]
[synchronized] returnType methodName(_)
[throws _]
, public, private, protected . .
, , .
, static, . , . , static. , this , . , . this , , .
// # 4: : Mark.java
package chapt03;
public class Mark {
private int mark = 3;
public static int coeff = 5;
public double getResult() {
return (double) coeff *mark/100;
}
public static void setCoeffFloat(float c) {
coeff = (int) coeff*c;;
}
public void setMark(int mark) {
this. mark = mark;
}
//
/*public static int getResult() {
setMark(5);//
return coeff*mark/100;//
}*/
}
Mark ob1 = new Mark();
Mark ob2 = new Mark();
ob1.coeff ob2.coeff 5, . :
Mark.coeff = 7;
:
ClassName. methodName (), :
Mark. setCoeffFloat ();
float z = Math. max (x, y); //
System. exit (1); //
, , .
, , .
final
final , . , final, , . :
/* # 5: final- : Rector.java: ProRector.java */
package chapt03;
public class Rector {
//
final int ID = (int)(Math. random ()*10);
//
final String NAME_RECTOR;
public Rector() {
//
NAME_RECTOR = ""; // !!!
}
// {NAME_RECTOR = "";} // !!!
public final void jobRector() {
//
// ID = 100; //!
}