• 自动拆装箱/正则表达式


    String和int类型的相互转换

    int—>String

    //方式1
    String str = num + "";  //拼接空串
       //方式2 记忆这个
       //String 中 静态方法 valueOf(num) 可以把多种类型转换成字符串
    String s = String.valueOf(num);
            //方式3
    Integer integer = new Integer(num);
     String string = integer.toString();
     System.out.println(string);
    

    String—>int

              //方式1
            Integer integer1 = new Integer(strNum);
            int i = integer1.intValue(); //把包装类型转成他对应的基本类型
    
            //方式2 记这个方法
            int nun = Integer.parseInt(strNum);
    
    

    自动拆装箱

    自动装箱:将基本类型自动转换成他所对应的包装类型
    自动拆箱:将包装类型自动转换成他所对应的基本类型

    案例演示:

    public class MyTest2 {
        public static void main(String[] args) {
            Integer integer = new Integer(20);
            Integer integer2 = new Integer(200);
    
           int num2=integer+integer2; //自动拆箱
    
            //手动拆箱
            int i = integer.intValue();
            int i1 = integer2.intValue();
            int num=i+i1;
            System.out.println(num);
    
    
            int aa=200;
            Integer integer1=aa;  //自动装箱
    
            Integer integer3 = Integer.valueOf(200); //手动装箱
    
    
            //都完成了哪些操作?
            Integer ii = 100; //自动装箱
            ii += 200;//自动拆箱,自动装箱
    
    
        }
    }
    
    

    案例演示2:

    public class MyTest3 {
        public static void main(String[] args) {
            Integer i1 = new Integer(127);
            Integer i2 = new Integer(127);
            System.out.println(i1 == i2);//false
            //Integer类重写了equals方法比值是否相同
            System.out.println(i1.equals(i2));//true
            System.out.println("-----------");
    
            Integer i3 = new Integer(128);
            Integer i4 = new Integer(128);
            System.out.println(i3 == i4);//false
            System.out.println(i3.equals(i4));//true
            System.out.println("-----------");
    
            Integer i5 = 128; //自动装箱
            Integer i6 = 128;
    
            System.out.println(i5 == i6); //false
            System.out.println(i5.equals(i6)); //true
            System.out.println("-----------");
    
            Integer i7 = 127;
            Integer i8 = 127;
            System.out.println(i7 == i8); //true
        }
    }
    

    案例演示3:解释上述自动装箱的两个包装类一个是true一个是false

    public class MyTest4 {
        public static void main(String[] args) {
            //超过1个字节的范围
            Integer i5 = 128; //自动装箱
            Integer i6 = 128;
            Integer.valueOf(128);
            System.out.println(i5 == i6); //false
    
            //没有超过一个字节的范围
            Integer i7 = 127;
            Integer i8 = 127;
    
            System.out.println(i7 == i8); // true
    
            // Integer i7 = 127; 自动装箱  底层要调用
            //手动装箱
            Integer.valueOf(127);
    
        }
    }
    
    //valueOf()的源码
    public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    /*分析源码我们可以得出:i7.i8是自动装箱产生的对象,其值都是127,这里很特殊的是127正好在-128<=i7<=127这个范围内,所以会去IntegerCache中取,既然失去IntegerCache中去取,那么自然该对象应该是一个对象,所以在堆内存地址应该是一样的,所以在判读两个对象是不是==的时候。会输出true*/
    

    正则表达式

    正确规则的表达式,他通过特定的语法,书写出一些规则,用于对数据进行校验,正则表达式是一门独立的语法,很多语言都支持。

    规则字符在java.util.regex Pattern类中
    A:字符
    	x 字符 x。举例:'a'表示字符a
    	\ 反斜线字符。
    	
     新行(换行)符 ('u000A') 
    	
     回车符 ('u000D')
    B:字符类
    	[abc] a、b 或 c(简单类) 
    	[^abc] 任何字符,除了 a、b 或 c(否定) 
    	[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
    	[0-9] 0到9的字符都包括
    C:预定义字符类
    	. 任何字符。我的就是.字符本身,怎么表示呢? .
    	d 数字:[0-9]
    	w 单词字符:[a-zA-Z_0-9]
    		在正则表达式里面组成单词的东西必须有这些东西组成
    D:边界匹配器
    	^ 行的开头 
    	$ 行的结尾 
    	 单词边界
    		就是不是单词字符的地方。
    		举例:hello world?haha;xixi
    E:Greedy 数量词 
    	X? X,一次或一次也没有 比如""空串 就是没有
    	X* X,零次或多次  大于等于1次 都算多次
    	X+ X,一次或多次
    	X{n} X,恰好 n 次 
    	X{n,} X,至少 n 次 
    	X{n,m} X,至少 n 次,但是不超过 m 次 
    

    案例演示:用正则来截取字符串

    public class MyTest {
        public static void main(String[] args) {
    
            String str="我喜欢你=稀罕你=爱死你";
            String str2 = "我喜欢你=稀罕你=爱死你=我喜欢你=稀罕你=爱死你";
            // 根据正则来截取字符串
            String[] strings = str2.split("=");
            System.out.println(Arrays.toString(strings));
            System.out.println(strings[0]);
            System.out.println(strings[1]);
            System.out.println(strings[2]);
    
            String str3 = "我喜欢你=asdfa稀罕你=asdfs1212爱死你=23232我喜欢你=212121稀罕你=asdfasdfefefefefeffefasdf爱死你";
    
            String[] split = str3.split("[=a-z0-9]+");
            System.out.println(Arrays.toString(split));
    
        }
    }
    
  • 相关阅读:
    ASP.NET MVC EF 连接数据库(一)-----Database First
    设计模式——策略模式
    设计模式——简单工厂模式
    分享一些技术大牛的博客
    有货双中心双活架构实践
    分布式协调服务Zookeeper应用场景
    分布式服务框架资料汇总
    Java线程池ThreadPoolExecutor解析
    服务注册中心Zookeeper和Eureka比较
    JVM内存结构、垃圾回收及性能调优
  • 原文地址:https://www.cnblogs.com/godles/p/11983812.html
Copyright © 2020-2023  润新知