Tuesday 15 July 2014

java - Explicit initialization of primitives -


I understand that Java primitives are the default starting value; For example 0 int .

From my research it seems that I should not rely on these values. Should I always give a clear initial look and if so, where?

Here is a part of my program:

  public class calculator {// Start price where it has been declared? Private int values; Public Calculator () {// Start Price in Constructor? } Public Zero Addition (int other) {value + other; }}    

Using basic values ​​created by Java is absolutely fine, you do not need it Let's start the example of your class or the stable field. Java language specificity is required, so any implementation of language should follow. In other words, if you use the default value of member variables, your code will remain 100% portable. This applies to both types of primitive and context (later started with null .)

For more information, see page 81.

4.12.5 The starting value of the variable

Before each variable can be value in a program, its value must be: - Each class variable, instance variable, or array component Is started with the original value when it is created

The only case where you must start clearly, when you declare local variables:

Before a local variable is used explicitly, an original If need be, by early or assignment, in a certain way, which can be verified by using the rules of certain assignments.

However, because failure to launch a local is a compile time error, the compiler points to every missing start-up before running your program.

No comments:

Post a Comment