Basic functions of IDEA, data type extension, type conversion

Posted by faswad on Sat, 01 Jan 2022 16:47:20 +0100

Familiar with the basic functions of IDEA

Original address: https://www.cnblogs.com/xclw/p/10497678.html

1. Format code: Ctrl+Alt+L

2. Rename variable: hover over the variable, Shift+F6

3. Open the directory where the file or project is located: right click Show in Explorer

4. Add shortcut keys to surround code blocks: Ctrl+Alt+T, corresponding to Ctrl+K, S in VS. you can quickly add if, try catch, region, editor fold and other surrounding code blocks to the code

            //region Description
            int a;
            //endregion
            //<editor-fold desc="Description">
            int a;
            //</editor-fold>

5. Collapse and expand codes

By default, if, while, do and other code blocks cannot be collapsed. Select Ctrl +, The selected code becomes collapsible, the cursor is at any position, and press Ctrl +. Once, This code becomes non collapsible

Ctrl+Shift+. Makes the code block where the cursor is located collapsible

Ctrl+B/Ctrl+Click to quickly open the class or method at the cursor (jump to the definition)

CTRL + Alt + < --- code position backward (win10) conflicts with the shortcut key of the graphics card. See here https://blog.csdn.net/u010814849/article/details/76682701/ )

