CodeGym /Courses /JAVA 25 SELF /IDEA Lifehacks for Java

IDEA Lifehacks for Java

JAVA 25 SELF
Level 14, Lesson 0
Available

1. Autocomplete (Tab)

IntelliJ IDEA is a very smart development environment. Some consider it not just a great IDE for Java development, but the best IDE in the world in general. Which, in fact, is not far from the truth.

For example, IntelliJ IDEA has a fantastic feature called autocomplete. IDEA parses and analyzes all the files in your project (including JDK 25 libraries) and understands what classes, methods, and variables you have.

You just start typing a word, and it immediately offers suggestions. One of the most important keys is the «Tab» key.

For example, if you want to use the dynamic array ArrayList, you just need to type Arr and press "Tab": IDEA will auto-complete the class name entirely.

If you put a dot after the object's name, IDEA will offer a list of methods and fields available for this object. This is very convenient so you don't have to memorize all methods by heart.

For example, you created a text variable. You don't have to remember the exact name of the method to convert text to uppercase. Just write the variable name and put a dot:

Important! First, we write the variable name text, and only then put a dot. You will see the available methods in the dropdown list.

CamelHumps

Let's say you want to type the name of a long class, for example InputStreamReader. IntelliJ IDEA allows you to save time: you can type just the capital letters — ISR (InputStreamReader) and press Tab. IDEA transforms this abbreviation into the full class name. It's almost magic.

2. Live Templates

IntelliJ IDEA has thousands of code snippets called Live Templates. Here are the most popular ones for a Java developer:

Main method - psvm

Typing public static void main(String[] args) manually is slow and boring.
Just type the 4 letters psvm and press Tab. IDEA will replace it with:

public static void main(String[] args) {

}

It's easy to remember: public static void main.

Console output - sout

There is also a way to quickly type System.out.println();

Type the 4 letters "sout" and press Tab.

Also easy to remember: system.out.println

Variable output - soutv

A very useful template. If you have a variable and you want to print its value to the console:

var age = 25;
// Type soutv and press Tab:
System.out.println("age = " + age);

Loop - fori

You can quickly write a classic index-based loop using the fori + Tab command. IDEA will generate the skeleton:

for (int i = 0; i < ; i++) {
}

Iterating over a list - iter

If you have a list or array, use iter + Tab. IDEA will create a for-each loop:

for (String arg : args) {

}

Note for the future - todo

Leaves a special comment that is highlighted in IDEA with a bright color and added to the task list (TODO tab at the bottom of the screen):

// TODO: implement this method later

Critical note - fixme

Similar to TODO, but used for places where the code works incorrectly or requires an urgent fix. IDEA highlights it even brighter to attract attention:

// FIXME: an error occurs here, needs fixing!

3. Postfix Completion

In Java 25 we often use the var keyword, and Postfix Completion helps to write code from left to right without going back to the beginning of the line.

You write an expression, put a dot, and select a template:

  • .var — Creates a variable, automatically substituting the type or var.
  • .sout — Wraps the expression in System.out.println(...).
  • .not — Inverts a boolean expression (adds !).
  • .for — Creates a loop over a collection.

Examples:

You type Press Tab — you get
"Hello".sout System.out.println("Hello");
new Scanner(System.in).var var scanner = new Scanner(System.in);
isValid.not !isValid

4. Surround With

Sometimes you need to wrap already written code inside an if condition, a loop, or a try-catch error handling block.

  1. Select the lines of code.
  2. Press Ctrl+Alt+T (Windows/Linux) or Cmd+Opt+T (macOS).
  3. Select the desired construct from the list (if, while, try/catch, etc.).

Example menu in IDEA:

5. Safe Rename: Shift+F6

Never rename variables, methods, or classes by simply deleting text! If a variable is used in 10 places, you will have to find them manually.

Use Rename Refactoring:

  1. Place the cursor on the variable name.
  2. Press Shift + F6.
  3. Enter the new name and press Enter.

IntelliJ IDEA will automatically find all usages and carefully rename them.

6. Search Everywhere: Shift + Shift

If you lost a file or forgot where a setting is located in the menu — press the Shift key twice in a row.

The Search Everywhere window will open. Start typing the class name or setting (for example, "Font size"), and IDEA will instantly find it.

7. Local History

Imagine: you deleted a piece of code, clicked "Save", closed the file... And then realized you deleted too much. Ctrl+Z won't help anymore.

Local History will save you. IDEA saves versions of your code every few minutes.

  1. Right-click in the code editor.
  2. Select Local History - Show History.
  3. Find the needed version on the left and click the Revert button.

8. Code Style

Very often, copying results in broken code formatting: crooked indents, extra spaces. Java has a strict formatting style.

To format the code properly, press: Ctrl+Alt+L.

Example:

Before After
public class Main {
   public static void main(String[] args) {
System.out.println("Hello World!");
   if(2<3){
System.out.println("Oops"); }

for(int i=0;i<10;i++){
System.out.println(i);
    }
   }
}
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        if (2 < 3) {
            System.out.println("Oops");
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

Style settings can be found under Settings - Editor - Code Style:

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION