• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please tell me why Null is the output for the following program?  RSS feed

 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output:
3
null
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, haraprasad. The reason it prints null at Line 18 is that you assign null to arr at Line 14, and nothing in your code ever assigns anything else to arr. So, when Line 18 is executed, arr still contains null.

Clearly, you expected something to change the value stored in arr. What line of your code did you think would change it?
 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we call the method at line 15,arr got a new object and referenced to that new object

so i thought it would show the referene value
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can re-write your code with one minor change:



Would you expect arr to be set to something here?

The point is, the reference variable inside your calc() method has nothing to do with your reference variable outside the method. The fact that you call them the same thing is irrelevant - they are different things.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haraprasad mohapatra wrote:When we call the method at line 15,arr got a new object...


Java passes its arguments by value, not by reference. That means that the calc method received a copy of the value in arr. Now, this may seem confusing, because you have an arr that is local to your main method, and another arr that is local to your calc method. When calc is called at Line 15, the value in main's arr is copied to the arr that is calc's argument. Changes to that argument made in calc only change that argument; they don't change the contents of the variable from which the argument was copied. So, even though the copied value (which is null) is replaced by a reference to an array of integers at Line 6, that only changes the value of the variable called "arr" that is local to calc. It doesn't change the value of the variable called "arr" that is local to main. They are two different variables, one initially receiving a copy of the contents of the other.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure you understand how and why fred's version of your program behaves the same way yours does. If you can comprehend that, you will have the complete answer to your question.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the piece that you're missing here is that when you pass something to a function, it does not directly pass the reference. It passes a copy of that reference. Keep in mind, when the reference is copied it still points to the same actual object in memory, so it IS possible to edit an object even with the copy reference. It's not possible to replace the reference though. In a case like this, passing the null reference in does absolutely nothing.

There are pros and cons to this compiler approach. You can't accidentally mess with a reference (but you still can mess with the underlying object), but on the other hand, you cannot do things like you're trying to do in the OP. Instead, you have to either initiate them in the main, or set it to the return value.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!