java中的一些小语法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheidou123/article/details/60470168

目录:

一 for循环

二 switch

三 try catch finally

四 &和&& |和||

五 floor cell round

六 数字与字符串相加

七 instanceof运算符

八运算符优先级

九位运算符

十移位运算符

十一Assert

十二StringUtils常用方法

十三System.gc和System.runFinalization区别

一:for循环

1.for(int i=0;i<10;i++){}循环的执行顺序:

int i=0;初始化这一步只执行一次;

i<10;进行条件判断;条件达成返回true 否则false不往下执行,跳出for循环圈

i++ ; 是最后执行的,当循环体内的代码执行完它才进行赋值。

2.跳出多重for循环:

①方式1:使用label标签

package dxc1;
public class Test123 {
	 public static void main(String[] args) {
		heidou:
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				  System.out.println("i是"+i+"j是"+j);
				  if(i==2&&j==3){
					  break heidou;
				  }
			}
	  		}}
}

输出:

i是0j是0
i是0j是1
i是0j是2
i是0j是3
i是0j是4
i是1j是0
i是1j是1
i是1j是2
i是1j是3
i是1j是4
i是2j是0
i是2j是1
i是2j是2
i是2j是3

②方式2

在外面加个条件

 int arr[][] = { { 1, 2, 3 }, { 4, 5, 6, 7 }, { 9 } };
        boolean found = false;

        for (int i = 0; i < arr.length && !found; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println("i=" + i + ",j=" + j);
                if (arr[i][j] == 5) {
                    found = true;
                    break;
                }
            }
        }

二、switch

支持int,char,byte,short,枚举,String(JDK1.7以上)类型

package dxc1;
public class Test123 {
	public static void main(String[] args) { 
		int i=3; 
		switch(i) 
		{ 
		  
		   case 1: 
		       System.out.println(1); 
		       break; 
		   case 2: 
		       System.out.println(2); 
		       break; 
		   case 3: 
		       System.out.println(3); 
		       break;  //屏蔽点
		   default: 
		       System.out.println("default"); 
	    	} 
}}

当屏蔽点未屏蔽时,输出:

3

否则输出:

3
default

看俩特殊情况:

package dxc1;
public class Test123 {
	public static void main(String[] args) { 
		int i=3; 
		switch(i) 
		{ 
		   case 1: 
		       System.out.println(1); 
		       break; 
		   case 3: 
		       System.out.println(3); 
		   default: 
		       System.out.println("default");
		   case 2: 
		       System.out.println(2); 
		       break; 
	    	} 
}}

输出:

3
default
2
package dxc1;
public class Test123 {
	public static void main(String[] args) { 
		int i=3; 
		switch(i) 
		{ 
		   case 1: 
		       System.out.println(1); 
		       break; 
		   default: 
		       System.out.println("default");
		   case 3: 
		       System.out.println(3); 
		   
		   case 2: 
		       System.out.println(2); 
		       break; 
	    	} 
}}

输出:

3
2

三、try catch finally

①除非有System.exit(int status)否则都会执行,

这里补充个知识点:

system.exit(0):正常退出,程序正常执行结束退出
system.exit(1):是非正常退出,就是说无论程序正在执行与否,都退出,

②finally{}代码块比return先执行。
③多个return是按顺序执行的的,多个return执行了一个后,后面的return就不会执行了。
④记住一点,不管有不有异常抛出, finally都会在return返回前执行。

看个例子:

package dxc1;
public class Test123 {
	public static void main(String[] args) {
      try{
    	  int i = 100/ 0;
          System.out.print(i);
        }catch(Exception e){
        	System.out.print(1);
            throw new RuntimeException();
    	  }finally{
           System.out.print("我是异常");
	       }
         System.out.print("小尾巴");
  }
}
 

输出:

我是异常我是finallyException in thread "main" java.lang.RuntimeExceptionat dxc1.Test123.main(Test123.java:9)

说明catch和finally执行了

还有个很重要的例子:

