Java基础(二)

运算符

Java支持的运算符:优先级

算术运算符:+,-,*,/,%,++,--

赋值运算符:=

关系运算符:>,<,>=,<=,==,!=instanceof

逻辑运算符:&&,||,!

位运算符:&,|,^,~,>>,<<,>>>

条件运算符:?:

扩展赋值运算符:+=,-=,*=,/=

package operator;

public class Demo01 {
    public static void main(String[] args) {
        //二元运算符
        //Ctrl + D:复制当前行到下一行(推荐使用!!)
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 40;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);//需要强制转换才能保留小数
    }
}
package operator;

public class Demo02 {
    public static void main(String[] args) {
        long a = 1212121321654621L;
        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,类型转换:byte,short,char用运算符运算后自动转型为int类型
    }
}
package operator;

public class Demo03 {
    public static void main(String[] args) {

        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(c%a);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}
package operator;

public class Demo04 {
    public static void main(String[] args) {
        //++ -- 自增,自减 一元运算符
        int a = 3;

        int b = a++;//执行完这行代码后,先给b赋值,再自增
        int c = ++a;//执行完这行代码前,先自增,再给b赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //幂运算 2^3 math工具类
        double pow = Math.pow(2, 3);
        System.out.println(pow);
     }
}
package operator;

//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        //与或非
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:" + (a && b));
        System.out.println("a || b:" + (a || b));
        System.out.println("!(a && b):" + !(a && b));

        //短路运算
        int c = 5;
        boolean d = (++c < 6) && (c++ < 7);
        System.out.println(c);
    }
}
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);
        System.out.println(2 >> 3);
    }
}
package operator;

public class Demo07 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        a += b;
        a -= b;
        System.out.println(a);

        //字符串连接符 + , String
        System.out.println(""+(a+b));//字符串在前,后面都拼接,如果加了括号那就先计算括号中的
        System.out.println(a+b+"");//字符串在后,前面先计算,总之是一个优先级问题
    }
}
package operator;

public class Demo08 {
    public static void main(String[] args) {
        //x ? y : z
        //如果x==true,则结果为y,否则结果为z

        int score = 80;
        String type = score < 60 ? "不及格" : "及格";//必须掌握
        System.out.println(type);
    }
}

包机制

  • 包的本质就是文件夹,防止命名空间(namespace)重复
  • 一般利用公司域名倒置作为包名
  • 项目结构--齿轮--"Compact Middle Packages" 把勾去掉
  • 使用外部类报错,Alt+Enter选择导入包
  • import com.xxx.*(使用通配符全部导入)

JavaDoc

搜JDK帮助文档,到官网查文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。