• Java高级特性 实用类


    枚举enum   

    枚举是一个类,就是一个引用数据类型

    枚举类中根据需求定义多个枚举值,

    枚举值一旦被定义,不允许被改变,静态的常量

      01.在Student类中定义Gender类型的sex私有属性:

       private Gender sex;

       02.创建Gender的枚举 

    public enum Gender{

    MAN("男"),WOMEN("女");   

    private String sex;

    private Gender(String sex){
    this.sex=sex;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

       }

      03.在测试类中调用

    stu.setSex(Gender.MAN)

    String sex = stu.getSex().getSex();
    System.out.println(sex);//控制台输出--->男

    1.我们是用的封装不能从根本上禁止用户的输入

    2.使用枚举,从根本上限定用户的输入

    3.所有的枚举值都是静态常量,可以通过枚举类.枚举值

    4.枚举类中构造方法,必须是私有的 private

    基本数据类型的包装类

    1.集合中的泛型,<>不允许出现基本数据类型,包装类可以

    2.定义了一个基本数据类型的变量,变量名能点出来东西

    3.基本数据类型不能转换成对象,包装类可以

    4.所有的包装类都是由final修饰的,不允许被继承

    5.在基本数据类型需要转换成对象的时候,使用包装类

    6.jdk1.5以后,允许基本数据类型和包装类进行混合运算,底层有装箱和拆箱操作

    基本数据类型    包装类
    byte          Byte
    short         Short
    int          Integer
    long          Long
    float         Float
    double        Double
    上面的六种都是数值类型!都是 extends Number implements Comparable<T>

    public abstract class Number implements java.io.Serializable 支持 序列化


    char Character
    boolean Boolean

    上面两种都是 implements java.io.Serializable, Comparable<T>

    比如说:
    public Serializable getNum(Serializable s){
    }

    我们调用getNum(参数可以是8种包装类中的任何一个)
    返回值也可以是 8种包装类中的任何一个

    装箱和拆箱--->包装类和基本数据类型的转换

        01.装箱:把基本数据类型转换成对应的包装类Integer num=1

        02.拆箱:把包装类转换成对应的基本数据数据类型 int num2=num

    基本数据类型转换:

       01.自动类型转换

       02.强制类型转换

    引用数据类型转换

      01.向下转换

      02.向上转换

    /**
    * 1.所有的包装类都有对应的基本数据类型作为参数,来构造自己的实例
    */
    @SuppressWarnings("unused")
    @Test
    public void Test01(){
    Byte b=new Byte((byte) 1);
    Short s=new Short((short) 1);
    Integer i=new Integer(1);
    Long l=new Long(1);
    Float f=new Float(1.0);
    Double d=new Double(1.0);

    Character c=new Character('1');
    Character c2=new Character((char) 20);
    Boolean bo=new Boolean(true);
    }
    /**
    * 1.Float有三种实例化的方法参数分别是double float 和String
    * 2.除了Character以外的的7种包装类都有将String 作为参数 来构建自己的实例
    * 6种数值类型的的包装类都继承了Number
    * 所以在使用String作为参数来创建自己的实例时
    * 如果参数不能转换成数值 则抛出NumberFormatException
    */
    @SuppressWarnings("unused")
    @Test
    public void Test02(){
    Byte b=new Byte("1");
    Short s=new Short("1");
    Integer i=new Integer("1");
    Long l=new Long("1");
    Float f=new Float("1");
    Double d=new Double("1");

    //Character c=new Character("1");
    Boolean bo=new Boolean("1");
    Boolean bo2=new Boolean(true);
    System.out.println(bo+" "+bo2);
    }
    /**
    * 1.除了Character以外的的7种包装类都有parseXxx(String s)
    * 比如说Byte b=new Byte("1");
    * b.parseByte(String);
    * 01.4种整型对应的包装类都是parseXxx(String s,int radix)radix进制转换
    * 02.其他4种没有parseXxx(String s,int radix)
    * 03.Character压根没有parseXxx()
    */
    @SuppressWarnings("unused")
    @Test
    public void Test03(){
    Byte b=new Byte("1");
    Short s=new Short("1");
    Integer i=new Integer("1");
    Long l=new Long("1");

    Float f=new Float("1");
    Double d=new Double("1");
    Character c=new Character('1');
    Boolean bo=new Boolean("true");

    System.out.println(bo);
    }
    /**
    * 4.进制转换 需要学习位运算
    */
    @Test
    public void Test04(){
    System.out.println("2进制的10对应的数据----->"+Integer.toBinaryString(10));
    System.out.println("8进制的10对应的数据----->"+Integer.toOctalString(10));
    System.out.println("16进制的10对应的数据----->"+Integer.toHexString(10));
    }
    /**
    * 5.valueOf
    * 把基本数据类型转换成对应的包装类---->装箱
    * xxxValue 8中包装类型都有
    * 把xxx类型转换成对应的基本数据类型--->拆箱
    */
    @Test
    public void Test05(){
    int num=3;
    Integer i=Integer.valueOf(num);
    num=i.intValue();
    }
    /**
    * 6.经典的面试题
    * 因为Integer.valueOf(num)会缓存-128~127之间的数据
    * 如果我们的数据在这个区间,不回去创建新的对象,而是从缓存中获取
    * 否则会new Integer()
    */
    @Test
    public void Test06() {
    int num1=127;
    int num2=127;
    System.out.println(num1==num2);
    Integer a1=new Integer(127);
    Integer b1=new Integer(127);
    System.out.println(a1==b1);
    Integer a=127;
    Integer b=127;
    System.out.println(a==b);
    Integer c=128;
    Integer d=128;
    System.out.println(c==d);
    System.out.println("1"+1+1);
    }

    /**
    * 111
    * 211
    */

    @Test
    public void Test07() {
    System.out.println("1"+1+1);
    System.out.println(1+1+"1"+1);
    }

    Math
    01.就是一个算术类
    02.是final修饰
    03.除了构造方法之外所有的方法都是静态方法,方便我们使用

    天花板函数:
    ceil 天 向上取整 3.1 ==》4 3.0 ==》3
    floor 地 向下取整 3.9 ==》3

    四舍五入
    round(2.5)==>3
    round(2.498)==>2

    Random
    随机的boolean 数值    random.nextBoolean()

    随机0-1之间的小数     random.nextDouble()

    随机int类型的整数     random.nextInt()

    随机int类型指定的整数  random.nextInt(10)// 返回的是0-10以内  不包含10

     String

    /**
    * 大小写转换
    * 字符串的长度 比较equals  忽略大小写的比较equalsIgnore

    */

    @Test
    public void test01(){
    String str1="hello";
    String str2="HELLO";
    System.out.println("小写变成大写"+str1.toUpperCase());
    System.out.println("大写变成小写"+str2.toLowerCase());
    System.out.println("字符串的长度"+str1.length());
    System.out.println("hello.equals.HELLO----->"+str1.equals(str2));
    System.out.println("hello.equalsIgnoreCase.HELLO----->"+str1.equalsIgnoreCase(str2));
    }
    /**
    * 字符串转换成char类型的数组
    */
    @Test
    public void test02(){
    String str1="h e l l o";
    char[] strs = str1.toCharArray();
    System.out.println(strs.length);//9
    for (char c : strs) {
    System.out.println(c);
    }
    }
    /**
    * 字符串拆分 String regex 正则表达式
    */
    @Test
    public void test03(){
    String str1="h1+e2+l3+l4+o5";
    String[] strs = str1.split("\+");//有+号的地方进行拆分,不显示+号
    for (String c : strs) {
    System.out.println(c);
    }
    }
    /**
    * 查询指定字符的位置 下标从0开始
    */
    @Test
    public void test04(){
    String str1="123456@qq.com.cn";
    System.out.println("@出现的位置"+str1.indexOf("@"));
    System.out.println(".最后一次出现的位置"+str1.lastIndexOf("."));
    }
    /**
    * 截取字符串 指向拿到qq.com
    */
    @Test
    public void test05(){
    String str1="123456@qq.com.cn";
    int begin = str1.indexOf("@");
    int end = str1.lastIndexOf(".");
    str1 = str1.substring(begin+1, end);//begin包含当前位置,end不包含当前位置
    System.out.println(str1);
    }
    /**
    * 替换字符串 把qq换成163
    */
    @Test
    public void test06(){
    String str1="123456@qq.com.cn";
    str1 = str1.replace("qq", "163");
    System.out.println(str1);
    }
    /**
    * 返回字符串中指定位置的字符
    */
    @Test
    public void test07(){
    String str1="123456@qq.com.cn";
    char s=str1.charAt(6);
    System.out.println(s);
    }
    /**
    * 连接字符串concat(String)
    */
    @Test
    public void test08(){
    System.out.println("1"+11+1);//1111
    System.out.println(11+1+"1"+5.0+"a");//1215.0a
    String str1="a";
    String str2="bc";
    System.out.println(str1.concat(str2));
    }
    /**
    * 判断某个字符串中是否包含另一个完整的字符串
    */
    @Test
    public void test09(){
    String str1="abcdef";
    boolean b = str1.contains("def");
    System.out.println(b);
    }

    /**
    * String StringBuffer StringBuilder
    * String str1 = "abcdefg";
    str1 = "abc"; // 每次都是一个新对象

    01.String对象不可变
    02.StringBuffer StringBuilder对象可变
    03.StringBuffer线程安全 但是 效率比StringBuilder低 ,适合于多线程的情况下使用
    04.StringBuilder 线程不安全,但是效率是最高的! 适合于单线程的情况下使用
    */

    @Test
    public void test10(){
    String str1="hello";// 定义一个变量
    int num = 1000000; // 定义操作字符串的次数
    // 设置开始时间
    long begin = System.currentTimeMillis();
    for (int i = 0; i < num/100; i++) {
    str1+="bye";
    }
    long end = System.currentTimeMillis();
    System.out.println("String操作1w次执行的时间是:"+(end-begin));
    // 使用StringBuffer
    str1="hello";
    StringBuffer sb=new StringBuffer(str1);
    //设置开始时间
    begin=System.currentTimeMillis();
    for (int i = 0; i < num; i++) {
    sb.append("bye");
    }
    end=System.currentTimeMillis();
    System.out.println("StringBuffer操作100w次执行的时间是:"+(end-begin));
    // 使用StringBuilder
    str1="hello";
    StringBuilder sb2=new StringBuilder(str1);
    //设置开始时间
    begin=System.currentTimeMillis();
    for (int i = 0; i < num; i++) {
    sb2.append("bye");
    }
    end=System.currentTimeMillis();
    System.out.println("StringBuilder操作100w次执行的时间是:"+(end-begin));
    }

  • 相关阅读:
    SQL Server 2005 出现“此数据库没有有效所有者”错误的解决方法
    使用swfupload出现SecurityError Error #2156问题
    读取Excel表
    POJ 1953 (DP)
    POJ 1050 (DP)
    POJ 1276 (DP)
    POJ 1579 (DP)
    HDOJ 4223 (DP)
    POJ 1080 (DP)
    POJ 1458 (DP)
  • 原文地址:https://www.cnblogs.com/s10-/p/8108883.html
Copyright © 2020-2023  润新知