public class Testfinal { 
public static void main(String[] args) { 
System.out.println(heidou(5)); 
} 

private static int heidou(int i) { 
int a =5; 
try { 
return a; 
} 
finally{ 
a++; 
System.out.println("finally"+a); 
} 
} 

输出:

输出:                 
finally6
5
虽然 finally改变了a的值,但是输出依旧是没改变前的。

四、&和&& |和||

按位与:a&b是把a和b都转换成二进制数然后再进行与的运算;(都会运行)
逻辑与:a&&b就是当且仅当两个操作数均为 true时,其结果才为 true;只要有一个为false后面就不执行了
例如:
a&b 9&8
1001
1000
结果是1000
a&&b 9&&8 结果是1
同理 | 和||也是一个意思

五、floor cell round

floor: 求小于参数的最大整数。返回double类型-----n. 地板,地面
例如:Math.floor(-4.2) = -5.0
-----------------------------------------------------------
ceil: 求大于参数的最小整数。返回double类型-----vt. 装天花板;
例如:Math.ceil(5.6) = 6.0
-----------------------------------------------------------
round: 对小数进行四舍五入后的结果。返回int类型
例如:Math.round(-4.6) = -5

六、数字与字符串相加

System.out.println(1+"10"+3+"2");//11032

System.out.println(1+2+"10"+3+"2");//31032

System.out.println(1+"10"+3+1+"2");//110312

七、instanceof运算符

package dxc1;

interface A{
    
}

class B implements A{
      
}

class C extends B{
      
}
public class testinstance {
 
    public static void main(String[] args) {
        A ab=new B();
        A ac=new C();
        B bc=new C();
        B bb=new B();
        C cc=new C();
        //对象实现一个接口,用这个对象和这个接口进行instanceof判断,都为true。
        System.out.println("ab instanceof A="+(ab instanceof A));
        System.out.println("ac instanceof A="+(ac instanceof A));
        System.out.println("bc instanceof A="+(bc instanceof A));
        System.out.println("bb instanceof A="+(bb instanceof A));
        System.out.println("cc instanceof A="+(cc instanceof A));
        //对象和父类进行instanceof判断,都为true
        System.out.println("ab instanceof B="+(ab instanceof B));
        System.out.println("ac instanceof B="+(ac instanceof B));
        System.out.println("bc instanceof B="+(bc instanceof B));
        System.out.println("bb instanceof B="+(bb instanceof B));
        System.out.println("cc instanceof B="+(cc instanceof B));
        //对象和他的子类进行instanceof判断为false
        System.out.println("ab instanceof C="+(ab instanceof C));
        System.out.println("ac instanceof C="+(ac instanceof C));
        System.out.println("bc instanceof C="+(bc instanceof C));
        System.out.println("bb instanceof C="+(bb instanceof C));
        System.out.println("cc instanceof C="+(cc instanceof C));
    }
}
ab instanceof A=true
ac instanceof A=true
bc instanceof A=true
bb instanceof A=true
cc instanceof A=true
ab instanceof B=true
ac instanceof B=true
bc instanceof B=true
bb instanceof B=true
cc instanceof B=true
ab instanceof C=false
ac instanceof C=true
bc instanceof C=true
bb instanceof C=false
cc instanceof C=true

八、运算符优先级

九、位运算符

分为:“与”、“或”、“非”、“异或”
1.与运算符:&
两个操作数中位都为1,结果才为1,否则结果为0
2.或运算符:|
两个位只要有一个为1,那么结果就是1,否则就为0
3.非运算符:~
如果位为0,结果是1,如果位为1,结果是0
4.异或:^
两个操作数的位中,相同则结果为0,不同则结果为1

十、移位运算符

1)<< ,左移位:将操作符左侧的操作数向左移动操作数右侧指定的位数。移动的规则是在二进制的低位补0.
2)>> ,有符号右移位,将操作符左侧的操作数向右移动操作数右侧指定的位数。移动的规则是,如果被操作数的符号为正,则在二进制的高位补0;如果被操作数的符号为负,则在二进制的高位补1,>>右移几位相当于除以二的几次方
3)>>> ,无符号右移位:将操作符左侧的操作数向右移动操作数右侧指定的位数。移动的对则是,无论被操作数的符号是正是负,都在二进制的高位补0.

