栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

4 2021.10.9

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4 2021.10.9

运算符
  • 算数运算符:+ - * / % ++ --

  1. %:取余运算也叫模运算

public class Test3 {
    public static void main(String[] args) {
        int a=10;
        int b=3;
        //10除以3于1
        System.out.println(a%b);
​
    }
}
1
​
Process finished with exit code 0
​
  1. 加减乘除

public class Test1 {
    public static void main(String[] args) {
        //加减乘除
        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/(double)b);
        System.out.println(a*b);
​
    }
}
30
-10
0.5
200
​
Process finished with exit code 0
public class Test2 {
    public static void main(String[] args) {
        long a=12312414141441l;
        int b=123;
        short c=10;
        byte d=8;
        //输出的是long类型
        System.out.println(a+b+c+d);
        //输出的是int类型
        System.out.println(b+c+d);
        //输出的是int类型
        System.out.println(c+d);
​
    }
}
12312414141582
141
18
​
Process finished with exit code 0
​

运算的数中有一个数是double输出的是double,其余默认为int

  1. 自增,自减

public class Test4 {
    public static void main(String[] args) {
        int a=3;
        //先赋值给b在进行自增
        int b=a++;
        //先自增在赋值给c
        int c=++a;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}
5
3
5
​
Process finished with exit code 0
  • 赋值运算符:=

  1. a=b 把b的值赋值给a

  • 关系运算符:> < >= <= == !=

  1. == Java中的等于

  2. != Java中的不等于

  3. 关系运算符返回的结果: 正确 错误,布尔值

public class Test3 {
    public static void main(String[] args) {
        int a=10;
        int b=20;
        System.out.println(a>b);
        System.out.println(a=b);
        System.out.println(a<=b);
        System.out.println(a!=b);
    }
}
false
true
false
false
true
true
​
Process finished with exit code 0
  • 逻辑运算符:&& || !

  1. 表示与或非

public class Test6 {
    public static void main(String[] args) {
        //与或非
        boolean a=true;
        boolean b=false;
        //这里注意a&&b要加括号
        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<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}
a&&b:  false
a||b:  true
!(a&&b):  true
false
5
​
Process finished with exit code 0

与算法:两个变量为真结果才为true;或运算:两个变量有一个为真结果为true;非运算:如果为真则变为假,反之。

短路运算:在与运算中当两个变量进行比较前面一个变量为false则直接结束不进行后面的运算

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

  1. 位运算符多用与位与位之间的运算如A=0010 B=1010 A&B将A与B中每一位进行对比

  2. ^运算符

public class Test5 {
    public static void main(String[] args) {
        //幂运算 2^3 =8
        int a=2;
        //使用Math方法表示2的3次方很多运算要用到很多工具类
        double pow=Math.pow(2,3);
        System.out.println(pow);
​
    }
}
8.0
​
Process finished with exit code 0
  1. & | ^ ~运算符:

A=0011 1100 B=0000 1101 A&B =0000 1100 对应位都是1结果为1 A|B =0011 1101 对应位有一个都是1结果为1 A^B =0011 0001 对应位不同结果为1反之为0 ~B=1111 0010

4<< >> 运算符:面试题拓展怎么快速计算2*8

public class Test7 {
    public static void main(String[] args) {
        int a=2;
        
        System.out.println(2<<3);
​
    }
}
16
​
Process finished with exit code 0

右移表示除2左移表示乘2。和2进制打交道速度快

  • 条件运算符:?:

public class Test9 {
    public static void main(String[] args) {
        //三元运算符?:
        //x ? y:z 如果x位true执行y,如果x位false执行z
        int score=80;
        
        String type = score<60 ? "不及格":"及格";
        System.out.println(type);
    }
}
及格
​
Process finished with exit code 0
  • 拓展运算符:+= -= *= /=

public class Test8 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int a1 = 0;
        int a2 = 0;
        a1 += b; //a = a+b
        a2 -= b; //a = a-b
        System.out.println(a1);
        System.out.println(a2);
        //字符串连接符+
        //如果字符串在后面则不会进行拼接
        System.out.println(a+b+"");
        //在前面加一个字符串就自动将a和b识别位字符串
        System.out.println(""+a+b);
    }
}
20
-20
30
1020
​
Process finished with exit code 0
包机制,JavaDoc

包机制

  • 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间

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

  • 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。用'import'语句可完成此功能

  • 阿里巴巴开发手册规定了许多规范推荐

JavaDoc

  • javadoc命令是用来生成自己API文档的

  • 参数信息

  1. @author 作者名

  2. @versino 版本号

  3. @param 参数名

  4. @return 返回值情况

  5. @throws 异常抛出情况

package com.operator;
​

​
public class Test10 {
    public static void main(String[] args) {
​
    }
}
​
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号