This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output:
3
null
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic