随笔day06

常量

  1. 常量:初始化后不能载改变值!不会变动的值。

  2. 所谓常量可以理解为一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变

final 常量名=值;
final double PI=3.14
  1. 常量名一般使用大写字符

例:

final static  double PI=3.14;//修饰符,不存在先后顺序
   public static void main(String[] args) {
       System.out.println(PI);
  }    

变量的命名规范

  1. 所有的变量、方法、类名要见名思意

  2. 类成员变量:首字母和驼峰原则:monthSalary除了第一个单词以外,后面的单词首字母大写lastName

  3. 局部变量:首字母小写和驼峰原则

  4. 常量:大写字母和下划线:MAX_VALUE

  5. 类名‘首字母大写和驼峰原则:Man,GoodMan

  6. 方法名:首字母小写和驼峰原则:run(),runRun()

 

 

运算符

java语言支持如下运算符:

  1. 算上运算符:+,-,*,/,%(取余,模运算),++,--

  2. 赋值运算符:=

  3. 关系运算符:>,<,>=,<=,==,!=(不等于),instanceof

  4. 逻辑运算符:&&(与)、||(或)、!(非)

  5. 位运算符:&,|,^,~,>>,<<,>>>(了解)

  6. 条件运算符:?:

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

例:

public class Demo01 {
   public static void main(String[] args) {
       //二元运算符
       //ctrl+D 复制当前行到下一行
       int a=10;
       int b=20;
       int c=25;
       int d=25;
       System.out.println(a+b);
       System.out.println(a-b);
       System.out.println(a*b);
       System.out.println((float) a/b);//强制转换,因为输出结果有小数打的存在
  }
}
public class Demo02 {
   public static void main(String[] args) {
       long a=131351351315L;
       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
       //算术运算符,其中进行运算是有long类型,输出也是long类型,否则就是int类型
       double e=151313123.12;
       float f=123.12F;
       long g=1231314561L;
       int h=123;
       System.out.println(e+f+g+h);
       System.out.println(f+g+h);
       System.out.println(g+h);
  }
}
public class Demo03 {
   public static void main(String[] args) {
       //关系运算符返回结果:正确,错误 用布尔值表示
       int a=10;
       int b=20;
       int c=21; //取余 ,模运算
       System.out.println(a>b);
       System.out.println(a<b);
       System.out.println(a==b);
       System.out.println(c%a);//c/a=2...1
  }
}  
public class Demo04 {
   public static void main(String[] args) {
       //++ -- 自增,自减 一元运算符
       int a=3;
       int b=a++;//执行完这行代码后,先给b赋值,再自增
       //a=a+1
       System.out.println(a);//4
       //a++ a=a+1;
       int c=++a;//执行完这代码之前,先自增,在给c赋值
       System.out.println(a);//5
       System.out.println(b);//3
       System.out.println(c);//5

       //幂运算 2^3 2*2*2=8 pow(幂运算)
       //math java里的数学类有很多数学相关的方法
       double pow = Math.pow(2,3);
       System.out.println(pow);
  }
}
package operator;
//逻辑运算符
public class Demo05 {
   public static void main(String[] args) {
//与(and)、或 (or)、非(取反)

       Boolean a=true;
       boolean b=false;
       System.out.println("a&&b:"+(b&&a));//逻辑与运算:两个变量都为真,结果才为true
       System.out.println("a||b:"+(a||b));//逻辑或运算:两个变量有一个为真,结果才为true
       System.out.println(("! a&&b:")+!(a&&b));//如果是真,则变成假,如果是假就变为真
        //短路运算

       int c=5;
       boolean d=(c<4)&&(c++<4);//先判断c<4,是错的,不执行后面的运算,所以c输出的结果还是5
       System.out.println(d);
       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   (异或运算,相同为零,相反为一)
     ~A 1100 0011
     2*8=16 2*2*2*2=16
     << *2 左移
     >> /2   右移
     0000 0000 0
     0000 0001 1
     0000 0010 2
     0000 0011 3
     0000 0100 4
     0000 1000 8
        */
       System.out.println(2<<3);//16
       System.out.println(3<<3);//24
       System.out.println(4<<3);//32

  }
}
package operator;

public class Demo07 {
   public static void main(String[] args) {
       int a=10;
       int b=20;
       int A=10;
       int B=20;
        //扩展赋值运算符
     // a+=b;// a=a+b
        A-=B;// A=A-B
       System.out.println(a);//10
       System.out.println(A);//-10
       //字符串连接符 +,
       System.out.println(a+b);
       System.out.println(""+a+b);//字符串在前面,会进行拼接操作 1020
       System.out.println(a+b+"");//字符串在后面,数值结果还是会相加 30


  }
}
package operator;
//三元运算符 条件运算符 ?:
public class Demo08 {
   public static void main(String[] args) {
   //x ? y: z
       //如果x==true,则输出y,否则就输出z
       int score=90;
       String type=score<60?"不及格":"及格";//重点掌握
       System.out.println(type);

  }
}
 
posted @   Rookie小白韩  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示