You are on page 1of 53

java

java
java
(_)($)

indentifier
username
user_name
_userName
$username

class
98.3
Hello World

12 0x12012

13L

5.1f.4f 2e3f 0f

5.1.42e-30d

truefalse

null

a 8 '\u0027'
\r
\n
\ttable
\bBack Space
\\
\\\

Hello World123 "Welcome \nXXX"


nullnull


int x=0,y;
y=x+3

(byte, short, int, long)

(float, double)

(char)

boolean

(class)

(interface)

byte-2727-1
short-215215-1
int-231231-1
long-263263-1
float1.4E-45~3.4E+38 , -1.4E-45~3.4E+38
101*22 , 101*2-3
double4.9E-324~1.7E+308, -4.9E324~-1.7E+308
char0216-1unicode

Booleantruefalse

byteshortcharint
longlong
floatfloat
doubledouble

System.out.println(a+1)
System.out.println(+a+1)

byte short char intlongfloat double

shortchar

int a=256;
byte b=(byte)a; //b0

byte b=1;
int i=b;
short s=(short)i;
char c=(char)s;

a) float f=1.3;
b) char c="a";
c) byte b=257;
d) boolean b=null;
e) int i=10;
f) short s=1; char cs=s;

+String

String
toString()

String s1= " Hello"+ "World";


String
String
String
String
String

//s1= "HelloWorld"

s2="Age"+1; //s2= "Age:1"


s3="Age"+new Integer(1); //s3= "Age:1"
s4="Answer :" +true; //s4= "Answer:true"
s5="Answer :" +new Boolean("true"); //s5= "Answer:true"
s6=5+1+ "1" +new Integer(1)+ 2 +4+ new Long(11);

System.out.println(5+1+"1"+new Integer(1)+ 2 +4+ new Long(11)); //6112411

15+1 6
//
26+"1" "61"
//
3"61"+new Integer(1) "611" //
4"611"+2 "6112"
//
5"6112"+4 "61124"
//
6"61124"+new Long(11) "6112411" //

public class TestScope


{
public static void main(String[] args)
{
int x = 12;
{
int q = 96; // xq
int x = 3;//Java
System.out.println("x is "+x);
System.out.println("q is "+q);
}
q = x;
/* x q */
System.out.println("x is "+x);
}

1 2.

return

void

return


public class Test
{
public static void main(String [] args)
{
int isum;
double fsum;
isum=add(3,5);
isum=add(3,5,6);
fsum=add(3.2,6.5);
}
public static int add(int x,int y)
{
reutrn x+y;
}
public static int add(int x,int y,int z)
{
return x+y+z;
}
public static double add(double x,double y)
{
return x+y;
}
}

+
x+123;x123

5%-2=1
/

int x=3510;x=x/1000*1000;
3000

1
x6

(x+5)/6
x6

2x09

int x=0;
while(true)
{
x = (x+1)%10;
}

1JAVA
x=y=z=5;
5
2 x += 3x = x + 3*= -=/=

1booleantruefalse
2===

1boolean
boolean
2&&&&
&&false
|||&&&

public class TestAnd


{
public static void main(String[] args)
{
int x=0;
int y=3;
if(x!=0 && y==y/x)
System.out.println("y = "+y);
}
}
while

&

&,|,^

1&1
0
0|0
1
^1
0

java

<<
>>
>>>

public class ShiftTest


{
public static void main(String [] args)
{
int x=0x80000000;
int y=0x80000000;
x=x>>1;
y=y>>>1;
System.out.println(0x80000000>>1 = + Integer.toHexString(x));
System.out.println(0x80000000>>>1 = +
Integer.toHexString(y));
}
}

0x80000000>>1 = c0000000
0x80000000>>>1 = 40000000

0x8000000016 =1000,0000, 0000, 0000, 0000, 0000, 0000,


00002
0xc000000016=1100,0000, 0000, 0000, 0000, 0000, 0000,
00002
0x4000000016 =0100,0000, 0000, 0000, 0000, 0000, 0000,
00002

>>
>>>
<<
(short circuit)
+

== equals()
(File,Date,String,equals())

int
int
int
int

a1= 12 >>1; //a16


a2=-12 >> 2; //a2-3
a3= 128 >> 2; //a332
a4= 129 >> 2; //a432

int a1= 12 >>>1; //a16


int a2=-12 >>>2; //a21073741821

int
int
int
int

a1= 12 << 1; //a124


a2=-12 << 2; //a2-48
a3= 128 << 2; //a3512
a4= 129 << 2; //a4516

&&||short circuit&

What will happen when you attempt to compile and run the following code
//1
int output=10;
boolean b1 = false;
if((b1==true) && ((output+=10)==20)){
System.out.println("We are equal "+output);
}else {
System.out.println("Not equal! "+output);
}
//2
int output=10;
boolean b1 = false;
if((b1==true) & ((output+=10)==20)){
System.out.println("We are equal "+output);
}else {
System.out.println("Not equal! "+output);
}

output=output+10

byteshortcharintlong
intint
inta>>bb32
a>>33a>>1
a>>32a
longa>>b b64

a>>1

x>>1x/2x<<2x*4
n
2nn2n
2xy =
1<< x

int a =2;int b = a + 3*a;


int a =2;int b = a + 3*a;

a =2; int b= a + 3 * a++;


b

int

a =2; int b= (a ++)+ 3 *


a;b

int

11

if ()
if () 1 else 2
if (1) 1
else if (2) 2
else if (2) 3

else n
{ }
1:2
If
if (1)
if (2) 1
else2
esle
if (2) 3
else4
{}

Switch ()
{
case 1:1;
case 2:2;

case n:n;
default: n;
}
default

case
switchint, byte, char, short

case
casecase
break
case

While ()
int x=1;
while(x<3)
{
System.out.println("x="+x);
x++;
}
while;

do
while ();

public class TestDo


{
public static void main(String[] args)
{
int x=3;
while(x==0)
{
System.out.println("ok1");
x++;
}
int y=3;
do
{
System.out.println("ok2");
y++;
}
while(y==0);
}
}

q
System.in.read()

do-while
while

for(1;2 ;3)
for(int x=1;x<10;x++)
{
System.out.println("x="+x);
}
int x=1;
for(;x<10;x++)
{
System.out.println("x="+x);
}
int x=1;
for(;x<10;)
{
System.out.println("x="+x);
x++;
}
int x=1;
for(;;)
{
if(x >= 10)
break;
System.out.println("x="+x);
x++;
}

breakswitch
break()
(whiledoforSwitch)

st:while(true)
{
while(true)
{
break st;
}
}

continue(while,do
for)continue

continue 0
10
for(int i=0;i<10;i++)
{
if(i%2==0)
continue;
System.out.println(i);
}


int x=1;
if (x) // x is int

int x=1;
if (x ! = 0)

public void amethod(int x){


if(x>0){
System.out.println("0");
}else if(x==0){
System.out.println("0");
}else if(x<0){
System.out.println("0");
}
}

int x=9;
final int Y=2;
switch(x){// int
default: System.out.println(default);
case 1: System.out.println(case1);
case Y: System.out.println(caseY);
}

int x=1;
final int Y=2;
switch(x){// int
default: System.out.println(default);
case 1: System.out.println(case1);
break;
case Y: System.out.println(caseY);
}

default
case1
caseY

case1

/while
int i=0;
while(i++<3)
System.out.println(i);
}
//do-while
int i=1;
do{
System.out.println(i);
}while(i++<3);
//for
for(int i=1;i<=3;i++)
System.out.println(i);

1
2
3

breakswitchwhile
1100
int a=1,result=0;
one: while(true){
result+=a++;
if(a==101)break one; //
}
System.out.println(result); //5050

continuefor
1100
int result=0;
for(int a=1;a<=100;a++){
if(a%2==0)continue; //a
else result+=a;
}
System.out.println(result); //2500

int [] x;
x = new
int[100];
x=null;

int ia[] ={1,2,3,4};


int [] ia=new int[]{3,4,5};
int a[5];//

java

Java

int[][] xx;
xx=new int[3][];
xx[0]=new int[3];
xx[1]=new int[2];

x[1][2]

int xx[][]=new int[2][3];



int[][] xx={{3,2,7},{1,5},{6}};

System.arraycopy()
Arrays.sort

You might also like