Home > Java > javaTutorial > body text

Java language basic syntax learning

怪我咯
Release: 2017-06-23 14:31:01
Original
1284 people have browsed it

Basic syntax of Java language

1. Identifiers and keywords

  1. Identifier

  • In the java language, it is used to mark class name, object name, variable name, method name, type name, array The valid character sequence of the name and package name is called an "identifier"; the

  • identifier consists of letters, numbers, underscores, and the dollar sign , And the first character of cannot be a number;

  • java language is case sensitive;

  • Identifier naming rules: the first letter of class names is capitalized, variable names and method names use camel case, constants are all capitalized, multiple words are separated by "_", and package names are all lowercase;

  • Keywords

    • In the Java language, some specialized words have been given special meanings, and these words can no longer be used to name identifiers. Characters, these proprietary words are called "keywords";

    • Java has 50 keywords and 3 reserved words, none of which can be used to name identifiers;

      ##extendsfinalfinally floatforgotoifimplementsimportinstanceofintinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuper##switchvolatile
    • true, false, and null are not keywords, they are reserved words, but they still cannot be used to name identifiers. Reserved words are keywords reserved by java and may be used as keywords in future upgrades. Keywords;

    • 2. Basic data types

      1. Integer type (int is the default type)

       

       2. Floating point type (double is the default type)

       


        • When assigning a value to a float type variable, if the assigned value has a decimal part, you must add "F" at the end. ” or “f”;

       3. Character type (2 bytes)


        • char ch = 'a';

        • Some characters cannot be entered into the program through the keyboard, so you need to use escape characters;

      4. Boolean type (1 byte)


        • boolean flag = true;

      5. Default value


        • Numeric variable: 0;

        • Character variable :'\0';

        • Boolean variable: false;

        • Reference data type : null;

      6. Conversion between different data types


        • Automatic type conversion (low to high)


        • Coercion (high to low)

      public class Test003 {
          public static void main(String[] args) {
              byte b = 100;
              int i = 22;
              float f = 78.98f;
              int res = b + i + (int)f;    //此处对f使用了强制类型转换(int)f,转换后的值为78
              System.out.println("res: "+res);    //res: 200
          }
      }
      Copy after login

      3. Operators and expressions

       1. Arithmetic operators

      public class Test003 {    public static void main(String[] args) {        int i = 5;
              System.out.println(0/i);    //0
              System.out.println(0%i);    //0
              System.out.println(i/0);    //除数不能为零,报异常java.lang.ArithmeticException
              System.out.println(i%0);    //除数不能为零,报异常java.lang.ArithmeticException    }
      }
      Copy after login

       2. Assignment operator

       3. Increment and decrement operator (++, --)

      public class Test003 {    public static void main(String[] args) {        int i = 5;
              System.out.println(i++);    //5
              System.out.println(++i);    //7
              System.out.println(i);    //7
              System.out.println(--i);    //6
              System.out.println(i--);    //6
              System.out.println(i);    //5    }
      }
      Copy after login

       4. Relational operators

       5. Logical operators

      public class Test003 {    public static void main(String[] args) {        boolean t = true;        boolean f = false;
              System.out.println(t && f);    //false,短路与运算符,若运算符左侧为false则不计算右侧的表达式
              System.out.println(t || f);    //true,短路或运算符,若运算符左侧为true则不计算右侧的表达式
              System.out.println(t & f);    //false,与运算符,不管左侧是否为false都要计算右侧的表达式
              System.out.println(t | f);    //true,或运算符,不管左侧是否为true都要计算右侧的表达式
              System.out.println(t ^ f);    //true,异或运算符,只要左右两侧不相同则为true,反之为false
              System.out.println(!f);    //true,取反运算符    }
      }
      Copy after login

      6. Bit operator

      public class Test003 {    public static void main(String[] args) {        //在位运算符中1相当于true,0相当于false
              int b1 = 6;    //二进制为00000000 00000000 00000000 00000110
              int b2 = 11;    //二进制为00000000 00000000 00000000 00001011
              System.out.println(b1 & b2);    //按位与运算符,二进制为00000000 00000000 00000000 00000010,结果为2
              System.out.println(b1 | b2);    //按位或运算符,二进制为00000000 00000000 00000000 00001111,结果为15
              System.out.println(b1 ^ b2);    //按位异或运算符,二进制为00000000 00000000 00000000 00001101,结果为13
              System.out.println(~b1);    //按位取反运算符,二进制为11111111 11111111 11111111 11111001,结果为-7
              System.out.println(b1 << 2);    //左移位运算符,二进制为00000000 00000000 00000000 00011000,结果为24
              int b3 = -14;    //二进制为11111111 11111111 11111111 11110010
              System.out.println(b3 >> 2);    //带符号右移位运算符,二进制为11111111 11111111 11111111 11111100,结果为-4
              System.out.println(b3 >>> 2);    //无符号右移位运算符,二进制为00111111 11111111 11111111 11111100,结果为1073741820    }
      }
      Copy after login

      7. Ternary operator

      public class Test003 {
          public static void main(String[] args) {
              int a = 1;
              int b = 2;
              int c = 4;
              int res = c==a+b?++a:c>a+b?++b:++c;    //三元运算符 (表达式)?(值1):(值2),若表达式为true则取值1,反之取值2
              System.out.println(res);    //++b,结果为3
          }
      }
      Copy after login

       8. Operator precedence

      ##4. Array

       1. One-dimensional array

      public class Test003 {    public static void main(String[] args) {        int[] i;    //声明一个整型的一维数组变量
              int ii[];    //声明一个整型的一维数组变量
              i = new int[5]; //创建一个长度为5的一维数组对象,并将变量i指向该对象
              float[] f = new float[5];    //直接创建一个长度为5的单精度浮点型一维数组对象,并将变量f指向该对象
              double[] d = {1, 2, 3.4, 4.5};    //直接初始化一个一维数组元素        
              System.out.println(d[3]);    //通过数组下标来获取数组内的元素,数组下标从0开始,结果为4.5
              System.out.println(f[0]);    //当创建出一个数组对象时,该对象内的数组元素为该数据类型的默认值,所以此处结果为0.0        //System.out.println(i[5]);    //当通过数组下标来获取数组内元素时,[]内的值>=数组长度则报异常java.lang.ArrayIndexOutOfBoundsException(数组下标越界)        //System.out.println(ii[0]);    //若一个数组变量只声明而未指向某一个具体的数组对象时,编译出错
              System.out.println(d.length);    //得到该数组的长度,结果为4    }
      }
      Copy after login

       2. Two-dimensional array

      public class Test003 {    public static void main(String[] args) {        int[][] i;    //声明一个整型的二维数组变量
              int ii[][];    //声明一个整型的二维数组变量
              int[] iii[];    //声明一个整型的二维数组变量
              i = new int[5][2]; //创建一个长度为5的二维数组对象,并将变量i指向该对象
              float[][] f = new float[5][2];    //直接创建一个长度为5的单精度浮点型二维数组对象,并将变量f指向该对象
              double[][] d = {{1}, {2,3}, {4,5,6}, {7,8,9,10}};    //直接初始化一个二维数组元素        
              System.out.println(d[3][1]);    //通过数组下标来获取数组内的元素,数组下标从0开始,结果为8
              System.out.println(f[0][0]);    //当创建出二个数组对象时,该对象内的数组元素为该数据类型的默认值,所以此处结果为0.0        //System.out.println(i[5][0]);    //当通过数组下标来获取数组内元素时,[]内的值>=数组长度则报异常java.lang.ArrayIndexOutOfBoundsException(数组下标越界)        //System.out.println(ii[0][0]);    //若一个数组变量只声明而未指向某一个具体的数组对象时,编译出错
              System.out.println(d.length);    //得到该数组的长度,结果为4
              System.out.println(d[2].length);    //得到二位数组内的下标为2的那个一维数组的长度    }
      }
      Copy after login

      5. Flow control statements (if, switch, for, while, do...while)

      1. Conditional branch statement

      public class Test003 {    public static void main(String[] args) {        int[] score = new int[5];
              score[0] = -7;
              score[1] = 65;
              score[2] = 80;
              score[3] = 90;
              score[4] = 59;        for(int i=0; i<score.length; i++) {            if(score[i]>=0 && score[i]<60) {
                      System.out.println("不及格");
                  }else if(score[i]>=60 && score[i]<80) {
                      System.out.println("及格");
                  }else if(score[i]>=80 && score[i]<90) {
                      System.out.println("良");
                  }else if(score[i]>=90 && score[i]<100) {
                      System.out.println("优");
                  }else {
                      System.out.println("成绩异常");
                  }
              }        
              char ch = &#39;a&#39;;        switch(ch) {    //switch括号内只支持 byte,short,int,char,enum五种数据类型,但是JDK1.7版本增加了String类型,所以相对于JDK1.7而言就是六种了
                  case &#39;A&#39;:    //case为switch语句的入口,break为出口,从入口开始执行,直到遇到出口或代码执行完毕才结束
                  case &#39;a&#39;:
                      System.out.println("优");                break;            case &#39;B&#39;:            case &#39;b&#39;:
                      System.out.println("良");                break;            case &#39;C&#39;:            case &#39;c&#39;:
                      System.out.println("及格");                break;            default:    //若上述条件均不匹配,则进default开始执行语句
                      System.out.println("不及格");
              }
          }
      }
      Copy after login

       2. Loop statement

      public class Test003 {    public static void main(String[] args) {        int res = 0;
              out:    //out是一个标号,告诉java从哪里开始执行程序
              for(int i=1; i<=10; i++) {            if(i==3) continue out;    //continue终止本次循环,执行下次循环
                  if(i==5) break out;    //break跳出循环
                  res = res + i;
              }
              System.out.println(res);    //结果为1+2+4=7
              
              int res2 = 0;        int i = 1;
              in:        do{            if(i==3) continue in;    //continue终止本次循环,执行下次循环
                  if(i==5) break in;    //break跳出循环
                  res2 = res2 + i;
                  i++;
              }while(i<=10);
              System.out.println(res2);
          }
      }
      Copy after login

      abstract assert boolean break byte case catch char
      class const continue default do double else enum
      synchronized this throw throws transient try void
      while

    The above is the detailed content of Java language basic syntax learning. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!