There is a variable that is simply declared:

int a; 

And there is a need to check whether it has already been initialized. How can I do that?

  • 3
    The compiler will check and refuse to compile the program, giving an error. Independent verification does not make sense and therefore there is no way for such verification. - Sergey
  • @Sergey sorry no :( thanks - Semyon Shnurkov
  • Why do you need it? - Roman
  • one
    @ SemyonShnurkov, you write: "it is a pity that it is not :(" --- if it causes you regret, then you most likely don’t understand something about variables. It’s hard to imagine a situation where it would be needed. But if it is not a local method variable, but an object field, it will automatically be initialized to 0 by default. If you declare both, you can add initialization: int a = 1234567; and if this is a value that cannot be assigned elsewhere in the program, then you can check - if this value is preserved - it means that nothing is assigned valos -. m vokhm.
  • @ m.vokhm I have to say right away, for the time being I have just started to learn java. Before that I wrote in php, and there were functions for this, which I often used. Consequently, I had a lot of situations where the initialization check was really needed (maybe, of course, it's about me). Assign any random value of type int a = 1234567; , and then check it, for me somehow a crutch, because there is any likelihood that in the future this value will be assigned. Thanks for the answer, I will try to avoid this problem altogether :) - Semyon Shnurkov

2 answers 2

As I know, Java, unlike C ++ languages ​​and below, does not in itself initialize variables ...

Suppose if in C ++ you write: int a; and not to initialize it, after compiling and displaying on the screen, you can see that your variable was automatically initialized with a certain number taken from the main memory.

But in Java, as I know it is not. And if you try to use your non-initialized variable or object in conditions, the compiler will not compile your code, well, or throw a NullPointerException ... Therefore, so that being used to use it in conditions and so on, it’s better in advance set a constant to this variable or 0 but be 100% sure that this variable will never assign your constant or 0, and then check whether the variable has changed or not ...

Something like that:

 int a = -999; //code //ДСйствия Π½Π°Π΄ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ a //code if(a != -999){ System.out.print("Ввоя пСрСмСнная Π±Ρ‹Π»Π° ΠΈΠ·ΠΌΠ΅Π½Π΅Π½Π°!"); } 
  • Java Π² ΠΎΡ‚Π»ΠΈΡ‡ΠΈΠΈ ΠΎΡ‚ языков Π‘++ ΠΈ Π½ΠΈΠΆΠ΅ сам ΠΏΠΎ сСбС Π½Π΅ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Π΅ - it initializes, but not all. - post_zeew
  • int default int will be initialized to 0, and the Integer to null. Because int is a simple type, and Integer is a class. - Igor Kudryashov
  • @IgorKudryashov; @post_zeew, by default, only the class fields are initialized; the rest of the compiler will force you to initialize! And even int will be initialized manually int int = 0; - JVic
  • @IgorKudryashov, Not always. - post_zeew
  • @ Victor, this is what you? - post_zeew

You have only one option that the variable will not be initialized: If it is an object variable and it is a class field. Then it will be enough to check for null. Primitive types, if they are class fields, will be initialized by default. If the variable is declared in code, the compiler will not allow you to work with it until you initialize it, in most cases

  • one
    In this case, the variable will be initialized. null is initialization too. - post_zeew