1 变量&运算符&表达式

Head First Java 和 AcWing Java课程做的总结1。

先来一个经典hello world程序引入:

public class Main{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

1.1 变量

  • 变量必须先定义,才可以使用。不能重名。

  • 变量定义的方式:

  • public class Main{
        public static void main(String[] args){
            int a = 5;
            int b, c = a, d = 10/2;
        }
    }
    

内置基本数据类型:

类型 字节数 举例 包装的类
byte 1 123 Byte
short 2 12345 Short
int 4 123456789 Integer
long 8 1234567891011L Long
float 4 1.2F Float
double 8 1.2,1.2D Double
boolean 1 true,false Boolean
char 2 'A' Character

常量:

  • 使用final修饰:

  • final int N = 110;
    

类型转换:

  • 显示转化:int x = (int)'A';
  • 隐式转化:double x = 12, y = 4 * 3.3;

autoboxing:

  • 手动拆装箱

    • //手动装箱
      Integer i = new Integer(123);
      
      //手动拆箱( 实例方法 xxxValue() )
      float f = fv.floatValue();
      
      System.out.println(f); //123.0
      
  • 自动拆装箱(autoboxing)

    • 自动装箱:基本数据类型自动转换成包装类。

      自动拆箱:包装类自动转换成基本数据类型。

    • //自动装箱
      	Integer z = 1000;
          // z是一个引用,z是一个变量,z还是保存了一个对象的内存地址。
          // 等同于:Integer z = new Integer(1000);
      
      //自动拆箱
          System.out.println(z + 1);
          // 分析为什么这个没有报错呢?
          // 两边要求是基本数据类型的数字,z是包装类,不属于基本数据类型,这里会进行自动拆箱。将z转换成基本数据类型
          // 在java5之前这样写肯定编译器报错。
      
  • Interger的使用举例

    • //将十进制转换成二进制字符串:
      String binaryString = Integer.toBinaryString(3);
      
      //将十进制转换成八进制字符串:
      String octalString = Integer.toOctalString(8);
      
      //将十进制转换成十六进制字符串:
      String hexString = Integer.toHexString(16);
      
      //获取包装类的数值范围:
      System.out.println("int的最大值:" + Integer.MAX_VALUE);
       
      System.out.println("int的最小值:" + Integer.MIN_VALUE);
      
  • “==” 等号运算符不会触发自动拆箱机制。(只有+ - * /等运算的时候才会。)

  • 整数型常量池

    • java中为了提高程序的执行效率,将[-128到127]之间所有的包装对象提前创建好,放到了一个方法区的整数型常量池当中了,目的是只要用这个区间的数据不需要再new了,直接从整数型常量池当中取出来。
    • Integer i = -128 <-> 127Integer j = -128 <-> 127,而i == j的结果是true,即地址相同。

字符串和包装类对象的转换:

/*( 静态方法 parseXXX() )*/
//Interger包装类中
    static int parseInt(String s);
    // 静态方法,传参String,返回int
    // 如网页上文本框中输入的100实际上是"100"字符串。后台数据库中要求存储100数字,此时java程序需要将"100"转换成100数字。

	int retValue = Integer.parseInt("123"); // String -转换-> int
    //int retValue = Integer.parseInt("中文"); // NumberFormatException
    System.out.println(retValue + 100);


//Double包装类中
    double retValue2 = Double.parseDouble("3.14");
    System.out.println(retValue2 + 1); //4.140000000000001(精度问题)
 
    float retValue3 = Float.parseFloat("1.0");
    System.out.println(retValue3 + 1); //2.0

1.2 运算符

A = 10, B = 20
运算符 描述 实例
+ 把两个数相加 A + B 将得到 30
- 从第一个数中减去第二个数 A - B 将得到 -10
* 把两个数相乘 A * B 将得到 200
/ 分子除以分母 B / A 将得到 2
% 取模运算符,向零整除后的余数,注意余数可能为负数 B % A 将得到 0
++ 自增运算符 A++:先取值后加1;++A:先加1后取值
-- 自减运算符 A--:先取值后减1;--A:先减1后取值
+= 第一个数加上第二个数 A = A + B 可以简写为 A += B
-= 第一个数减去第二个数 A = A - B 可以简写为 A -= B
*= 第一个数乘以第二个数 A = A * B 可以简写为 A *= B
/= 第一个数除以第二个数 A = A / B 可以简写为 A /= B
%= 第一个对第二个数取余数 A = A % B 可以简写为 A %= B

运算符优先级:

1.3 表达式

整数的加减乘除四则运算:

public class Main{
    public static void main(String[] args) {
        int a = 6 + 3 * 4 / 2 - 2;

        System.out.println(a);

        int b = a * 10 + 5 / 2;

        System.out.println(b);

        System.out.println(23 * 56 - 78 / 3);
    }
}

浮点数(小数)的运算:

public class Main{
    public static void main(String[] args){
        double x = 1.5, y = 3.2;

        System.out.println(x * y);
        System.out.println(x + y);
        System.out.println(x - y);
        System.out.println(x / y);
    }
}

整型变量的自增、自减:

public class Main{
    public static void main(String[] args){
        int a = 1;
        int b = a ++ ;
        System.out.println(a + " " + b);

        int c = ++ a;
        System.out.println(a + " " + c);
    }
}
posted @   杨大康  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示