CTRL + Alt + -- > code position forward (win10 conflicts with the shortcut key of the graphics card, see here https://blog.csdn.net/u010814849/article/details/76682701/ )

6. Two methods of displaying method outline (method list):

View -- > tool windows -- > structure (Alt+7) docked window

Ctrl+F12 pop up window

7.Alt+Enter: import package

8. Shortcut key for quick implementation of interface method: Ctrl+i

9. Quickly override the base class method: Ctrl+O

10. Automatically generate getter setter: Alt+Insert

11. If the package name of the class is inconsistent with the path of the class file, it shall be corrected by ALT+ENTER

12. Comment code

[1] Line comment Ctrl+/
First, your cursor should be in this line. It can be in any position on this line. Press Ctrl + /, you can add "/ /" to the beginning of the line and comment out the line.

Press Ctrl + / againto remove the comment line.

[2] Block annotation Ctrl+Shift+/
To use block annotation, you need to select the block to be annotated first.

Press Ctrl+Shift + / to comment out the block code

When removing comments, you don't need to select all this code. Just press Ctrl+Shift + / on the comment content with the cursor.

[3] Method or class comments
At the beginning of a method or class, enter / * *, and then press enter to automatically generate a comment template according to the parameters and return values. We can write on this template.

13. Compressed tundish

In the package directory, if there is an empty directory in the multi-level package, it will shrink to AAA bbb. Form of CCC

In some cases, it is necessary to display the real hierarchical relationship of these directories. Click the setting button and select or deselect the Compact Middle Packages option to switch between the two display forms

As shown in the figure below

Data type extension

  1. Integer extension
  • Hexadecimal: binary (beginning with 0b) decimal octal (beginning with 0) hexadecimal (beginning with 0x)

public classs Demo01 {
public static void mian(String[] args){
int i = 10;
int i2 = 010; // Octal 0
int i3 = 0x10; // Hex 0x 0 ~ 9 A ~ F
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
}
}
```

  1. Floating point extension
  • How to express banking business?

    BigDecimal math tool class

  • float finite discrete rounding error is approximately close to but not equal to

  • double

  • It is best to use floating point numbers for comparison

  1. Character expansion
  • All characters are still numbers in nature
  • Encoding Unicode 2 bytes a=97, A=65
  • U0000 UFFFF(char c3 = '\u0061';)
  • Escape character \ t (TAB) \ n (newline)
  • Object from memory analysis
public classs Demo01 {
    public static void mian(String[] args){
        char c1 = 'a';
        char c2 = 'in';
        System.out.println(c1);
        System.out.println((int)c1);  //Cast type
        System.out.println(c2);
        System.out.println((int)c2);  //Cast type
    }
}
  1. Boolean extension
  • The code should be streamlined
public classs Demo01 {
    public static void mian(String[] args){
   boolean  falg = true;
        if (flag==true){}  //Novice
        if (flag){}   //an old hand
    }
}

Type conversion

  1. Because Java is a strongly typed language, type conversion is required for some operations

​ byte<short<char<int<long<float<double

  1. In the operation, different types of data are transformed into the same type first, and then the operation is carried out

  2. Cast type

    (type) variable name high - > low

    public classs Demo02 {
        public static void mian(String[] args){
            int i = 128;
            byte b = (byte)i;//out of memory
            System.out.println(i);
            System.out.println(b);
        }
    }
    
  3. Automatic type conversion

    (type) variable name low - > High

    public classs Demo02 {
        public static void mian(String[] args){
            int i = 128;
            double b = i;
            System.out.println(i);
            System.out.println(b);
        }
    }
    
    • Boolean values cannot be converted

    • Cannot convert an object type to an unrelated type

    • When converting high capacity to low capacity, force conversion

    • There may be memory overflow or accuracy problems during conversion

      public classs Demo02 {
          public static void mian(String[] args){
              System.out.println((int)23.7); //23
              System.out.println((int)-45.89f);  //-45
              System.out.println("-------------"); 
              char c = 'a';
              int d = c+1;
              System.out.println(d);  //98
              System.out.println((char)d);  //b
          }
      }
      
  4. matters needing attention

    • When operating large numbers, pay attention to the overflow problem

    • JDK7 new feature, numbers can be separated by underscores

      public class Damo02{
          public static void main(String[] args){
              int money = 10_0000_0000
                  int years = 20;
              int total = money*years;  //-1474836480, overflow during calculation
              long total2 = money*years; //The default is int, and there is a problem before the conversion
               System.out.println(total1);
              System.out.println(total2);
              long total3 = money*((long)years); //First convert a number to long,
              System.out.println(total3);
          }
      }
      

variable

  1. A variable is a variable

  2. Java is a strongly typed language, and each variable must declare its type

  3. Java variable is the most basic storage unit in a program. The key elements include variable name, variable type and scope.

  4. matters needing attention

    • Each variable has a type, which can be either a basic type or a reference type

    • Variable name must be a legal identifier

    • A variable declaration is a complete statement, so each declaration must end with a semicolon

      public class Demo03{
          public static void main(String[] args){
           // int a,b,c  
           // int a=1,b=2,c=3;   (not recommended, program readability)
              String name = "study";
              char x = 'x';
              double pi = 3.14;
          }
      }
      
  5. Variable scope

    • Class variable

    • Instance variable

    • local variable

      public class Demo04{
          //Class variable static is subordinate to class
          static double money = 2500;
          
          //Attributes; variable
          
          //Instance variables; Subordinate object; If initialization is not performed, the default value of this type is 0.0  
          //Boolean default false
          //The default values are null except for the basic type
          String name;
          int age;
          
          //main method 
          public static void main(String[] args){
              
              //Local variables; Values must be declared and initialized
              int i = 10;
              System.out,println(i);  //Output: 10
              
              //Variable type variable name = new Demo04();
              Demo04 demo04 = new Demo04();
              System.oout.println("demo04.age");  //Output: 0
              System.oout.println("demo04.name");   //Output: null
              
              //Class variable static
              System.out.println(money);
          }
          
          //Other methods
          public void add(){
              
          }
      }
      
  6. Naming conventions

    • All variables, method names and class names: see the meaning of the name
    • Class member variable first letter lowercase and hump principle: lastName except the first word, the following word first letter uppercase
    • Local variables: initial lowercase and hump principle
    • Class name: initial capitalization and hump principle student goodstudent
    • Method name: initial lowercase and hump principle run, runRun
    • Constants: uppercase letters and underscores MAX_VALUE

constant

  1. A constant is a value that cannot be changed after initialization

  2. A constant can be understood as a special variable. After its value is set, it is not allowed to be changed during program operation

       public class Demo05{
          static final double PI = 3.14;
            //final constant name = value
            //final double pi = 3.14
            //Modifier, no fore-and-aft order
           public static void main(String[] args){
             d System.out.println(PI);
           }
       }
    
  3. Constant names are usually in uppercase letters

Topics: Java