Cabbage Java study room covers core knowledge

1. Situational presentation

Since doing Java development, the IDEA editor is indispensable. There are a number of efficient code Completion features in the IDEA editor, especially the Postfix Completion feature, that make writing code much smoother.

Postfix Completion is also essentially code completion, and it’s a bit smoother to use than Live Templates, as you can see in the following diagram.

2. The Settings screen is displayed

You can use the following methods to open the Postfix Settings screen and enable Postfix.

3. The Postfix template is used frequently

3.1. Boolean variable templates

! : Negates boolean expression

//before public class Foo { void m(boolean b) { m(b!) ; } } //after public class Foo { void m(boolean b) { m(! b); }}Copy the code

if: Checks boolean expression to be ‘true’

//before
public class Foo {
    void m(boolean b) {
        b.if
    }
}

//after
public class Foo {
    void m(boolean b) {
        if (b) {

        }
    }
}
Copy the code

else: Checks boolean expression to be ‘false’.

//before public class Foo { void m(boolean b) { b.else } } //after public class Foo { void m(boolean b) { if (! b) { } } }Copy the code

3.2. Array variable templates

for: Iterates over enumerable collection.

//before
public class Foo {
    void m() {
        int[] values = {1, 2, 3};
        values.for
    }
}

//after
public class Foo {
    void m() {
        int[] values = {1, 2, 3};
        for (int value : values) {

        }
    }
}
Copy the code

fori: Iterates with index over collection.

//before
public class Foo {
    void m() {
        int foo = 100;
        foo.fori
    }
}

//after
public class Foo {
    void m() {
        int foo = 100;
        for (int i = 0; i < foo; i++) {

        }
    }
}
Copy the code

3.3. Basic templates

opt: Creates Optional object.

//before
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
  intValue.opt
  doubleValue.opt
  longValue.opt
  objValue.opt
}

//after
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
  OptionalInt.of(intValue)
  OptionalDouble.of(doubleValue)
  OptionalLong.of(longValue)
  Optional.ofNullable(objValue)
}
Copy the code

sout: Creates System.out.println call.

//before public class Foo { void m(boolean b) { b.sout } } //after public class Foo { void m(boolean b) { System.out.println(b); }}Copy the code

3.4. The Object templates

nn: Checks expression to be not-null.

//before public class Foo { void m(Object o) { o.nn } } //after public class Foo { void m(Object o) { if (o ! = null){ } } }Copy the code

null: Checks expression to be null.

//before public class Foo { void m(Object o) { o.null } } //after public class Foo { void m(Object o) { if (o ! = null){ } } }Copy the code

notnull: Checks expression to be not-null.

//before public class Foo { void m(Object o) { o.notnull } } //after public class Foo { void m(Object o) { if (o ! = null){ } } }Copy the code

val: Introduces variable for expression.

//before
public class Foo {
    void m(Object o) {
        o instanceof String.var
    }
}

//after
public class Foo {
    void m(Object o) {
        boolean foo = o instanceof String;
    }
}
Copy the code

3.5. Other Templates

new: Inserts new call for the class.

//before
Foo.new

//after
new Foo()
Copy the code

return: Returns value from containing method.

//before public class Foo { String m() { "result".return } } //after public class Foo { String m() { return "result"; }}Copy the code