java位运算的优先级

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值