鱼C论坛

 找回密码
 立即注册
查看: 2504|回复: 0

[学习笔记] JAVA学习Day2【运算符】

[复制链接]
发表于 2020-8-2 17:08:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学习视频:https://www.bilibili.com/video/BV17J411G72L?p=20

20 - 40

  1. /*
  2. * (1)浮点类型不精确 因为十进制的小数部分转二进制需要舍去
  3. * (2)float类型的4个字节能表示的数字范围比long类型的8个字节还要大 因为float底层存的是指数
  4. */
  5. class test01{
  6.         public static void main(String[] args) {
  7.                 System.out.println(10);
  8.                 System.out.println(0b10);//二进制在数字前面加0B或0b
  9.                 System.out.println(010);//八进制在数字前面加0
  10.                 System.out.println(0x10);//十六进制在数字前面加0X或0x
  11.         }
  12. }

  13. /*
  14. * 基本数据类型之间的转换
  15. * (1)自动类型转换(小容量存到大容量)boolean不参与、byte、short、char如果进行数学运算会自动升级为int
  16. * byte < short < int < long < float < double
  17. * (2)强制类型转换(大容量存到小容量中)强制类型转化是有风险的,可能会出现溢出
  18. */

  19. class test02{
  20.         public static void main(String[] args) {
  21.                 double d1 = 10;
  22.                 System.out.println(d1);
  23.                
  24.                 int d2 = (int)10.0;
  25.                 System.out.println(d2);
  26.                
  27.                 byte b1 = 1;
  28.                 byte b2 = 2;
  29.                 byte b3 = (byte)(b1 + b2);
  30.                 System.out.println(b3);
  31.                
  32.                 char c1 = '1';
  33.                 char c2 = '2';
  34.                 char c3 = (char)(c1 + c2);
  35.                 System.out.println(c3);
  36.                
  37.                 short s1 = 1;
  38.                 short s2 = 2;
  39.                 short s3 = (short)(s1 + s2);
  40.                 System.out.println(s3);
  41.                
  42.                 int x= 1;
  43.                 int y = 2;
  44.                 System.out.println(x/y);//整数/整数 结果还是整数  
  45.                 System.out.println((double)x/y);
  46.                
  47.                
  48.         }
  49. }


  50. /*
  51. * 数据类型转换的特例:字符串类型
  52. */

  53. class test03{
  54.         public static void main(String[] args) {
  55.                 char c1 = '0';
  56.                 char c2 = '1';
  57.                 System.out.println(c1 + c2 + "");//97
  58.                 System.out.println("" + c1 + c2);//01
  59.                 System.out.println(c1 + "" + c2);//01
  60.         }
  61. }


  62. /*
  63. *  运算符
  64. *  取余% 只看被模数(符号前面)的正负号  被模数%模数
  65. */
  66. class test04{
  67.         public static void main(String[] args) {
  68.                 int x= 10;
  69.                 int y = 3;
  70.                 System.out.println();
  71.                
  72.                 int m = 1;
  73.                 int n = ++m;
  74.                 System.out.println(m);//2
  75.                 System.out.println(n);//2
  76.                
  77.                 m = 1;
  78.                 n = m++;
  79.                 System.out.println(m);//2
  80.                 System.out.println(n);//1
  81.         }
  82. }


  83. class test05{
  84.         public static void main(String[] args) {
  85.                 int i = 1;
  86.                 int j = 5;
  87.                 j *= i++ + j++;
  88.                 //j*=i++ + j 等价于 j = j * (i++ + j)
  89.                 System.out.println(j);
  90.         }
  91. }

  92. /*
  93. * 位运算
  94. * (1)左移<< 右移 >> 无符号右移 >>> 与& 或|  异或^ 取反~
  95. */

  96. class test06{
  97.         public static void main(String[] args) {
  98.                 System.out.println(4 << 3);//等价于4乘以2的3次方 为32!!
  99.                 //<< 几位就乘以2的几次方!!
  100.                 System.out.println(32 >> 3);//等价于32除以2的四次方!!
  101.                 // >> 几位就除以2的几次方!!
  102.                 System.out.println(32 >>> 3);
  103.                 System.out.println(-32 >>> 3);
  104.                 }
  105. }


复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 21:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表