Contenido del Curso
Java OOP
Java OOP
Code Editor Main Features
Features
Let's briefly go through the auxiliary functionality of IntelliJ IDEA:
Error handling
Our task is to write a method that takes a string and sorts all the letters in it in alphabetical order. While writing this method, we will explore the features of our IDE. The method's signature is already provided for us, but IDEA highlights a syntax error for us. If we hover over this error, we can see what the problem is: we haven't added the return
keyword with the method's return type. IDEA also offers us the option to fix this by clicking a single button.
Note
A syntax error is an error that indicates incorrect syntax in the code. For example, if we want to declare a
String
variable and instead ofString variable
, we writeSrting variable
, it will be a syntax error.
Also, looking at the top right corner, we see one syntax error and one warning. By clicking on this button, a console will open, displaying a list of errors in the format: "error name: line where this error is detected." In our case, it's missing return statement: 9
.
Code suggestions
The first thing we do is create a variable of type String
. When we start typing with the letter "S," we see a miracle - IntelliJ IDEA automatically suggests options for us. We can navigate through the suggestions using the arrow keys on our keyboard. To select what we need, we press the "tab" key, and IntelliJ IDEA automatically completes it for us.
Note
Such auto-completion suggestions will occur very frequently and almost for everything. I advise you to try writing different programs on your own, and you will see how much it simplifies and automates the work.
Method suggestions
Next, we need to create an array of type String
and put each character of the string
from the parameter into it. For this, we will use the split()
method. When we type a dot to call the method, a window pops up again, suggesting to use the split()
method. IntelliJ IDEA does all the work for us. On the right, we can see the data type that the method returns, and on the left, the name and parameters that the method takes. We choose the split()
method and proceed further.
Import suggestions
To sort an array in alphabetical order, we need to use the sort()
method from the Arrays
library. We can import this library automatically by simply typing Arrays
, and IntelliJ will automatically import the library for us. All we need to do is press "tab" when IntelliJ suggests importing this library.
Auto-filling loops
We created and sorted the array. Next, we created an object of the StringBuilder
class to convert the sorted array into a String
. For this, we need a for-each loop where we will use the append()
method to add array elements to the string. When we start typing the name of the array, IntelliJ suggests several options - "for" and "for-each". We select "for-each" using the arrow keys, and IntelliJ creates a template for writing the for-each loop for us.
Summary
We wrote a method to sort a string in alphabetical order using the tools provided by IntelliJ. We haven't covered all the tools because there are so many of them. There are so many that even an hour-long video wouldn't cover the entire set of features. So, I'll repeat myself: experiment, practice, and then you'll be able to learn everything and how to use them effectively.
¡Gracias por tus comentarios!