Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize int to 0 or not?

Tags:

In the android source code I see they define four variables as

protected int mPaddingRight = 0;
protected int mPaddingLeft = 0;
protected int mPaddingTop;
protected int mPaddingBottom;

In Java, what is the difference in initializing a variable to 0 or not? I don’t understand that in some compilers I cannot do a comparison unless I initialize the field. But that is not the case here. Does this have to do with optimization? Or is this just inconsistent/bad coding practice?

like image 925
Nouvel Travay Avatar asked Apr 26 '16 01:04

Nouvel Travay


People also ask

Is an int initialized to 0?

No difference - int members are initialized to zero by default.

Is an int initialized to 0 C++?

Well, int's are value types in both c++ and C#, they use X bytes of ram, and all 0 bits, they are 0. So whether you initialize them or not, in memory they are still 0.

Are ints initialized to 0 in Java?

Initialize Array Elements to Zero in JavaBy default in Java, data types like int, short, byte, and long arrays are initialized with 0. So, if you create a new array of these types, you don't need to initialize them by zero because it's already their default setting.

Why do we initialize 0?

Named variable with static or thread-local storage is initialized to zero. It is used as initialization of values for non-class types and members of a class that do not have a constructor. It is used to initialize a character array when its length is greater than the number of characters that are to be assigned.

Does Java initialize int 0 by default?

Does Java initialize int 0? By default in Java, data types like int, short, byte, and long arrays are initialized with 0. So, if you create a new array of these types, you don't need to initialize them by zero because it's already their default setting.

What happens when you zero initialize an int in C++?

What happens in practice is that you cannot rely on the result of that calculation. In C++, non-static or global built-in types have no initialization performed when "default initialized". In order to zero-initialize an int, you need to be explicit:

How to initialize variables with 0?

They'll be initalized with 0 implicitly. However if you use good IDE or have other tools, it would be really easy for you to search and replace = 0; with = SomeOtherValueHere;. Also I think it is a good practice to always initialzie your variables before you access them.

Does Java automatically initialize array to 0?

By default in Java, data types like int, short, byte, and long arrays are initialized with 0. So, if you create a new array of these types, you don't need to initialize them by zero because it's already their default setting. Does Java automatically initialize variables?


2 Answers

According to Java primitive data types turorial , all primitive data types have a default value. So the initialization it's implicit. A good practice: initialize values before using to prevent unexpected behavior.

byte    0
short   0
int 0
long    0L
float   0.0f
double  0.0d
char    '\u0000'
String (or any object)      null
boolean false
like image 128
F.Igor Avatar answered Oct 06 '22 23:10

F.Igor


It is a good coding practice to initialize variables.

From Oracle Docs:

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The benefits of initializing the variables are as following:

  1. Makes it easier to follow your code
  2. Makes life easier for static analysis tools.
  3. Most of the prevalent design patterns asks you to initialize variable to a default value, so that the programmer knows exactly to which value the variable is initialized.
  4. It is always good practice to initialize variables to prevent undefined behavior later in the program.
  5. Debugging becomes easier if you initialize the variables.
like image 44
Pritam Banerjee Avatar answered Oct 06 '22 23:10

Pritam Banerjee