JAVA语言基础部分

1.语言基础

二进制操作 

“&”按位与:a与b同时为1结果为1,否则为0;

“|”按位或:a与b其中任一个为1,否则为0

“~”按位取反

“^”按位与或:a与b值相同(同为0或1)时为0,否则为1

“<<”左移:右边以0来补充

“>>”右移:左边第1位相同值来补全,右边抛弃

“>>>”无符号右移:左边高位以固定值0补全

技巧:一个数左移n位,结果为这个数乘以2n ; 一个数右移n位,结果为这个数除以2n

 

2.循环控制

foreach写法

int[] ids=new int[]{1,2,3};

for (int i : ids) {
     System.out.print(i);
}
 
循环退出break是退出内层,多层的循环退出,可使用:break 标签;
first_label: for(int i=0;i<10;i++) {
		 for(int j=0;j<12;j++) {
			 if(j==10) {
				 break first_label;
			 }
		 }
	 }

 

3.字符串

字符串对比可以==,但字符对象要用equals或equalsIgnoreCase

                String name="a";
		String name2="a";
		System.out.print(name==name2);//结果true
		
		String w=new String("a");
		String w2=new String("a");
		System.out.print(w==w2);//结果false
		System.out.print(w.equalsIgnoreCase(w2));//结果true

replace字符时要注意大小写,区分大小写的;

split可以限制切割次数:aString.split(",",2);

格式化:String s=String.format("格式",变量);

image

image

image

image

猜你喜欢

转载自www.cnblogs.com/evemen/p/9786768.html