5
After seeing a question related to this, and many college colleagues with doubts linked to it, I decided to ask this question.
- The guy
int
may equal tonull
?
5
After seeing a question related to this, and many college colleagues with doubts linked to it, I decided to ask this question.
int
may equal to null
?8
Short answer:
int
will never be of value null
Long answer:
In java almost everything is an object, or Object
, but not everything, and what is not an object is a primitive type (primitive
). And only objects can have the value null
.
The difference more summarized is that objects contain a reference to states and behaviors namely variables and methods, respectively. And primitive types have only one value.
Are primitive types:
In case we need to use primitive types as objects we have the class wrapper
, that transforms primitive types so that they can be used in situations where primitive types cannot be operated, only objects. An example of this are the collections (class Collections
) that only operate objects.
The above information may be contradictory if you have already created and/or manipulated some kind of collection using primitive types. This is because java has a feature called Autoboxing and Unboxing
, which makes automatic conversion when we are using primitive types but we could only use objects. That is, makes the class call wrapper
automatically.
Example:
java.util.Stack<Integer> pilha = new Stack();
int num = 10;
pilha.push(num);
The value for num
cannot be null
even after insertion into the stack, because and that was inserted into the stack was an equivalent object (in this case a Integer
) and the num
remains a int
.
Reference 1: Can an int be null in Java?
8
As well said in @pmargreff’s reply, int
under no circumstances will it be null
.
But what if the type variable int
is not initialized, with what value it turns out?
It depends, if the variable belongs to the class it is initialized automatically with the value 0
, if it is a method variable it will not be initialized automatically, for this condition it must be explicitly initialized in the code, otherwise it will not compile.
Similar to the primitive type, Integer-type reference variables of the class instance are also initialized, however as null
, while methods face exactly the same situation as the primitive variable: no build error is implicitly initialized if you try to access its value. Example:
public class TesteInt {
public static void main(String[] args) {
new TesteInt().teste();
}
int iInstancia;
Integer objInstancia;
public void teste() {
int iLocal;
Integer objLocal;
System.out.println(iInstancia);
System.out.println(objInstancia);
//System.out.println(iLocal); //erro de compilação
//System.out.println(objLocal); //erro de compilação
}
}
Exit:
0
null
If the lines indicating erro de compilação
are uncommented, in fact, the code will not be able to be compiled.
Browser other questions tagged java typing
You are not signed in. Login or sign up in order to post.
He added well, I saw now that I ended up escaping a little from the scope : )
– pmargreff