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

N 2

Java
1.
Java. ,
,
() .
"", Java.
.
Java
.
2.
2.1 Java. .
Java
, .
, escape-
, .
, ,
, , .
javac .
: , ,
, , .
Java
: ,
.
, ,
secretPassword some_thing.
(keywords) . ,
, ,
Java (
).
Java
boolea
n

long

defaul
t

while

static

class

catch

import

byte

short

else

final

synchronize
d

extends

finall
y

packag
e

char

break

for

native

threadsafe

implement
s

throw

null

double

case

if

private

transient

instanceo
f

throws

super

float

continu
e

return

protecte
d

void

interface

try

this

int

do

switch

public

abstract

new

false

true


< > <> [final] <>[=<>];
:
;
,
;
2.2. .
. Java
(character literals).
, .
,
,
. (\)
, . ,
\ .
.
:
escape

(BS)

\b

(backspace)

\t

(tab)

(LF)

\n

(linefeed)

(FF)

\f

(from feed)

(CH)

\r

(carriage return)

\\

\ddd

ddd

\xdd


dd

\udddd

dddd
Unicode

(HT)

Unicode

(string literals)
, , " ".
"Hello, World ,

. javac
.
( ).
, "This is the beginning",
- " of a beautiful relationship .
"This is the beginning" + " of a beautiful
relationship". ( .)
.
, ,
.
.
2.3.
(operators) ,
.
(, 2+3). ,
(+), . +
.

3+2
pre+fix

12-3

length*width

miles/gallons

10%4

&

num&musk

this|that

tall^short

mask=~item

<<

var<<3

>>

var>>12

<<<

arg<<<howMuch

>>>

Arg>>>howMuch

==

if (a==14)

!=

if (a!=14)

<

if (a<14)

>

if (a>14)

<=

if (a<=1)

>=

if (a>=4)

&

if ((a>14)&(a<17))

&&

if((a>14)&&(b>17))

if((a==16)|(b==19))

||

if ((a==25)||(a==8))

if (!((a==16)||(a==3)))

if ((a==16)^(a==19))

a+=b

a=a+b

a-=b

a=a-b

a*=b

a=a*b

a/=b

a=a/b

a%=b

a=a%b

a&=b

a=a&b

a|=b

a=a|b

a^=b

a=a^b

a<<=b

a=a<<b

a>>=b

a=a>>b

.()[]

<< >> <<<


>>>

++ -- !
~

< > <= >=

&&

== !=

||

new
*/%

&

?:

+-

= += -= *= /= %= &= ||=
<<= >> = ^=


2.4
Java . ,
, , . , BASIC,
. ,
, .
2.4.1
Java.

,

byte

8 27 27-1
short
16 216 216-1
int

32 232 232-1
long
64 264 264-1
float

32 1.7*10-38

1.7*1038
double

64

-1.40239846E-45 3.40282347E+38
char
16 , , ,
16-
Unicode /u0000 /uffff
boolean

8 true false
()

0
0
0
0
0.0f
0.0d
'0x0'

false

() (boolean literal) :
true () false (). (

Pascal), - , 0 1.
,
.
. char 16-
, unicode. ,
unicode escape- .
, unicode
. , Java Unicode,

Java.
Java.
, Java
.
0. char
null, boolean false.
2.4.2
Java
(reference data types). ,
.
, .
.
Java
.
2.4.3
(arrays) .
,
. ,
.
, float,
char int. , , .
(
, ).
:
<> <_>[]

<> , .
.
<_> .
,
.
Java. ,
:
int []d; //
int d[]; //

, , .
, d
, .

:

int[] array1,array2, array3;

,
new,
.
:
d=new int[10];

d
.
, .
. ( -
null).
new:
Foo[] c=new Foo[10]; //
String b[]=new String[10];

,
. , , ,
d:
d[0]=1;
d[1]=2;
d[2]=6;
d[3]=5;
d[4]=1;
...
d[9]=7;

:
int [] a;
a={1,3,67, 77,11,2};
int [] b={3,2,1};
String s1=One;
String s2=Two;
String c[]={s1,s2};

a , b .
,
.
,
:
class Array{
public static void main (String args[]){
int LISTSIZE =5;
String[] ShoppingList;
int i=LISTSIZE;
//
ShoppingList = new String[LISTSIZE];
//
ShoppingList[0] = carrots;
ShoppingList[1] = tofu;
ShoppingList[2] = rise milk;
ShoppingList[3] = onions;
ShoppingList[4] = pasta noodles;
for (i=0; i<LISTSIZE;i++)
System.out.println(ShoppingList[i]);
}
}

int n;
// n .
int intArray[]=new int[n];

intArray
n . n ( )
, . , Java
.
.
.
intArray=new int[250]; //

Java
.
,
length, .
:
for (int i=0; i<intArray.length;i++)
intArray[i]=i;

intArray.length .
Java .
Java ( Java
Java , ).
, .
.
, :
int d[][];

.
, , ,
[3][4].
Java ,
.
. ,
.
:
int ticTacToeBoard[][]=new int [3][3];
int AgeWeight [][]=new int[100][]; //
int
int AgeWeight [0][]=new int[3];
int AgeWeight [1][]=new int[5];
//

: ticTacToeBoard 3 3
AgeWeight, .
,
(). ,
fifthDimention:
double fifthDimention[][][][][];

2.5.
Java
if (), for (), while ().

2.5.1 if
if:
if (<>) <1>;
else <2>;
/*else- .*/

, 1 .
else-, 2.
Java
()
true false.
.
( ).
, .
2.5.2 switch
switch
.
switch ():
switch (){
case _1: ;
break;
case _2: ;
break;
.
default: ;
}

switch case-. , case-, ,


case- . case , default-,
, switch
. case- ,
,
switch-, ,
case-,
switch () break.
2.5.3 for
for:
for(_1; _2; 3) ;

_1 .
_1 ,
. _1 .
_2 ( .. _2
, ,
). _2 , , .
_3 .
.
:

for (int i=1; i<7; i++){


System.out.println(x);
}

, for .
for(i=0; IsTrustIt(item[i]), i++);

.
IsTrustIt, .
: int
i for,
i for,
.
2.5.4 while
while:
while();

- true, ,
.
while:
int x=100;
while (x>0){
System.out.println(x--);
}

(x>0), true,
, false.
2.5.5 do-while
do , (
).
while.
do ;
while() ;

do-while .
do-while:
do{

System.out.println(time);
time--;
}while(time>0);

2.5.6

if-else. (?),
(:):
int PosValue = x>0? x:0;
float Avg=num>1?total/num:num;


. true, ,
false . .
x=10 PosValue 10, x=-1 0.
num>1,

. ,
, , , ,
.
2.5.7 break continue
break Java
, break.
break switch. break
break :
break;

Java while,
do, for switch. :
for(int i - 0; i < data.Length; i++) { // data
if(data[i] == target) {
// , ,
Index = i;
//, ,
break;
// !
}
}
// break Java .

Java goto. break


. break Java
, ,
switch . :
testfornull: if (data != null) {
// ,
for(int row=0; row<numrows; row++) {
// ,
for(int col=0; col<numcols; col++) { // .
if(date[row][col] == null)
// ,
break testfornull;
// , .
}
} // break testfornull Java .

break ; continue
. continue, , ,
while, do for. continue . continue . ,
, . :
for(lnt i=0; i<data.length; i--) {
if(dafa[i] == -1)
continue;
process(dafa[i]),
}

// data.
// ,
// .
// .

2.6
2.6.1
.
class:
class _ {

}

.
,
(, ,).

/*

(, )
*/
public class Point {
public double x, ;
//
public Point (double x. double ) { // ,
this.x = ; this. = ;
//
}
public double distanceFromOrigin() {
//,
return Math.sqrt(x*x + y*y);
//
}
}

Polnt.java
Point.class. Java-
.
2.6.2
:
_ _;

(
) new:
_=new _();

.
( )
.:
_._._


// Point (2, -3.5)
//
Point = new Point(2.0, -3.5);
p.y = p.x+p.x;
double d = p.distanceFromOrigin();


.:
_._

java.lang .
System.out.println(). System
java.lang, ut , , PrintStream,
println() .
, ,
_.
2.6.2 .
Point
class Point
{
int , ;
Point(int , int )
{
this.x = ;
this. y = y;

}
double distance(int , int )
{
int dx = this.x - ;
int dy = this. - ;
return Math.sqrt(dx*dx + dy*dy);
}
double distance(Point p)
{
return distance(p.x, p.y);
}

}
class PointDist
{
public static void main(String args[])
{
Point p1 = new Point(0, 0);
Point p2 = new Point(30, 40);
System.out.println("p1 = " + pi.x + ", " + p1.y);
System.out.println("p2 = " + p2.x + ", " + p2.y);
System.out.println("p1.distance(p2) = " + p1.distance(p2));
System.out.println("p1.distance(60, 80) = " + p1.distance(60, 80));
}
}

main PointDist
Point .

3.
Java
1. TeachPro Java
2. .. JAVA
3. , JAVA.
4. , Java.
5. .. JAVA. intuit.ru
4.
:
1. Eclipse.
2. 1, .
3. 2, (main).
4. 1 .
1.
5. 1.
.
6. 1 (
, )
7. 1, ,
.
8. print 1, .
9. finalize(), ,
.
10. toString()
11. 1 2.
12. 1 main.
13. :
:

;
.
5.
:
1) ;
2) ;
3) ;
4) .
6.

, random()(
Math java.lang) 2 .
(
1 6) ,
. 300 .

(
Math.random()) ,
( System.in.read()).

10

12

.
random() .

13

.
random() .

14

.
random()

15

.
random()

Оценить