Java基础知识

218 阅读6分钟

B站学习截图

基础语法

关键字、保留字、标识符

1. 关键字(keyword)

  • 定义:被Java语言赋予了特殊含义,用于专门用途的字符串(单词)
  • 特定:关键字中所有字母都为小写字母
  • 关键字有哪些 | 用于定义变量的关键字 | | | | | | --- | --- | --- | --- | --- | | class | interface | enum | byte | short | | int | long | float | double | char | | boolean | void | | | | | 定义流程控制的关键字 | | | | | | if | else | switch | case | default | | while | do | for | break | continue | | return | | | | | | 定义访问权限修饰符的关键字 | | | | | | private | protect | public | | | | 定义类、函数、变量修饰符 | | | | | | abstract | final | static | synchronized | | | 定义类与类之间之间关系 | | | | | | extends | implements | | | | | 定义建立实例与引用实例,判断实例的关键字 | | | | | | new | this | super | instanceof | | | 用于处理异常的关键字 | | | | | | try | catch | finally | throw | throws | | 用于包的关键字 | | | | | | package | import | | | | | 用于修饰关键字 | | | | | | native | strictfp | transient | volatile | assert | | 用于定义数据类型的字面值 | | | | | | true | false | null | | |

2. 保留字(reserved words)

先有的Java版本尚未使用,但以后的版本可能会作为关键字使用。自己命名标识符时要避免使用这些保留字:goto、const

3. 标识符(identifier)

  • 什么是标识符呢?

Java对各种变量、方法和类等要素命名时使用的字符序列称为标识符

  • 定义符合标识符规则
  1. 由26个英文字母大小写、0-9、_或$组成
  2. 数字不可以开头
  3. 不可以使用关键字和保留字,但能包含关键字和和保留字
  4. Java中严格区分大小写,长度无限制
  5. 标识符不能包含空格
  • 凡是自己可以起名字的地方都叫标识符:比如,类名、变量名、方法名、接口名、包名...
  • Java中的名称命名规范
  • 包名:多单词组成时,所有单词均要小写;xxyyzz
  • 类型、接口名:多单词组成时,所有单词的首字母都要大写;XxYyZz
  • 变量名、方法名:多单词组成时,第一个单词的首字母小写,第二个单词开始后面单词的首字母大写;xxYyZz
  • 常量名:所有单词都大写,多单词组成时每个单词用_连接;XX_YY_ZZ

注意:

  • 在起名字时,为了提高阅读性,要做到见名知意;
  • Java采用Unicode字符集,因此标识符可以用汉字进行命名,但不建议使用

变量、数据类型

1. 变量(variable)

  • 变量的概念
  1. 内存中的一块存储区域
  2. 该区域的数据可以在同一类型范围内不断变化
  3. 变量时程序中最基本的存储单元,包含变量类型、变量名和存储的值
  • 变量的作用

用于在内存中保存数据

  • 使用变量注意
  1. Java中每个变量必须先声明再使用
  2. 使用变量名来访问该区域的数据
  3. 变量的作用域,其定义在一个{}内
  4. 变量只有在其作用域内才生效
  5. 用一个作用域内,不能定义重名的变量
  • 变量的分类

|按数据类型分类| 对于每一种数据都定义了明确的具体的数据类型(强类型语言),在内存中分配了不同的内存空间 | | | || --- | --- | --- | --- | |基本数据类型| 数值型 | 整数型(byte、short、int、long) | 浮点型(float、double)| || 字符型 | 字符型(char)| | || 布尔型 | 布尔型(boolean)| | |引用数据类型| 类 | 类(class)-- 字符串在这里| | || 接口 | 接口(interface)| | || 数组 | 数组(array)| | |按声明位置分类|变量在代码中定义的位置 | | |成员变量| 实例变量 | 不以static修饰的变量| | | | 类变量 | 以static修饰的变量| | |局部变量| 形参 | 方法、构造器中定义的变量| | || 方法局部变量 | 在方法内定义| | || 代码块局部变量 | 在代码块内定义| |

注意:二者在初始化值方法的异同 异:局部变量除形参外,需显式初始化 同:都有声明周期