十一、Assert

1.简介

assert关键字是从JAVA SE 1.4 引入的,默认不开启

当执行代码时,使用-ea选项使断言有效,也可以使用-da选项使断言无效(默认为无效),在正式环境应当避免使用

同样,也可以通过在-ea或-da后面指定包名来使一个包的断言有效或无效。例如,要使一个com.test包中的断言无效,可以使用:

-da:com.test

要使一个包中的所有子包中的断言能够有效或无效,在包名后加上三个点。例如:

-ea:com.test...

即可使com.test包及其子包中的断言无效。

2.常用语法:

Assert.notNull(Object object, "object is required")  ;  //对象非空
Assert.isTrue(Object object, "object must be true") ;   //  对象必须为true
Assert.notEmpty(Collection collection, "collection must not be empty");      // 集合非空
Assert.hasLength(String text, "text must be specified");   //    字符不为null且字符长度不为0
Assert.hasText(String text, "text must not be empty") ;   //   text 不为null且必须至少包含一个非空格的字符
Assert.isInstanceOf(Class clazz, Object obj, "clazz must be of type [clazz]") ;   //    obj必须能被正确造型成为clazz 指定的类

3.示例

package com.thinkgem.jeesite.common.service;

import org.springframework.util.Assert;

public class TEST {
	 public static void main(String[] args) {
		    String test="123";
	        Assert.notNull(test, "校验1出错")  ;  //对象非空
	        System.out.println("第一个校验");
	        String test1="";
	        Assert.notNull(test, "校验2出错")  ;  //对象非空
	        System.out.println("第二个校验");
            try {
	        String test3=null;
	        Assert.notNull(test3, "校验4出错")  ;  //对象非空
	        System.out.println("第三个校验");
            }catch(Exception e) 
            {
            	System.out.println("捕获第三个校验");
            }  
            String test4=null;
	        Assert.notNull(test4, "校验4出错")  ;  //对象非空
	        System.out.println("第四个校验");     
	    }
}

输出:

第一个校验
第二个校验
捕获第三个校验
Exception in thread "main" java.lang.IllegalArgumentException: 校验4出错
	at org.springframework.util.Assert.notNull(Assert.java:112)
	at com.thinkgem.jeesite.common.service.TEST.main(TEST.java:22)

十二、StringUtils常用方法

1. public static boolean isEmpty(String str)

判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true 
StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理
StringUtils.isEmpty("   ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false

2.public static boolean isNotEmpty(String str)

判断某字符串是否非空,等于 !isEmpty(String str)

StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("         ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true 

3.public static boolean isBlank(String str)

判断某字符串是否为空或长度为0或由空白符(whitespace) 构成

    StringUtils.isBlank(null) = true
      StringUtils.isBlank("") = true
      StringUtils.isBlank(" ") = true
      StringUtils.isBlank("        ") = true
      StringUtils.isBlank("\t \n \f \r") = true   //对于制表符、换行符、换页符和回车符

      StringUtils.isBlank()   //均识为空白符
      StringUtils.isBlank("\b") = false   //"\b"为单词边界符
      StringUtils.isBlank("bob") = false
      StringUtils.isBlank(" bob ") = false 

4.public static boolean isNotBlank(String str)

判断某字符串是否不为空且长度不为0且不由空白符(whitespace) 构成,等于 !isBlank(String str)

   StringUtils.isNotBlank(null) = false
      StringUtils.isNotBlank("") = false
      StringUtils.isNotBlank(" ") = false
      StringUtils.isNotBlank("         ") = false
      StringUtils.isNotBlank("\t \n \f \r") = false
      StringUtils.isNotBlank("\b") = true
      StringUtils.isNotBlank("bob") = true
      StringUtils.isNotBlank(" bob ") = true 

十三、System.gc和System.runFinalization区别

⑴System.gc();
//告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的

⑵System.runFinalization();
//强制调用已经失去引用的对象的finalize方法

这样用:


     System.gc();
     System.runFinalization();

要在runFinalization之前执行gc();

猜你喜欢

转载自blog.csdn.net/cheidou123/article/details/60470168