静态导入

在JDK 1.5之后提供了静态导入功能,那么什么叫静态导入呢?如果一个类中的方法全部是使用static声明的静态方法,则在导入的时候就可以直接使用“import static”的方式导入,导入的格式如下:

import static 包.类.* ;

 

//静态导入包

import static java.lang.System.out;

import static com.jy.test.Person.show;

 

public class Test {

 

       public static void main(String[] args) {

              System.out.println("adfasdfa");

              out.println("aaaaaaaaa");

              //调用person中的方法

              show();

       }

}

断言

在JDK 1.4之后,Java中增加了断言的功能,那么什么叫断言呢?断言就是肯定某一个结果的返回值是正确的,如果最终此结果的返回值是错误的,则通过断言检查肯定会为用户提示错误信息。断言的定义格式如下所示:

assert boolean表达式 ;

assert boolean表达式 : 详细的信息

 

public class Test {

       public static void main(String args[]){

              int[]  x= {1,2,3} ;         // 定义一个数组,长度为3

              assert  x.length == 0 ;             // 此处断言数组长度为0,肯定是错误的

       }

}

 

 

编译程序:javac Test.java

验证程序:java –ea Test

String的常用操作:

public class str {

    //字符串与字符串数组的转换

    public static void main(String[] args) {

        //字符串与字符串数组的转换
        String test1 = "12adc32";
        char [] arr1 = test1.toCharArray();
        System.out.println(arr1);

        //从字符串中取出指定位置的字符,一个汉字占两个字节
        System.out.println(test1.charAt(2));

//        把一个字符串变为byte数组,也可以吧一个byte数组变成字符串
        byte[] byte_test1 = test1.getBytes();
        System.out.println(byte_test1);
        //再转回去,用构造方法
        String ss = new String(byte_test1,0,byte_test1.length);
        System.out.println(ss);

        //是否包含某个字段
        System.out.println(test1.contains("c3"));

        //去掉左右空格
        String test2 = "  fss daf  ";
        System.out.println(test2.trim());

        //替换
        System.out.println(test2.replaceAll(" ",""));

        //截取字符串
        String test3 = "123456";
        System.out.println(test3.substring(0,2));
        System.out.println(test3.substring(0));//直到末尾

        //拆分字符串,如果是"|","."需要写成\\|,\\.的形式
        String test4 = "fasdf twetwe safasd";
        String [] str_spl = test4.split(" ");
        System.out.println(Arrays.toString(str_spl));

        //查找一个指定的字符串是否存在“你好”
        boolean flag = test4.contains("fasdf");
        System.out.println(flag);//true

        //字符串大小写转换
        String test5 = "Zym";
        test5 = test5.toLowerCase();
        System.out.println(test5);
        System.out.println(test5.toUpperCase());

        //判断是否以指定的字符串开头或者结尾
        System.out.println(test5.startsWith("Z"));
        System.out.println(test5.endsWith("m"));

        //不区分大小写比较
        String test6 = "ZYM";
        boolean b6 = test5.equalsIgnoreCase(test6);
        System.out.println(b6);

        //     返回指定字符在此字符串中第一次出现处的索引。
        String sr="aaaaaabbcc";
        System.out.println(sr.indexOf("a")); //如果不存在查找的字符,返回-1
//         返回指定字符在此字符串中最后一次出现处的索引。
        System.out.println(sr.lastIndexOf("a"));
//        当且仅当 length() 为 0 时返回 true。
        System.out.println(sr.isEmpty());


    }

}

StringBuffer

线程安全

当一个字符串的内容需要被经常改变时就要使用StringBuffer

在StringBuffer中使用append()方法,完成字符串的连接操作

StringBuffered是线程安全的字符串操作,效率低;

public synchronized StringBuffer append(String str) {

        super.append(str);

        return this;

}

这样写的方法,每次操作后方法返回的都是自身类型this,都可以使用链式访问!!!

public class str {

    static StringBuffer stringBuffer;

    public static void main(String[] args) {
        StringBuffer test = new StringBuffer("1111");

        test.append("222").append("3333");
        System.out.println(test.toString());

    }



}

SringBuilder类:

线程不安全,效率高!

        //reverse 字符串反转

               String ss="abc";

               StringBuffer sf1 = new StringBuffer(ss);

               System.out.println(sf1.reverse());

 

StringBuilder si = new StringBuilder(ss);

               System.out.println(si.reverse());

 

System工具类:

System工具类是一些与系统相关的属性和方法的集合,而且在System类中所有的属性都是静态的,要想引用这些属性和方法,直接使用System类调用即可。

