Java基础:基本运算符,自增自减运算符,初始Math类,逻辑运算符,位运算符,三元运算符

package 基本运算符;
/*
运算符
   算术运算符:+,-,*,/, %, ++, --
   赋值运算符=
  关系运算符:>,<, >= <=, == != instanceof
  逻辑运算符:&&, ||, !
  位运算符:&, |, ^, ~, >>, <<, >>>(了解!!!)
  条件运算符?:
  扩展赋值运算符:+=, -=, *=, /=
 */
public class Demo1 {
public static void main(String[] args) {
    //二元运算符
    int a = 10;  //ctrl+c 复制
    int b = 20; //ctrl+v 粘贴
    int c = 30;
    int d = 40;
    //  +加  -减   *乘   /除
    //强转小数使用double进行
    System.out.println (a+d);
    System.out.println (a-d);
    System.out.println (a*d);
    System.out.println (a/(double)c);
}


}
public static void main(String[] args) {
        long a = 12222222L;
        int b =2222;
        short c = 10;
        byte d =8;
        //cast转换
        System.out.println (a+b+c+d);//long
        System.out.println (b+c+d);//int
        System.out.println (c+d);//int
        //关系运算符返回的结果:正确,错误  布尔值
        // 大于 >  //小于< //==等于 //!= 不等于
        //往后运算符会用到if结构上
        int e = 10;
        int f = 20;
        int g = 26;
        //取余,模运算
        System.out.println (g%e); // 26 / 10 = 6
        System.out.println (e>f);
        System.out.println (e<f);
        System.out.println (e==f);
        System.out.println (e!=f);
 public static void main(String[] args) {
        //++  -- 自增,自减  无运算符
         int a = 3;

         int b = a++;//执行完这行代码后,先给b赋值,再自增
        //a = a +1;
        System.out.println (a);
         //a a = a+1;
        int c = ++a;//执行完这行代码前,先自增,再给b赋值

        System.out.println (a);
        System.out.println (b);
        System.out.println (c);
        //幂运算 2^3 2*2*2 = 8   很多运算,我们会使用一些工具类来操作
        //反过来3,2就是3乘3的意思
        //Math类提供了很多科学工程计算需要用到的方法和常数,
        // 很多特殊的运算需要借助于Math.pow();
        double pow = Math.pow (2,3);

        System.out.println (pow);

//逻辑运算符 位运算符
public class Deno4 {

    public static void main(String[] args) {
    // 与(and) 或(or)  非(取反)
  boolean a  = true;
  boolean b = false;
        System.out.println ("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true
        System.out.println ("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,则结果才为true
        System.out.println ("!(a && b):"+ !(a&&b));//如果是真,则变为假,如果是假则变为真
        //短路运算
        int c= 5;
             //C<4=false肯定是错的 后面c++<4肯定不执行,所有说他肯定不会走+号没变还是5
        boolean d = (c<4)&&(c++<4);
        System.out.println (d);
        System.out.println (c);

    }
}
     /*
       A= 0011 1100
       a= 0000 1101

       A&B 0000 1100
       A/B 0011 1101
       A^B 0011 0001
       ~B  1111 0010 相反0000 1101
     2* 8=16 2* 2* 2 *2
     //位运算效率极高
     << 左移 *2  //乘
     >> 右移 /2 //除
                示例
         0000 0000 0 //八个0就是0
         0000 0001 1 //二进制01就是1
         0000 0010 2  //2就是2进1就是10
         0000 0011 3  //3就是11
         0000 0100 4  //4就是他要加1所以是100
         0000 1000 8  //后面都是一样的道理
         0001 0000 16

      */

        System.out.println (2<<3);
        //一元和二元运算符
        int a = 10;
        int b = 20;
        a+=b; //a = a+b
        a-=b; //a = a-b

        System.out.println (a);
       //字符串连接符   + , ""String是字符串,都转换成String在进行连接所以""+a+b输出的结果为1020
        //""在前面会对这个数进行拼接
        System.out.println (""+a+b);
        //先计算前面的值a+b输出的结果为30没变
        System.out.println (a+b+"");

    }
}
  //三元运算符
public class Deno6 {
    public static void main(String[] args) {
        //x  ? y = z
       //如果x==true,则结果为y,否则结果为z
      //优先级() 先乘除 后加减
        int seore  = 50;

        String type= seore <60 ? "及格": "不及格";//必须掌握,在可发的过程中十分的常见。
        System.out.println (type);

    }
}

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
public synchronized String nextId() { long timestamp = timeGen(); //获取当前毫秒数 //如果服务器时间有问题(时钟后退) 报错。 if (timestamp < lastTimestamp) { throw new RuntimeException(String.format( "Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果上次生成时间和当前时间相同,在同一毫秒内 if (lastTimestamp == timestamp) { //sequence自增,因为sequence只有12bit,所以和sequenceMask相与一下,去掉高位 sequence = (sequence + 1) & sequenceMask; //判断是否溢出,也就是每毫秒内超过4095,当为4096时,与sequenceMask相与,sequence就等于0 if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); //自旋等待到下一毫秒 } } else { sequence = 0L; //如果和上次生成时间不同,重置sequence,就是下一毫秒开始,sequence计数重新从0开始累加 } lastTimestamp = timestamp; long suffix = (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence; String datePrefix = DateFormatUtils.format(timestamp, "yyyyMMddHHMMssSSS"); return datePrefix + suffix; } protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } protected long timeGen() { return System.currentTimeMillis(); } private byte getLastIP(){ byte lastip = 0; try{ InetAddress ip = InetAddress.getLocalHost(); byte[] ipByte = ip.getAddress(); lastip = ipByte[ipByte.length - 1]; } catch (UnknownHostException e) { e.printStackTrace(); } return lastip; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值