2. 基本数据类型

  • 整型(byte、short、int、long)

      1. Java个数据类型有固定的表数范围和字段长度,不受具体的OS影响,以保证Java程序的可移植性
      1. 声明long型常量后,数值后需加“l”或“L”
      1. bit是计算机中最小的存储单元;byte是计算机中基本存储单元
      1. Java整形常量默认为int型,通常也是声明为int,除非不足以表达较大的数,才使用long | 类型 | 占用存储空间 | 表数范围 | | --- | --- | --- | | byte | 1字节=8bit位 | 2^8=256:-128 ~ 127 | | short | 2字节 | -2^15 ~ 2^15-1 | | int | 4字节 | -2^31 ~ 2^31-1 | | long | 8字节 | -2^63 ~ 2^63-1 |
  • 浮点型(float、double)

      1. 与整数类型类似,Java浮点类型也有固定的表数范围和字段长度,不受具体操作系统的影响
      1. 浮点型常量有两种表示形式:
      • a. 十进制数形式:5.12、512.0f、.512
      • b. 科学计数法形式:5.12e2、512E2、100E-2
      1. float单精度:尾数可精确到7位有效数字;声明为该类型时,需要在数值后加“f”或“F”;很多情况下,精度很难满足要求
      1. double双精度:尾数可精确到14位有效数字。通常采用此类型
      1. Java的浮点型常量默认为double型 | 类型 | 占用存储空间 | 表数范围 | | --- | --- | --- | | 单精度float | 4字节 | -3.403E38 ~ 3.403E38 | | 双精度double | 8字节 | -1.798E308 ~ 1.798E308 |
  • 字符型(char)

      1. char类型用来表示通常意义上的“字符”(2字节)
      1. Java中的所有字符均采用Unicode编码。故一个字符可以存储一个字母/一个汉字/其他书面语的一个字符
      1. 字符型变量的三种表现形式:
      • a. 字符常量是用单引号括起来的单个字符:char c1='中';;
      • b. Java中允许使用转义字符\将其后的字符转换为特殊的字符型常量:char c2='\n'; -- 代表换行符
      • c. 直接使用Unicode值来表示字符型常量:\u000a代表\n
      1. char类型是可以运算的,因为它都对应有Unicode码
  • 布尔型(boolean)

3. 数据类型转换
基本数据类型之间的运算规则

注意:

  1. 这里只讨论7种基本数据类型变量之间的运算,不包含与boolean类型的运算
  2. 下面说的容量大小,指的是表示数的范围的大和小,比如float的容量大于long的容量
  • 自动类型提升
    • 当容量小的数据类型变量与容量大的数据类型变量做运算时,结果自动提升为容量大的数据类型byte、char、short -> int -> long -> float -> double
    • 特别的:byte、char、short三种类型的变量做运算时,结果类型为int
  • 强制类型转换:自动类型提升的逆运算
    • 需要使用强转符:()
    • 强制类型转换会导致精度的丢失
/**
 * 类型转换
 */
class TypeConvert {
   
   /**
    * 获取变量类型公共方法
    */
   public static String getType(Object o){
      // 使用int类型的getClass()方法
      return o.getClass().toString();
   }
   
   /**
    * 类型自动提升
    */
    static byte byte_variable = 2;
   static int int_variable = 120;
   // byte byte_int_variable = byte_variable + int_variable;  编译不通过
   static int byte_int_variable = byte_variable + int_variable;
   
   static short short_variable = 24;
   //static short byte_short_variable = byte_variable + short_variable; 编译不通过
   static int byte_short_variable = byte_variable + short_variable;
   
   static char char_variable = 56;
   static char char_variable_2 = 57;
   //static char char_variable_3 = char_variable + char_variable_2; 编译不通过
   static int char_variable_3 = char_variable + char_variable_2;
   
   static double double_variable = 12.3;
   static double char_double_variable = char_variable + double_variable;
   
   
   /**
    * 强制类型转换
    * @param args
    */
   static int double_updownto_int_variable = (int)double_variable; // 截断操作
   // 没有损失精度
   static long long_variable = 123;
   static short long_updownto_short_variable = (short)long_variable;
   
   
   public static void main(String[] args){
      System.out.println("byte_int_variable: " + byte_int_variable);
      System.out.println("type of byte_int_variable: " + getType(byte_int_variable));
      
      System.out.println("byte_short_variable: " + byte_short_variable);
      System.out.println("type of byte_short_variable: " + getType(byte_short_variable));
      
      System.out.println("char_variable_3: " + char_variable_3);
      System.out.println("type of char_variable_3: " + getType(char_variable_3));
      
      System.out.println("char_double_variable: " + char_double_variable);
      System.out.println("type of char_double_variable: " + getType(char_double_variable));
      
      System.out.println("double_updownto_int_variable: " + double_updownto_int_variable);
      System.out.println("type of double_updownto_int_variable: " + getType(double_updownto_int_variable));
      
      System.out.println("long_updownto_short_variable: " + long_updownto_short_variable);
      System.out.println("type of long_updownto_short_variable: " + getType(long_updownto_short_variable));
   }
}