Why do we create object variables like this? [duplicate] - java

This question already has answers here:
Differences between new Integer(123), Integer.valueOf(123) and just 123
(6 answers)
create a new Integer object that holds the value 1?
(2 answers)
Advantage of using new Integer(a) over a
(3 answers)
using new(Integer) versus an int
(3 answers)
Closed 3 years ago.
I am new to learning Java, and I was told to create object variables like this:
Integer a = new Integer(2);
Instead of like this:
Integer a = 2;
Can someone explain why is creating object variables the 2nd way bad?
edit: I am adding this here cause I am getting mixed answers
Which one am I supposed to use and when?

The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
(Emphasis mine.)
See also: Autoboxing

Related

Comparing 2 complex numbers [duplicate]

This question already has answers here:
Is it possible to overload operators in Java? [duplicate]
(6 answers)
How to compare two double values in Java?
(7 answers)
Closed 2 years ago.
I have created a class for complex numbers and a constructor for the class which takes 2 doubles, like
Complex c = new Complex(3.0,1.0)
Then how do I compare a Complex number to 0? I know that I need to create a tolerance number, say
TOL = 1.0e-10
Do I just do this:
Complex NullComplex = new Complex(TOL,TOL);
c >= NullComplex;
I would like to get a boolean output from this. But whenever I want to compare, I get that the operators "=>" are undefined for the type double, Complex (or Complex, Complex)

what will happen if I change the value of int in Java [duplicate]

This question already has answers here:
When changing the value of a variable in C, is a new primitive created or is the current primitive mutated?
(2 answers)
Closed 4 years ago.
I have a very basic, silly question.
Let's say in a function foo(), I put:
int a = 3; // after this, I know it will allocate a space in stack and put 3 in it.
Then, I put a = 5; // Here is my question:
What happened after I put a = 5?
Will a new space is allocated and put 5 in it? Or it will find out the new space in stack and then put 5 in it?
No it will not create new space just change value but if you pass this variable to a function will create new space as argument

How method .toArray(IntFunction<A[]> generator) knows the size of new Array [duplicate]

This question already has answers here:
Java 8 stream's toArray and size parameter
(3 answers)
Closed 5 years ago.
I'm looking for the answer for a question: how method .toArray(IntFunction<A[]> generator) knows the size of new Array.
Actually I know how to use this method to create new Array that contains all of stream elements (e.g. String[]::new, Size -> new String[Size]), but in the original java code we can see that the IntFunction<A[]> generator applies given function to int argument. And there is my question HOW this function gets the number of elements of the stream.
I've been reading the source code of this classes for 3 hours, but I did not find the answer.
the answer to your question is stated in java docs.
toArray uses the
provided generator function to allocate the returned array, as well
as any additional arrays that might be required for a partitioned
execution or for resizing.
The generator function takes an integer, which is the size of the
desired array and produces an array of the desired size.

Java Newbie - When to use it? Relating to Boxing/Unboxing I think [duplicate]

This question already has answers here:
Which is better: letting Java do autoboxing or using valueOf()
(3 answers)
Closed 6 years ago.
Integer x = 5;
Integer x = Integer.valueOf(5);
Is there any scenario where I would want to use the 2nd one specifically or is it redundant altogether and shouldn't not bother about it?
After Java 5 (because of autoboxing / unboxing) there is no difference except the first one is shorter.
Both statements are equivalent.
The statement Integer x = 5 would be compiled to
Integer x = Integer.valueOf(5);
The compiler will do that for you behind the scene, so the only difference is the number of character in source file.

How is unboxing done in Java? [duplicate]

This question already has answers here:
How does auto boxing/unboxing work in Java?
(4 answers)
Closed 7 years ago.
I know how boxing is done. For example, when you write Integer a = 4, Integer.valueOf(int) is being called. Likewise, can i see the implementation code for unboxing as well?
Update : It is answered in the suggested question.
In such cases,intValue() gets called to get the primitive value.
Integer a= new Integer(4);
int val = a.intValue();
Like the integer, all the wrapper classes have this method to get it's primitive value.

Categories