狂神说学Java-04基础语法学习(下)

时间:2021-05-24 13:24:18   收藏:0   阅读:0

基础语法学习

5.运算符

Java语言支持一下运算符

新建一个base包
技术图片
然后把这些类移动到base包中
创建一个operator包,在这个包中新建类

package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
    }
}
package operator;

public class Demo04 {
    public static void main(String[] args) {
        int a = 3;
        int b = a++;//执行完这行代码后,先给b赋值,再自增
        int c = ++a;//执行完这行代码后,先自增,再给c赋值
        System.out.println(a);//5
        System.out.println(b);//3
        System.out.println(c);//5
    }
}

注意短路特性

package operator;

public class Demo05 {
    public static void main(String[] args) {
        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);//false
        System.out.println(c);//5
    }
}

位运算

package operator;

public class Demo06 {
    public static void main(String[] args) {
         /*
        A = 0011 1100
        B = 0000 1101
        ----------------------
        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B = 1111 0010

        2*8 16 2*2*2*2
        位运算效率极高!!!
        <<相当于*2
        >>相当于/2
        */
        System.out.println(2<<3);
        /*
        0000 0010 是2
        0000 0100 是4
        0000 1000 是8
        0001 0000 是16
         */
    }
}

拓展:字符串连接符 +

package operator;

public class Demo07 {
    public static void main(String[] args) {
        //字符串连接符
        int a = 10;
        int b = 20;
        System.out.println(""+a+b);//"1020"
        System.out.println(a+b+"");//"30"
    }
}

6.包机制

一般使用公司域名倒置作为包名

javadoc

package com.brucezhang.base;

/**
 * @author brucezhang
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;

    /**
     * @param name
     * @return 
     * @throws Exception
     */
    public String test(String name) throws Exception{
        return name;
    }
}

在文件管理器中打开这个Doc的类
技术图片
在当前目录中打开cmd,运行命令
技术图片
打开生成的index.html

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!