basic grammar java details

I. Overview

1.java Language Overview

  1. Is SUN (Stanford University Network, Stanford University Networks) introduced in 1995 a high-level programming language; the father of java --- James Gosling (James Gosling.) .

  2. Application:

    • Java SE (Java Standard Edition) Standard Edition : Support for the Java platform desktop applications (such as applications under Windows) offers a complete Java core API, this version formerly known as J2SE;
    • Java EE (Java Enterprise Edition) Enterprise Edition : for developing applications in enterprise environments to provide a set of solutions. The technology system included techniques such as: Servlet, Jsp, etc., aimed at Web application development. Version formerly known as J2EE;
    • Java ME (Java Micro Edition) Small version: support the Java platform running on the mobile terminal (mobile phone, PDA), and on the Java API has been streamlined, and added support for mobile terminals, this version formerly known as J2ME;
    • Java Card: Support some Java applets (Applets) running on the platform small memory devices (such as smart cards).
  3. java development:

    • In 1996, it released JDK 1.0, approximately 83,000 pages to create Java technology applications
    • In 2004, a milestone release versions: JDK 1.5, highlighting the importance of this version, renamed JDK 5.0
    • In 2009, Oracle's acquisition of SUN, the transaction price of $ 7.4 billion
    • In 2011, the JDK 7.0 release
    • 2014, released JDK 8.0, JDK 5.0 is the second biggest changes since version
    • Subsequently released JDK 9.0, JDK 10.0, JDK 11.0, JDK 12.0, JDK 13.0
  4. java language features:

    • Object-oriented : two basic concepts (classes, objects) with three characteristics (encapsulation, inheritance, polymorphism)

    • Robustness : Absorption advantages C / C ++ language, the program removes some of the impact robustness (pointer memory application and release, etc.)

    • Cross-platform :

      • Cross-platform: written in the Java language applications can run on different platforms. "Write once, Run Anywhere"

      • Principle: as long as the operating system needs to run java applications, install a Java Virtual Machine (JVM Java Virtual Machine) can be. By the JVM is responsible for running Java programs in the system
        Here Insert Picture Description

Operating mechanism and operation process 2.java

  1. Java Virtual Machine (Java Virtal Machine)

    • The JVM is a virtual computer, having a set of instructions and using the different storage areas. Responsible for executing instructions, data management, memory, register
    • For different platforms, different virtual machines, only a platform provides a virtual machine corresponding java, java procedures can run on this platform
    • Java Virtual Machine shielding mechanisms underlying the differences in operation of the platform to achieve a "once, run everywhere"
  2. Garbage collection mechanism (Garbage Collection)

    Java technology provides a garbage collector for automatically detecting each block out of memory space allocated, then automatic recovery worthless memory block.

    There are two standard collection in java determine whether the memory space in line with the garbage collector:

    • Object given a null value NULL then did not call off
    • Given a new value for the object, that is, to re-allocate memory space
  3. Operating principle :

    • Write java source files, to name as a suffix .java
    • Compiled into byte code file using java compiler .java source files are compiled into instructions JVM acceptable set, and stored in a file in the form of bytecode .class
    • Interpreted bytecode .class files, the JVM byte code is read, fetching an instruction, and it can be interpreted computer language execution

    java source code - (java compiler) -> .class file - (JVM interpretation) -> computer language
    Here Insert Picture Description

    3.java environment to build

  4. 什么是JDK ,JRE

    • JDK(Java Development Kit): Java开发工具包

      JDK是提供给Java开发人员使用的,其中包含了java的开发工具,也包括了JRE。所以安装了JDK,就不用在单独安装JRE了。其中的开发工具:编译工具(javac.exe) 打包工具(jar.exe)等

    • JRE(Java Runtime Environment): Java运行环境

      包括Java虚拟机(JVM Java Virtual Machine)和Java程序所需的核心类库等,如果想要 运行一个开发好的Java程序,计算机中只需要安装JRE即可

  5. JDK 、JRE 、JVM 关系
    Here Insert Picture Description
    JDK = JRE + 开发工具集(例如Javac编译工具等)
    JRE = JVM + Java SE标准类库

    总结: 使用JDK 的开发工具完成的java 程序,交给JRE 去运行

  6. 环境变量

    • Java_HOME:JDK的安装路径 (JAVA_HOME: JDK安装路径)
    • PATH:使系统可以在任何路径下识别java命令 (PATH: %JAVA_HOME%\bin)
    • CLASSPATH:Java加载类的路径 (CLASSPATH: .;%JAVA_HOME%\lib) '.'表示当前路径
  7. 运行步骤:在cmd窗口中执行java程序时先用javac命令将java源文件编译成字节码(.class)文件javac 源文件名.java,再用java命令将字节码文件解释为能被计算机执行的语言java 字节码文件 (不能加后缀名)。文档注释(/** */)可以通过javadoc命令生成API文档javadoc -d 文档存放目录 源文件名.java

