Skip to content

Java

网站备注
downloadfull download , how to remove and install
docs

基本数据类型

基本类型存储大小初始化默认值取值范围包装类型
byte1字节(8位)0-128~127Byte
short2字节(16位)0-32768~32767Short
int4字节(32位)0-2^31 ~ 2^31 - 1Integer
long8字节(64位)0L。"L"理论上不分大小写,但若写成"l"容易与数字"1"混淆,不容易分辨,所以最好大写。-2^63 ~ 2^63 - 1Long
float4字节(32位)0.0f,必须加F/f符合IEEE754标准的浮点数,1.4E-45 ~ 3.4028235E38Float
double8字节(64位)0.0d,默认浮点类型,可以加D/d,或者不加符合IEEE754标准的浮点数,4.9E-324 ~ 1.7976931348623157E308Double(默认)
char2字节(16位)‘\u0000’\u0000 ~ \uffff(十进制等效值为 0~65535,本质也是数值)Character
boolean1字节(8位)/4字节(32位)falsetrue/falseBoolean

命名

  • 类名:大驼峰
  • 方法名:小驼峰
  • 变量名:小驼峰

常量

final 声明,一般大写。

java
final int A = 1;

char类型

java
System.out.print("\u0414"); \\以\u开头

数值类型转换

Java
public class Main {
    public static void main(String[] args) {
        int price1 = 970;
        int price2 = 1285;
        int price3 = 3582;

        int sum = price1 + price2 + price3;
        System.out.println("The sum of price is " + sum);

        float avg = sum / 3;
        System.out.println("The average price is " + avg); //1945.0

        float avg2 = (float) sum / 3;
        System.out.println("The average price is " + avg2);//1945.6666 更准确
      
        float avg2 = (float) (sum / 3);
        System.out.println("The average price is " + avg2);//1945.0
      
      	System.out.println(3/2); // 1
    }
}