JAVA basic data type initialization

January 07, 2023

Java does its best to ensure that all variables are properly initialized before use. If defined as a "local" variable relative to a method, this guarantee is manifested by a compile-time error. So if you use the following code:

Voidf() {inTI; i++; }

You will receive an error message telling you that i may not have been initialized. Of course, the compiler can also give i a default value, but it looks more like a programmer's mistake, in which case the default value will "help up". If you force the programmer to provide an initial value, you can often help him/her to correct the "bugs" in the program. However, if the base type (primary type) is set to a data member of a class, the situation becomes slightly different. Since any method can initialize or use that data, it may not be a practical practice to force the programmer to initialize it to an appropriate value before the data is officially used. However, it is also very unsafe to give it a garbage value. Therefore, all basic type data members of a class are guaranteed to get an initial value.
You can see these values ​​in the following small program:
//: IniTIalValues.java
// Shows default iniTIal valuesclass Measurement
{booleant;charc;byteb;shorts;inTI;longl;floatf;doubled;voidprint()
{
System.out.println("Data type Inital value"+"boolean "+ t +""+"char "+ c +""+"byte "+ b +""+"short "+ s +""+" Int "+ i +""+"long "+ l +""+"float "+ f +""+"double "+ d);
}
}
publicclassInitialValues
{
Publicstaticvoidmain(String[] args)
{
Measurement d = newMeasurement(); d.print();/* In this case you can also say: new Measurement().print(); */} }///:~

The input results are as follows:

Data type Initalvaluebooleanfalsecharbyte0short0int0long0float0.0double0.0

Among them, the Char value is null (NULL), no data is printed.
As you will see later, when you define an object handle inside a class, if you don't initialize it to a new object, that handle will get a null value.

Musical Coaster

Our Musical Coaster is good in quality and competitive in price. We are manufacturer and supplier of Musical Coaster following your specific requirement. We are looking forward to your E-mail and establishing cooperative relationship with you! We would provide professional Musical Coaster with good services for you!

Led Coaster, Led Light Up Music Coaster, Flashing Led Coaster

AST Industry Co.,LTD , https://www.astsoundchip.com