JAVA | 2. Output keyword and reserved word identifier variable type conversion String

catalogue

1.1 output

1.2 keywords and reserved words  

1.3 identifier  

1.4 variables  

  1.4.1 concept

  1.4.2 function

  1.4.3 precautions

  1.4.4 assignment of variables

  1.4.5 data type

  1.5 type conversion

  1.5.1 automatic lifting type

  1.5.2 cast type

1.6 String

1.1 output

System.out.println(); //Wrap after output
System.out.print(); //No line wrapping after output

1.2 keywords and reserved words  

      Strings with special meanings are all lowercase.

The three gray words at the bottom are not indicated as keywords in the official documents, but they can be used as keywords  

1.3 identifier  

  • A string used when naming features such as various variables, methods, and classes
  • Any name you give yourself is an identifier

  Naming conventions in Java

1.4 variables  

  1.4.1 concept

  • A storage area in memory
  • The data in this area can change continuously within the same type range
  • Variables are the most basic storage unit in a program. Contains the variable type, variable name, and stored value

  1.4.2 function

  • Used to save data in memory

  1.4.3 precautions

  • Each variable in Java must be declared before use
  • Use the variable name to access the data of this area
  • Scope of variable: within a pair of {} where its definition is located
  • A variable is valid only within its scope
  • Variables with duplicate names cannot be defined within the same scope

  1.4.4 assignment of variables

int num = 7; //Assignment on definition

int num;
num = 7; //Declaration before assignment
  • Variables must be assigned before use!

  1.4.5 data type

(1) Integer: byte, short, int, long

  • Pay attention to the expression range of byte
  •   In Java, the integer constant is int by default, and 'l' or 'l' should be added after declaring a long constant
    long a = 1234567899L;
    long b = 12123; //The compilation passed, because the following value defaults to int and is assigned to long, which belongs to automatic type promotion
                    //If the following value is too large, the compilation fails
  • Variables in Java programs are usually declared as int unless it is insufficient to represent a large number

(2) Floating point type: float, double

  • Two representations:
    • Decimal form: 5.12         512.0f         . five hundred and twelve
    • Scientific counting method form: 5.12e2         512E2         100E-2
  • The floating point type of Java is double by default. Declare a floating constant, followed by 'F' or 'F'  
    float a = 12.3F;
  • The mantissa of float can be accurate to 7 bits. The accuracy of double is twice that of float. Double is generally used

(3) Character type: char

  • All characters in Java use Unicode encoding, so a character is 2 bytes, which can store a letter and a Chinese character, and because the character has the corresponding Unicode encoding, it can perform operations
  • Note that if the length exceeds, an error will be reported! Only one character can be stored!
  • Three forms:
    • Character constants should be enclosed in single quotation marks ('')
      char ch = 'a';
    • Java allows the use of escape characters (\) to convert the characters after them into special character constants  
      • If you want to output an escape character, you need to precede it with another one\
        System.out.print("\\n");    //Output \ n
    • Directly use Unicode value to represent character constant: '\ uXXXX', where XXXX represents a hexadecimal integer, for example: \ u000a represents \ n
  • String connection
    char ch = '\n';
    System.out.print("Hello" + ch);
    /*
    The above operation is equal to
    System.out.print("Hello\n");
    */

    "+" implements the connection between strings

  • Expanding knowledge: code set and garbled code

    • UTF-8 is the most widely used Unicode implementation on the Internet

    • GBK is a Chinese code set

    • If UTF-8 is used for storage and GBK is used for parsing, garbled code will occur

  (4) boolean: boolean

  • There are only two values: true and false
  • It is often used in cycle judgment and condition judgment
    boolean isNight = true;
    if(isNight){
        System.out.println("Good night!");
    }

  1.5 type conversion

  • Premise: boolean is not included

  1.5.1 automatic lifting type

  • The result type obtained by the operation of variables with small capacity and large capacity is large capacity
    byte b1 = 2;
    int i1 = 7;
    byte b2 = i1 + b1; //Compilation failed
    int i2 = i1 + b1; //Compile passed
    float f1 = i1 + b1; //Compile passed
  •   byte,short,char --> int --> long --> float --> double
    • When byte, short and char are used for operation (including between the same types), the result should be at least int

  1.5.2 cast type

  • The strong conversion character: () is required, and the precision may be lost during inverse conversion
float f1 = 7.7;
int i1 = (int)f1; //print i1 = 7

1.6 String

  • It is not a basic data type and belongs to a reference data type
  • Use a pair (""); No characters can be placed in the String variable, and there is no limit on the length
    String a = "1"; //Compile passed
    String b = "";  //Compile passed
    char c = '';    //Compilation failed
  •   String can operate with eight basic data types, and can only be a connection operation. The result is string type
    int num = 2019;
    String str = "Student No.:";
    String info = str + num;  //info = student number: 2019
    boolean b1 = true;
    String info1 = info + b1;  //info1 = student number: 2019true
    //be careful:
    String str2 = 3.5f + "";  //str2 = "3.5"

    Exercise 1:

    char ch = 'a';
    int num = 10;
    String str = "Hello";
    System.out.println(ch + num + str); //Output: 107Hello
    System.out.println(ch + str + num); //Output: aHello10

    Exercise 2:

    //Want to output **
    System.out.println('*' + '\t' + '*');  //Output: 93
    System.out.println('*' + "\t" + '*');  //Output: **
    System.out.println('*' + '\t' + "*");  //Output: 51*

    Summary:

    • The "+" operation between char s is an operation between ASCII codes

    • Only if the variables involved in the operation contain String, "+" is a connection operation

Tags: Java Eclipse

Posted on Thu, 07 Oct 2021 18:59:56 -0400 by DimeDropper