4.helloword程序

  • Java应用程序的执行入口是main()方法.public static void main(String[] args) {...}

  • Java语言严格区分大小写

  • Java方法由一条条语句构成,每个语句以“;”结束

  • 一个源文件中最多只能有一个public类。其它类的个数不限,如果源文件包含一个public类,则文件名必须按该类名命名

  • 注释:

    • 单行注释: //注释文字

    • 多行注释: /* 注释文字 */

    • 文档注释(Java 特有): 注释内容可以被JDK提供的工具 javadoc 所解析,生成一套以网页文件形式体现的该程序的说明文档。

      /**
      @author 指定java 程序的作者
      @version 指定源文件的版本
      */

二.变量与运算符

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 protected 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,不是关键字。

  • 保留字(reserved word)

    现有Java版本尚未使用,但以后版本可能会作为关键字使用

    goto 、const

2.标识符

  1. 标识符
    • Java 对各种 变量、 方法和 类等要素命名时使用的字符序列称为标识符
  2. 标识符命名规范
    • 由字母,数字,下划线,和‘$’中的任意字符组合而成
    • 数字不可以开头
    • 需要具有一定意义,且不能是系统关键字
    • 严格区分大小写
  3. 名称命名规范
    • 包名:多单词组成时所有字母都小写:xxxyyyzzz
    • 类名、接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz
    • 变量名、方法名:多单词组成时,第一个单词首字母小写,第二个单词开始每个单词首字母大写:xxxYyyZzz
    • 常量名:所有字母都大写。多单词时每个单词用下划线连接:XXX_YYY_ZZZ

3.变量

  1. 变量

    • 变量的概念

      • 内存中的一个内存区域
      • 该区域的数据可以在同一类型范围内不断变化
      • 变量是程序中最基本的存储单元。包含变量类型、变量名和存储的值
    • 注意

      • java中每个变量·必须先声明,后使用
      • 变量的作用域:定义所在的一对{}内容,变量只有在其作用域内才有效
      • 同一作用域内,不能定义重名的变量
    • 声明变量

      • 语法: <数据类型> <变量名称>
      • 示例:int i;
    • 变量赋值

      • 语法: <变量名称> = <值>
      • 示例:i = 6;
    • 声明和赋值变量

      • 语法: <数据类型> <变量名> = <初始化值>
      • 示例:int i = 6;
    • 分类-按声明的位置不同 Here Insert Picture Description

  2. 数据类型
    Here Insert Picture Description

    • 整数类型:默认为int型,声明long需加'l'或'L'
      Here Insert Picture Description

    • 浮点类型:默认为double型,声明float型需要加'f'或'F';float可以精确到7位有效数字,double精度是float的两倍。
      Here Insert Picture Description
    • 字符类型:占2个字节。

    • 布尔类型:只允许true和false,无null。不可以使用0或非0的整数替代false和true。

  3. 基本数据类型变量间转换

    • 自动类型转换:容量小的类型自动转换为容量大的数据类型
      Here Insert Picture Description

      当和字符串进行连接运算时(+)时,

      3+4+"hello" //7hello

      "hello"+3+4 //hello34

      'a'+1+"hello" //98hello

      "hello"+'a'+1 //helloa1

    • 强制类型转换:将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符:(),但可能造成精度降低或溢出。

  4. 基本数据类型与String间转换

    字符串不能直接转换为基本类型,但通过基本类型对应的包装类则可以实现把字符串转换成基本类型。如:Integer.parseInt()

4.运算符

运算符是一种特殊的符号,用以表示数据的运算、赋值和比较等。

  1. 算术运算符
    Here Insert Picture Description

  2. 赋值运算符

    符号:=

    当“=”两侧数据类型不一致时,可以使用自动类型转换或使用强制类型转换原则进行处理

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

  3. 比较运算符

    比较运算符的结果都是boolean型,也就是要么是true,要么是false;

    比较运算符“==” 不能误写成“=”。
    Here Insert Picture Description
  4. 逻辑运算符

    & —逻辑与 | —逻辑或 ! —逻辑非
    && —短路与 || —短路或 ^ —逻辑异或
    Here Insert Picture Description
    “&”和“&&”的区别:

    单&时,左边无论真假,右边都进行运算;

    双&时,如果左边为真,右边参与运算,如果左边为假,那么右边不参与运算。

    “|”和“||”的区别同理,||表示:当左边为真,右边不参与运算。

    异或( ^ )与或( | )的不同之处是:当左右值都相等时,结果为false

  5. 位运算符

    位运算是直接对整数的二进制进行的运算。

    二进制或运算符(or):符号为|,表示若两个二进制位都为0,则结果为0,否则为1

    二进制与运算符(and):符号为&,表示若两个二进制位都为1,则结果为1,否则为0。

    二进制否运算符(not):符号为~,表示对一个二进制位取反。

    异或运算符(xor):符号为^,表示若两个二进制位不相同,则结果为1,否则为0。

    左移运算符(left shift):符号为<<

    右移运算符(right shift):符号为>>

    头部补零的右移运算符(zero filled right shift):符号为>>>

    位运算只对整数有效,遇到小数时,会将小数部分舍去,只保留整数部分。所以,将一个小数与0进行二进制或运算,等同于对该数去除小数部分,即取整数位。
    Here Insert Picture Description
    无 <<< 运算符
    Here Insert Picture Description
  6. 三元运算符

    格式:(条件表达式)?表达式1:表达式2

    条件表达式为true,运算结果是表达式1;为false,运算结果是表达式2;

  7. 运算符的优先级

    只有单目运算符、三元运算符、赋值运算符是从右向左运算的。
    Here Insert Picture Description

三.流程控制

流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块。

其流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:顺序结构、分支结构、循环结构

1.顺序结构

程序从上到下逐行地执行,中间没有任何判断和跳转。

Java中定义成员变量时采用合法的前向引用

2.分支结构

  1. if-else结构

    • if(条件表达式){ 执行代码块; }
    • if(条件表达式){ 执行代码块1; }else{ 执行代码块2; }
    • if(条件表达式){ 执行代码块1; }else if(条件表达式){ 执行代码块2; }else{ 执行代码块3; }
  2. switch-case结构

    switch(表达式){
        case 常量1:
            语句1;
            // break;
        case 常量2:
            语句2;
            // break;
        default:
            语句;
            // break;
    }
    • switch(表达式)中表达式的值 必须是下述几种类型之一:byte ,short ,char ,int ,枚举 (jdk 5.0) ,String (jdk 7.0)
    • case子句中的值必须是 常量,不能是变量名或不确定的表达式值
    • 同一个switch语句,所有case子句中的常量值互不相同
    • break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾
    • default子句是 可任选 的。同时,位置也是灵活的。当没有匹配的case时,执行default
  3. 如果判断的具体数值不多,又符合byte、short、char、int、String、枚举等类型,建议使用switch-case,效率稍高;对区间判断,对结果为boolean类型判断,建议使用if语句。

3.循环结构

在某些条件满足的情况下,反复执行特定代码的功能。循环语句的四个组成部分:初始化部分(init_statement)、循环条件部分(test_exp)、循环体部分(body_statement)、迭代部分(alter_statement)
Here Insert Picture Description

  1. for循环

    for(1初始化部分; 2循环条件部分; 4迭代部分){
        3循环体部分;
    }
    • 循环条件部分为boolean类型表达式,当值为false时,退出循环
    • 初始化部分可以声明多个变量,但必须是同一个类型,用逗号分隔
    • 可以有多个变量更新,用逗号分隔
  2. while循环

    1初始化部分;
    while(2循环条件部分){
     3循环体部分;
     4迭代部分;
    }
    • Do not forget to declare the iteration part . Otherwise, the cycle will not end into an infinite loop
    • for and while loops can be interchangeable
  3. do-while loop

    1初始化部分;
    do{
        3循环体部分;
        4迭代部分;
    }while(2循环条件部分);
    • do-while loop executes at least one cycle
  4. Nested loops (multiple cycle)

    The body of a cycle in another cycle, to form a nested loop.

    The inner loop is a nested loop as the loop of the outer loop . When only the inner loop of the loop condition is false, it will be completely out of the inner loop, before the end of the outer layer when cycles, the next cycle begins.

    Provided m times as the number of cycles the outer, inner layer is n times, the inner loop is executed actually required m * n times.

  5. break, continue to use

    • break
      • break statement is used to terminate the execution of a statement block
      • When the break statement appears in a nested statement block, the tag can indicate which layer is to be terminated statement block
    • continue
      • It continues statement is used to skip the block in the loop the first execution continues next cycle,
      • Continue statement appears in the nested loop bodies may be skipped by the label indicates which one cycle
    • return
      • return: is not dedicated to the end of the cycle, its function is the end of a method . When a method to execute a return statement, this method will be ended
      • The difference is that break and continue, return directly to the end of the whole method, no matter how many layers within the circulation of this return in
    • note
      • break can only be used in switch statements and loops
      • continue only for loop statement
      • continue this cycle is terminated, break layer to terminate the present cycle
      • break, you can not have other statements after continue, because the program never executed subsequent statement
      • Label statement must immediately head cycle. Reference numeral statements can not be used in front of the non-loop

Guess you like

Origin www.cnblogs.com/itzlg/p/12023985.html