Avoid redundant initialization

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: java-best-practices/redundant-initializer

Language: Java

Severity: Warning

Category: Performance

Description

When initializing fields, prevent initializing fields to the default value. Any additional initialization means more bytecode instructions, and allocating many of these objects may impact your application performance.

If you initialize to a default value, remove the initialization.

Non-Compliant Code Examples

class Test {
    boolean b   = false;
    byte by     = 0;
    short s     = 0;
    char c      = 0;
    int i       = 0;
    long l      = 0;
    float f     = .0f;
    double d    = 0d;
    Object o    = null;

    MyClass mca[] = null;
    int i1 = 0, i2 = 0;
}

Compliant Code Examples

class Test {
    boolean b                = true;
    byte by                  = 1;
    short s                  = 2;
    char c                   = 3;
    int i                    = 4;
    long l                   = 5;
    public final boolean b   = false;
    final long l             = 0;
    float f                  = .1f;
    double d                 = 10d;
    Object o                 = new Object();

    MyClass mca[] = new MyClass[3];
    int i1 = 1, ia1[] = new Something[];

}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis