• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass By Value in Java

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


Hi,

In my interview interviewer asked me What is pass by value in java?
I replied that in case of pass by value for primitive types JVM creates a separate copy of memory and assign value into it
and in case of reference type JVM create separate memory of reference type and will assign the memory address value referred by the another reference type variable into it and both
variable point to same object.
was I explained correctly? if I am right then he asked another question what is pass by reference?

Thanks in advance.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, all the values will be sent by Pass by Values. Only difference is, in case of primitive data types, exact values will be passed to methods and in case of objects, memory locations can be passed to methods. Hence, in case of objects, memory locations can be referenced in called methods to get the object references.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might like to go through Cup Size -- a story about variables and Pass-by-Value Please (Cup Size continued) in the Campfilre Stories section of this site.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell Ritchie ,

What I explained to the interviewer was it right? I know Reference Variable stores memory address that JVM understands in bits pattern.
I know Java supports only pass by value.Interviewer wanted to know what is meaning of pass by reference?please anyone explain
with real life example.Sorry, This is not related to Java question but I have not idea about pass by reference.
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general when we say pass by reference we mean that the called method can able to change the contends of the argument/s passed by the calling method.

If you know the above statement that my question for you would be in java when you pass a reference variable to a method then will that method able to change the value or bit pattern stored in that variable ?

Thanks...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passing a value by reference mean you can change the original value.
A few examples from old threads might be worth reading: 1 2 3 (but make sure you have a free week to read this thread).
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Khuzema Dharwala,

"when you pass a reference variable to a method then will that method able to change the value or bit pattern stored in that variable ? "

I think, Yes. Reference variable of that method can refer to the different object.



am i right?

Thanks .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikant Singh wrote:"when you pass a reference variable to a method then will that method able to change the value or bit pattern stored in that variable ? "

I think, Yes.


No.

In Java, variables are always passed by value.

But you have to keep in mind that there are two kinds of variables: primitive variables and reference variables.

Primitive variables are variables that are of one of the following types: byte, short, int, long, float, double, char, boolean.

Reference variables are variables that refer to objects; all variables that are not primitive variables, are reference variables.

Variables are like little boxes that contain a value. Primitive variables simply contain the value that you want them to contain. Reference variables contain a reference (which can be, for example, the memory address) of an object that's stored in memory somewhere. So, referring to the bit pattern that was mentioned by Khuzema Dharwala: In primitive variables the bits just represent the value itself, while in reference variables the bits are a reference to an object, that's somewhere else in memory.

Note that with reference variables, you can have more than one variable that refers to the same object. If you change some value in the object through one variable, then you'll also see the change if you look at the object through the other variable, because the other variable looks at the same object.

When you pass a variable to a method, then it's passed by value - that means that not the variable itself, but only its value is passed to the method, and copied into the argument variable of the method.

Note that in Java, reference variables are also passed by value - that means that when you call a method and you pass it a reference variable, the reference is copied. Note that that is not the same as "pass by reference"!

Did you try running your code? You'll find that in line 32 the same name 'Doggy1' is printed as in line 30. In line 31 you're passing d1 to dogRefMethod. The reference that's contained in d1 is copied into d2, in line 18. But in line 20 you're making d2 refer to a new Dog object, completely separate from what d1 in main() refers to. Changing the name of that new Dog object in line 21 doesn't do anything to the Dog object that d1 in main() refers to.
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Khuzema Dharwala and Jesper de Jong for reply

"when you pass a reference variable to a method then will that method able to change the value or bit pattern stored in that variable ? "



What i have given example is example of pass by value.

Pass By Value:

It means that If we pass reference variable to a method, JVM will create separate copy of parameter reference variable and copy the value(bit pattern) stored in argument reference variable into it.It means that two copies of reference variable of same type with same bit pattern pointing to same object.so if we create an object in called method and parameter reference variable points to new object, argument reference variable still points to old object. So, if you pass a reference variable to a called method then that method would not able to change the value or bit pattern stored in that variable.

Pass By reference:

In this case there is only one copy of the reference variable and called method would able to change the bit pattern stored in that caller method argument reference variable.

Now am I right?




 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by bit pattern? I think you have got it right, but your using the non‑specific term bit pattern makes it difficult for me to be certain about that.
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bit pattern means reference value.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I presume reference value refers to its memory location rather than the contents of that memory location. You are using bit pattern under pass by reference apparently to mean the contents of the memory location.
I still think you are correct, but your explanation is confusing.
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,

Thanks for reply.

reference value refers to its memory location rather than the contents of that memory location




Same thing i wanted to say but you explained beautifully in simple one line.

I want to know how JVM represent memory location internally?

Thanks in advance

 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I want to know how JVM represent memory location internally?

It is not of direct use or importance, but you may of course be interested in it.

A raw comparison: like a pointer in C. Or, given the fact that objects might be relocated during the garbage collection, a pointer to a pointer.
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reasons why Java does not support pass by reference?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Java does not support pass by reference: Probably because it's not really necessary, and the people who designed the Java programming language wanted to keep things simple, so they decided not to include non-essential features that would make the language harder to understand.
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper de Jong,

Thanks for reply.

It means that Java does not support pass by reference because it is a complex topic not related to the actual programming task.That's Java provided JVM for memory management.

correct me if I am wrong?
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> It means that Java does not support pass by reference because

... of the design decision about this.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Memory management does not have anything to do with it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic