-1

so I think I'm missing something. I am aware that

  1. If no constructor is supplied java makes one for you.
  2. If there's a constructor defined, default constructor by java is not used.
  3. Constructor is used to initialize variables

Here's some simple code:

class a {

    int f; // a variable with no value
    int c; // a variable later initialized by the constructor
    int b = 5; // this will be second question, a less important one

    a(){
        c = 1; // Constructor initiatives C, but not F
    }

    public static void main(String[] args){
        a var = new a();
        System.out.print(var.f); // Please see my comment below
    }


}

Here's what I do not understand. Why is var.f printed? I did not initialize f in the constructor, however, there's no compile error and 0 value is initialed. I don't understand how '0' is initialized to 'f' despite me not having used it in constructor

Regarding b = 5, I understand what this code leads to, however, I do not think I understand what/who does the initialization here, is it new operator or something else? Thanks.

Edit: since the answers so far are not addressing my question


I am aware of the default values. I thought it was the default constructor that assigned them, is it not? If not, what assigns default values?

user207421
  • 305,947
  • 44
  • 307
  • 483
Sam
  • 79
  • 6

4 Answers4

0

Java like most of programming languages has default values for uninitialized variables. Every numeric type of variable is initialized to 0-related value. Boolean is false as default. Strings and all of the objects have null as their default value.

Check docs for more info: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

itwasntme
  • 1,442
  • 4
  • 21
  • 28
0

int is a primitive data type. By definition, primitives cannot be null as they are not objects and will have a default value if not initialized. See here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

If you want to be able to have a variable that is not initialized, you can use the object equivalent of an int java.lang.Integer

0

Instance variables that are declared but not initialised are given a default value.

References take a default value of null.

Primitives take a default value of zero

In regards to your query on your primitive int variable b, the new operator is not required, the new operator is used when instantiating a reference. When an instantiating a reference an object is created and memory is allocated on the JVM for that object.

Strings are a reference variable, but may be instantiated using the new keyword for example:

  String example = new String("abc");
  System.out.println(example); // prints abc.

Usually you would just write:

  String example = "abc";

In the latter the literal is placed in to the 'string pool'... You can read more about the string pool here: http://examples.javacodegeeks.com/core-java/lang/string/java-string-pool-example/

RamanSB
  • 1,162
  • 9
  • 26
0

If no constructor is supplied java makes one for you.

Correct.

If there's a constructor defined, default constructor by java is not used.

It is not generated.

Constructor is used to initialize variables

Correct.

c = 1; // Constructor initiatives C, but not F

Untrue. Your code initializes c. The generated constructor initializes both.

I did not initialize f in the constructor

No, but Java did.

Java generates the following code for a constructor:

  1. A super call.
  2. Default initialization code for all variables declared without initializers. The default values are false, zero, or null as appropriate to the type.
  3. Calls to all anonymous initializer blocks. (2) and (3) happen in textual order and can therefore be interleaved with each other.

Regarding b = 5, I understand what this code leads to, however, I do not think I understand what/who does the initialization here, is it new operator or something else?

See above.

I am aware of the default values. I thought it was the default constructor that assigned them, is it not?

No.

If not, what assigns default values?

The constructor. Any constructor.

user207421
  • 305,947
  • 44
  • 307
  • 483