Junior — Middle
Is it possible to use the assignment operator to set the value of an Integer variable when creating an object?
sobes.tech AI
Answer from AI
Yes, in Java, you can use the assignment operator to set the value of an Integer variable when creating an object. The Integer class is a wrapper for the primitive type int, and thanks to autoboxing, the compiler automatically converts the primitive int to an Integer object.
Example:
Integer number = 10; // autoboxing int to Integer
You can also explicitly create an Integer object using a constructor or a static method:
Integer number1 = Integer.valueOf(10);
Integer number2 = new Integer(10); // deprecated, not recommended
Thus, the assignment operator is quite suitable for setting the value of an Integer when creating an object.