[JAVA] null specifies the type

class Tips0031 {
  public void someMethod() {

    //When calling an assignment statement or method, it is difficult to understand by simply writing null, so it is better to specify the type.
    //In the following cases, it is not necessary to specify it.
    String str = null;
    //
    //However, when the variable declaration and the assignment statement are separated, it is better to specify them.
    //
    //Other processing
    //
    str = (String)null;
    //
    //Also when calling a method
    otherMethod(null);
    //It is better to do as below than above
    otherMethod((String)null);
    //
  }
  //
  private void otherMethod(String str) {
    //Some processing
  }
}

Recommended Posts

null specifies the type
About the explanation about functional type
Let's understand the Array (Element) type!
Let's understand the Optional (Wrapped) type!
Write a null case using the Optional type without using an if statement
Mockito argument null Assuming test specified the type and did not return
What is the right migration data type? ??
[Java] Convert Object type null to String type
Declare the model as an optional type