Java Doubts - Creating Classes(Objects)

Asked

Viewed 63 times

-1

a question of beginner.. If I create a Minhacalculator Object(Class), the default value of that object will be 0 or null?

Taking into account that I have declared only variables of the whole type in the Class

3 answers

1

All kinds nonprimitive (classes, arrays, interfaces...) have null default value.

All kinds primitive of language NAY start with null value.

This can be seen in documentation:

inserir a descrição da imagem aqui

In your case the MinhaCalculadora is a class, if you just declare it, by default it will have null value.

If you have any of these occasions I mentioned above within the class (for example, MinhaCalculadora has an attribute int, the value of the attribute will be 0 by default)

1

Come on, I’ll try to be as clear as I can.

If I create a Minhacalcular Object(Class), the default value of this object will be 0 or null?

When you create a new object it cannot be null. This is the default language. What can happen is you create a variable with your class type without assigning a value.

MinhaCalculadora minhaCalculadora;

In the above case, if you do not initialize your variable and try to use it later, you will receive an error stating that your variable is null;

Taking into account that I have declared only variables of the whole type in Class

The attributes of your class, those of primitive type, receive, if not initialized explicitly by the programmer, default values. In your case, integers will receive 0.

0

Primitive types such as int, float, and double when they have no defined value hold the value 0, objects hold the value null when they have no defined value. Your Minhacalcular object will have null value until a value is defined for it, because it is an object and not a primitive type. Note: set value = start variable Note 2: For example, Boolean does not receive 0, but false which is equivalent. So long, I hope I’ve helped

  • An object cannot be null. That would be the same as pointing to nowhere. What can happen is a variable with type set by the programmer not to be initialized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.