public static void main(String[] args) {

              try {

                     int a=1;

                     System.exit(1);//退出JVM

              } catch (Exception e) {

                     e.printStackTrace();

              }finally{

                     System.out.println("finally");

              }

 

       }

 

退出系统后,及使是finally中的代码,也不会被执行!!!

对象生命周期:

新生代的对象,不会垃圾回收;

年老代:可以垃圾回收的;

Runtime

public class TestRunTime {

    public static void main(String[] args) {
               //创建实例
        Runtime rt = Runtime.getRuntime();
        //返回 Java 虚拟机试图使用的最大内存量。
        long maxMemory = rt.maxMemory();
        System.out.println(maxMemory);
        long freeMemory = rt.freeMemory();//空闲内存
        System.out.println(freeMemory);
        System.out.println(maxMemory-freeMemory);//使用内存大小
        
//        执行本机命令 .调用windows中的记事本
        try {
            rt.exec("notepad.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }
        

    }

}

Object类

clone :如果下代码写的是浅克隆,深刻克隆,需要编码完成;(了解)
//空内容的接口成为标志接口;
public class Person implements Cloneable{
    private String id;
    private int age;
    
    
    public Person() {
        super();
    }
    public Person(String id, int age) {
        super();
        this.id = id;
        this.age = age;
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    //重写Object继承下来的clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        
        return super.clone();
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", age=" + age + "]";
    }
    
    
}

public class Test {

    public static void main(String[] args) {
        try {
            Person zs = new Person("00123", 20);
            Person zz=zs;//zz 和zs都在指向new Person("00123", 20);
            Person zs1 =(Person) zs.clone();//出现了一个新的对象在内存中
            zs1.setId("123");
            System.out.println(zs);
            System.out.println(zs1);
            System.out.println(zs==zs1);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        

    }
}

Objects

Jdk7新增了一个Objects工具类,提供了一些静态方法操作对象,这些方法是“空指针安全的”,比如你不确定一个引用变量是否为null,调用该引用变量的toString()方法会引发NullPointerException异常,如果使用Objects工具类提供的toString()方法就不会引发空指针异常
public static void main(String[] args) {
        TestObjects tm=null;
        
//        System.out.println(tm.toString());
        String str = Objects.toString(tm);
        System.out.println(str);

    }

Math类:

public static void main(String[] args) {
        // π
        System.out.println(Math.PI);
        //返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
        System.out.println(Math.random());
        //  返回两个 int 值中较大的一个。
        System.out.println(Math.max(10, 11));
        //返回两个 int 值中较小的一个。
        System.out.println(Math.min(10, 11));
        
        
        double num=12.4691d;
        //round,返回最接近参数的 long。
        System.out.println(Math.round(num));//四舍五入
        //floor  返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
        System.out.println(Math.floor(num));//去掉小数部分
        //ceil(double a)  返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
        System.out.println(Math.ceil(num));//去掉小数部分,整数部分+1
    }

Date类:

Date类是一个相对较为简单的操作类,在使用中直接使用java.util.Date类的构造方法并进行输出就可以得到一个完整的日期 。
public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
        // 格式化时间
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SS");
//        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        DateFormat df = new SimpleDateFormat("yy年MM月dd日 HH时mm分ss秒");
        String str=df.format(date);
        System.out.println(str);
        
        //获得毫秒数
        long dd = System.currentTimeMillis();
        System.out.println(dd);
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = d.format(dd);
        System.out.println(format);
        
        
    }

Calendar类(日历)

public static void main(String[] args) {
        //获得系统时间
        Calendar c = Calendar.getInstance();//实例化
        System.out.println(c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DAY_OF_MONTH));
        //时间设置
        c.set(1990, 3, 12, 16, 19, 30);
        System.out.println(c.getTime());
    }

NumberFormat类

NumberFormat表示数字的格式化类, 
    即:可以按照本地的风格习惯进行数字的显示。 
    public static void main(String[] args) {
//        DecimalFormat df = new DecimalFormat("###,###,###.###");
//        DecimalFormat df = new DecimalFormat("¥###,###,###.###");
//        DecimalFormat df = new DecimalFormat("###,###,###.###%");
        DecimalFormat df = new DecimalFormat("###,###,###.###\u2030");
//        DecimalFormat df = new DecimalFormat("000,000.000");
        System.out.println(df.format(12365.1236d));

 

posted on 2019-05-02 07:44  睡觉的阿狸  阅读(143)  评论(0编辑  收藏  举报