search

Home  >  Q&A  >  body text

Why do Java local variables need to be initialized?

Why do Java local variables need to be initialized? As mentioned. Thanks.

巴扎黑巴扎黑2790 days ago610

reply all(2)I'll reply

  • 黄舟

    黄舟2017-05-17 10:00:02

    1. The compiler cannot pass

    2. If there is no initialization, just point to an address similar to c. So the direct compilation of java failed.

    reply
    0
  • 高洛峰

    高洛峰2017-05-17 10:00:02

    Compared with global variables, local variables have a short life cycle and are declared many times. If an initial value is given like a global variable, it will affect performance. Not giving an initial value is unsafe, so we made a compromise and stipulated that users need to first Assign value and use again.

    reply
    0
  • Cancelreply