1. Autocomplete (Tab)
IntelliJ IDEA is a very smart development environment. Some people believe that it not just a great IDE for Java development, but the all-around best IDE in the world. Which, admittedly, is not far from the truth.
For example, IntelliJ IDEA has this magnificent thing called auto-completion. IntelliJ IDEA parses and analyzes all the files in your project (including all the libraries you are using). It understands what classes you have and what methods and variables those classes have. Then it uses all that information to help you write your code.
You just start typing a word, and it immediately offers hints to finish it. One of the most commonly used autocomplete keys is Tab.
For example, suppose you want to write "System". To do this, you need to type "Sys" and press the "Tab" key: IDEA will finish the rest for you.
If you put a period after the name of a variable, then IDEA will first determine what type the variable is, and then offer you a list of methods that can be called on the variable. This is super convenient.
Or suppose you want to write the class name InputStreamReader. IntelliJ IDEA can save you time: you can just type three capital letters "ISR" (InputStreamReader) and press Tab. IDEA will convert what you wrote into InputStreamReader. It's almost magic.
4.2. Live Templates: psvm, sout, psfs, fori
There are thousands of reasons why professional programmers adore IntelliJ IDEA, but it also has something for beginners. For example:
main method
Let's say you want to write public static void main(String[] args)
in less than a second.
To do this, type the 4 letters psvm and press Tab. IDEA will replace "psvm" with "public static void main(String[] args)". Magic.
It's super easy to remember the sequence psvm — its an abbreviation of "public static void main".
Console output
There is also a way to quickly write System.out.println();
To do this, you also need to write 4 letters ("sout") and press Tab. IDEA will replace "sout" with "System.out.println();"
This is also easy to remember: the 4 letters in sout come from System.out.println
Declaring a string constant
There is a way to quickly declare a String variable (at the class level).
To do this, you once again need to write 4 letters ("psfs") and press Tab. IDEA will replace "psfs" with "public static final String".
You won't have trouble remembering this: psfs is made up of the 4 letters from public static final String
There is a similar abbreviation that can be used with any data type: "psf", which stands for public static final
Loops
You can quickly write a loop in IDEA using the fori + Tab combination. When you do this, IDEA will replace fori with the following code:
for (int i = 0; i < ; i++) {
}
You just need to write in the maximum value for the i counter variable.
These may not be the most popular tricks for working Java professionals, but they will definitely make your life easier as a beginner.
3. Surrounding code fragments: Ctrl+Alt+T
Sometimes programmers face situations where they want to do something with the code that has already been written. IntelliJ IDEA makes this easier by providing a special command to wrap existing code with something.
For example, suppose you want to execute some code not once, but several times. It makes sense to create a loop and place the code inside the loop. You can, of course, write the loop header and put a curly brace at the beginning and another curly brace at the end. Then you could also copy the required code into the loop, and go through all the lines inside the body of the loop and shift them to the right.
But there is an easier way: you can surround the existing code in a loop, and IntelliJ IDEA will do the rest by itself. You will need to do 3 things:
- Highlight the code you want to surround with other code
- Press Ctrl+Alt+T
- Select one of the options for adding surrounding statements:
- if
- if-else
- while
- do while
- for
- try catch
- try finally
- try catch finally
- synchronized
Here's an example of a menu in IDEA:
4. Code style: Ctrl+Alt+L
And one more piece of advice. Quite often, copying the code messes up its alignment: there are extra spaces in one place, while they are missing somewhere else, etc. The code seems to work, but it looks like a train wreck.
To keep your code looking great, you just need to press 3 buttons: Ctrl+Alt+L
As soon as you press this combination, IntelliJ IDEA will immediately format all the code in the current file, placing spaces and tabs wherever necessary. It will no longer be code, but eye candy.
Example:
Before | After |
---|---|
|
|
By the way, each "code style" is fully customizable in the IntelliJ IDEA settings:
GO TO FULL VERSION