java位运算的优先级如下(简单记录一下):
~的优先级最高,其次是<<、>>和>>>,再次是&,然后是^,优先级最低的是|。
public class Test
{
public static void main(String[] args)
{
int x = 8|9&10^11;
System.out.println(x);
}
}
输出为:
11
下面的这段代码输出啥呢?
public class Test extends BB
{
private int a = 1;
public Test(int a)
{
this.a = a;
System.out.println("Constucter Test");
}
public void draw()
{
System.out.println("Test.draw(), a = " + a);
}
public static void main(String[] args)
{
new Test(5);
}
}
class BB
{
private int a = 10;
public BB()
{
System.out.println("Constucter BB");
draw();
}
public void draw()
{
System.out.println("BB.draw(), a = " + a);
}
}
输出结果为:
Constucter BB
Test.draw(), a = 0
Constucter Test
下面的代码输出啥?
public class Test
{
public static void main(String[] args)
{
for(int i=0; i<5; i++)
{
switch(i)
{
case 0:
System.out.println(0 + "***" + i);
case 1:
System.out.println(1 + "***" + i);
case 2:
System.out.println(2 + "***" + i);
default:
System.out.println(4 + "***" + i);
break;
case 3:
System.out.println(3 + "***" + i);
}
}
}
}
输出为:
0***0
1***0
2***0
4***0
1***1
2***1
4***1
2***2
4***2
3***3
4***4
下面的代码又输出啥呢?
public class Test
{
public static void main(String[] args)
{
int i = 0;
for(foo('A'); foo('B') && i < 2; foo('C'))
{
i++;
foo('D');
}
}
public static boolean foo(char ch)
{
System.out.print(ch);
return true;
}
}
输出为:
ABDCBDCB