We have listed the patterns of problems that were mistaken due to misleading or simple mistakes when preparing for the Oracle Java SE8 Silver exam.
Array
int[] nums;
nums = {1, 0}; //Successful compilation / execution
nums = {1.0}; //Compile error
It is a pattern in which the third line is mistaken for a period and a comma. There will be a space after the comma, so be careful and you won't make a mistake.
String
String hoge = " ho ge ";
hoge.trim();
System.out.print(hoge); //"Ho ge" is output
A pattern that calls String methods (especially trim, replace, etc.) but does not reassign them to variables. The above example omits other processing, but if it is buried in other string operations, it will be overlooked unexpectedly.
StringBuilder
public String hoge(){
StringBuilder huga = new StringBuilder("huga");
return huga.append(" huga");
}
It is a pattern that casts StringBuilder to String without permission in the brain after performing a character string operation. There are multiple choices, so be careful about the return type, especially when using StringBuilder.
Variable name
class Hoge {
int a;
void huga() {
int a;
...
System.out.println(a);
}
}
It is easy to understand because it can be color-coded in the IDE, but it is easy to forget the existence of local variables in a single color. If you refer to a field, this is attached, so it's easy to understand ...
Inheritance inheritance
abstract class A {
abstract void hoge();
}
abstract class B extends A {
void huga() {};
}
class C extends B {
}
In this case, class C will result in a compilation error. The cause is that it doesn't implement the class A method hoge. Class B inherits from class A, but since it is an abstract class, it does not cause an error even if hoge () is not implemented, Class C is a concrete class and needs to be implemented.
In the above case, the no-argument constructor is not automatically generated. It's easy to overlook it when it's called, but it causes a compile error.
ToString is called at the time of string concatenation and is implicitly converted to String. I often convert explicitly, and I feel that I haven't had the opportunity to rely on implicit conversion. (There is a problem of combining StringBuilder type and String type with + operator, but will it be useful day?)
I didn't know that there aren't many situations where you have to specify the insertion position and you can specify it with the add method. ~~ I think it's more practical to learn how to read the reference than to memorize the methods one by one. ~~
Is there a pattern that should not be processed internally?
I learned a lot of patterns that I haven't touched on before, but it's a little subtle if it can be used in practice. There are many subjects such as anti-patterns that work through compilation, and I feel that it is problematic for beginners to learn such patterns as "compilable and executable". Therefore, I think that this exam should be taken after understanding that the main purpose of this exam is to deepen the understanding of the language and not to be able to write a good program. ~~ And I realized the greatness of IDE and reference. ~~