Reputation: 2716
What happens if two objects of the same type have identical reference?
For eg -
BufferedReader bufferedReader=null;
try{
bufferedReader = new BufferedReader(new InputStreamReader(a.openStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
//do something
}
bufferedReader = new BufferedReader(new InputStreamReader(b.openStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
//do something
}
}
a
and b
in this case are URLs.
The code I have here is similar to this page. In this case, will the buffered reader object first have the data of URL a
, and later the data of b
?
Upvotes: 0
Views: 1627
Reputation: 6111
It is nothing but similar of doing, Let me explain giving simple example, you will be able to map then in your case.
int a = 10;
a = 2 + 3 ;
sysout(a);
// a have 5 right now, next line will change the contents to 9,
so previous data within a is no longer exist.
a = 4 + 5 ;
sysout(a);
User defined type
class Student{
private int rollNo;
//getter-setter
}
Student s = new Student();
s.setRollNo(10);
sysout(s.getRollNo);
Now, on same reference you are creating object for new Student, then reference to previous Student will be lost and your reference will start pointing to new Student Object.
s = new Student();
s.setRollNo(20);
sysout(s.getRollNo);
Upvotes: 1
Reputation: 16545
I don't believe that you've phrased your question to mean what you really want to ask. To answer your unasked question ...
You have a reference bufferedReader
of type BufferedReader
.
On line 1, you initialise the reference to null
. It doesn't point to an object.
On line 4, you change where the bufferedReader
reference points to. It now points to an object that ultimately sources data from a
.
One line 7 you change where the bufferedReader
reference points to. It now points to an object that ultimately sources data from b
.
will the buffered reader object first have the data of URL a, and later the data of b?
Yes, your code will retrieve the data from URL a, and then retrieve the data from URL b.
What happens if two objects of the same type have identical reference?
That would be more like:
Foo o1 = new Foo();
Foo o2 = o1;
Now, o1
and o2
point to the same object. In that case, invoking a method via either reference will affect the same (single) object.
Upvotes: 1
Reputation: 470
Well think about it this way - each time you're calling the new keyword, you're calling the constructor of the object class after it. And that creates a new object for you on the heap. So in the snippet above, you've done that twice, first you initialize a BufferedReader()
object chained to stream a, for your bufferedReader
reference. Once the while loop completes and you hit the next new keyword, you create another BufferedReader
object chained to stream b, and now your bufferedReader
reference handle points to that.
Java has automatic garbage collection, and one reference can only point to one object on the heap at a time (even an Array is considered its own object). So once you switch the reference from the BufferedReader object chained to a, to the one chained to b, the former one is garbage collected.
Remember that on the left side is declaration, e.g.:
Object obj1;
The right side is a call to create a new object of the provided type, e.g.:
obj1 = new Object();
The equals in between makes your obj1 reference point to that newly created object on the heap. Hope that helps.
Upvotes: 0
Reputation: 121599
The same variable (bufferedReader) refers to one object after the first "new", then it's reassigned to a second, different object at the second "new".
It's also worth noting that the Java garbage collector will free unused objects ... but it WON'T close files that are still opened. The second "new" introduced a resource leak ;)
Upvotes: 0