java基础知识--03

java基础知识

 

Math.round()

返回离它最近的整数,如果有两个,则返回最大那个

如:-11.5:返回11   -11.51:返回-12

 

Math.ceil()

返回最大的整数值

如:11.1:返回12   -11.9返回-11

 

Math.floor()

返回最小的整数值

如:11.9:返回11    -11.1返回-12

 

Math.random()

返回0-1之前的小数

拓展:随机生成a~z之间的字符

System.out.println((char)('a'+Math.random()*26));

 

String str = "i"和String str = new String(“i”)

String str = "i":

编译阶段:如果常量池中没有油“i”,则在常量池创建字符串,如果有了就不创建了。然后在栈中开辟名为str的空间,保存“i”在常量池的中地址

创建0或1个对象

 

new String(“i”):

编译阶段:如果常量池中没有油“i”,则在常量池创建字符串,如果有了就不创建了。

运行阶段:在常量池中复制“i”字符串到堆里,在栈中保存“i”在堆里的地址

创建1或2个对象

String类的底层是基于char数组的

 

花括号{}、static{}、构造方法

static{}: 类加载时调用

花括号{}:构造方法被调用前调用,一个类可以有多个{}

构造方法:new对象时调用,类的方法名称可以与类名称一样


/**
 * ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
 * &  | |      | |   * *     * *   | |      | |  &
 * &  | |      | |    * *   * *    | |      | |  &
 * &  | -------- |     * * * *     | -------- |  &
 * &  | -------- |      *   *      | -------- |  &
 * &  | |      | |     * * * *     | |      | |  &
 * &  | |      | |    * *   * *    | |      | |  &
 * &  | |      | |  * *       * *  | |      | |  &
 * $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 */

/**
 * Created by HXH on 2021/3/24
 * @desc
 */
public class TestStatic {
    TestStatic(){
        System.out.println("构造方法:TestStatic");
    }
    public void TestStatic(){
        System.out.println("普通函数:TestStatic");
    }

    static {
        System.out.println("static块:TestStatic");
    }

    {
        System.out.println("代码块:TestStatic");
    }
}




/**
 * ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
 * &  | |      | |   * *     * *   | |      | |  &
 * &  | |      | |    * *   * *    | |      | |  &
 * &  | -------- |     * * * *     | -------- |  &
 * &  | -------- |      *   *      | -------- |  &
 * &  | |      | |     * * * *     | |      | |  &
 * &  | |      | |    * *   * *    | |      | |  &
 * &  | |      | |  * *       * *  | |      | |  &
 * $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 */

/**
 * Created by HXH on 2021/3/24
 * @desc
 */
public class TestStatic2 extends TestStatic {
    TestStatic2(){
        System.out.println("构造方法:TestStatic2");
    }

    static {
        System.out.println("static块:TestStatic2");
    }

    {
        System.out.println("代码块:TestStatic21");
    }
    {
        System.out.println("代码块:TestStatic22");
    }
    {
        System.out.println("代码块:TestStatic23");
    }
    public static void main(String[] args) {
        new TestStatic2().TestStatic();
    }
}

输出结果:

static块:TestStatic
static块:TestStatic2
代码块:TestStatic
构造方法:TestStatic
代码块:TestStatic21
代码块:TestStatic22
代码块:TestStatic23
构造方法:TestStatic2
普通函数:TestStatic

 

throws 和 throw 的区别:

throws:用在方法的后面的,表示这个方法可能抛出哪些异常,可以有多个异常,用逗号连接(可能是加s的原因)

throw:代码运行到某一处时,自己手动抛出异常,只能抛出一个,并且肯定会抛出,当前线程强制结束。

 

IO 输入流 输出流

输入还是输出,是站在内存的角度看的。就内存的角度而言,输入进来,就是把外部文件读取进内存的操作;输出去,就是把内存中的数据通过流的形式,传送到外部去,常常会生产文件,或放到一个流容器中。

字节流与字符流区别:

文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成。

 

输入流:

字节输入流:

字符输入流:

 

输出流:

字节输出流:

字符输出流:

参考资料,图片来源:https://blog.csdn.net/bjpowernode_com/article/details/113848634

 

 

try不一定要catch !!!

try捕获可能发生的异常,然后catch住,抛出来。

每次使用得都那么的理所当然,偶尔还会加个finally。

然而,今天在翻源码的过程中,偶然间发现,try还能这么用!

 private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
        this.localCache.putObject(key, ExecutionPlaceholder.EXECUTION_PLACEHOLDER);

        List list;
        try {
            list = this.doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
        } finally {
            this.localCache.removeObject(key);
        }

        this.localCache.putObject(key, list);
        if (ms.getStatementType() == StatementType.CALLABLE) {
            this.localOutputParameterCache.putObject(key, parameter);
        }

        return list;
    }

 

总结下:try的使用有以下形式:

        try {
            //可能有异常,如果有异常,我也不管
        }finally {
            //有没有异常都会执行
        }


        try {
            //可能有异常,有异常我就尝试获取它
        }catch (Exception e){
             //抛出异常
        }


        try {
             //可能有异常,有异常我就尝试获取它
        }catch (Exception e ){
            //抛出异常
        }finally {
            //有没有异常都会执行
        }

 

而且,idea也有提示的,还是每次都没注意到。。。

 

posted @ 2021-03-21 20:56  q彩虹海q  阅读(29)  评论(0)    收藏  举报