Java is Strictly Pass by Value!

Recommended Answers

All 14 Replies

Ok. You have a question? Tell us something we don't already know...

Can u explain
Changes are reflected back if we do not assign reference to a new location or object:
Illustrate this!

Yes, Java always passes parameters by value.
But sorry, your question is not clear. Please explain exactly what you want to know.

Under the covers, all Java objects are references. When you assign one variable to another, the reference is copied, not the object's data. At that point, both variables hold a valid reference to the the object and the actual object data cannot be garbage-collected until both variables are cleared (set to NULL or another object reference). This is one reason why "dangling references" are such a problem in Java as well as C++. Caveat programmer!

We are all guilty of speaking a kind of shorthand about objects and variables. Most of the time it's OK, but when we talk of call by value etc it really matters.
An object is an object. A variable is not an object and an object is not a variable.
What we call an object variable is correctly a reference variable. It holds a reference to an object (or null).
So "all Java objects are references" is well-intended, but strictly false. It's the variable that's a reference, not the object.

commented: Good explanation +7
commented: Talk objectively to me. +1 +12

JamesCherrill
Thanks ! Could uou please help me with the example via programm .It will be more clear for me.

Thanks in advance.

JamesCherrill
Hi James ,I want to become a good programmer as I am beginner .I just want to start in right way how to start by giving my giving effort .I want your help .
Could you provide me the direction .
Please could you help me.I will be very thankful to u for this.

I want a clear picture How internally the Hash Map works in Java?

I just want to start in right way how to start by giving my giving effort

Excellent. Glad you want to show efffort and start off on the right foot. However, your posts are coming off as a bit demanding and vague. Not saying that was your INTENT. Just saying that's how it came across, at least to me. Putting in effort requires that you spend time crafting your post to make it as clear as possible and in a way that we can quickly figure out what you want.

How internally the Hash Map works in Java?

This isn't the way to get help and this isn't a beginner question. How ANYTHING works internally isn't a beginner question.

Java is Strictly Pass by Value!

Ditto this "question" in the initial post. If it's a question, there should be a question mark somewhere. If it's not a question and it's intended to teach other people something, all the non-newbies already know this and no newbie can learn anything from this because you don't expand on the concept. So what is the point?

Step back, figure out what you want to accomplish with these posts, and start a new thread with a question that people can actually understand and answer please.

An example of a way you might rephrase your initial post might be something like the following...

Hi. I am a beginner in Java and I am trying to understand why I cannot pass by reference in Java like I can in C++. Googling "pass by reference in Java", I came up with this article...

http://javadude.com/articles/passbyvalue.htm

You could then continue by briefly explaining where you didn't understand something in the link and asking for clarification on it.

What this does is...

  1. Show you've put in some effort and googled the sources out there as opposed to simply posting here with no effort.
  2. Gives us a good idea of what your level is and exactly where you are stuck.
  3. Prevents us from simply replying with that link and telling you to read it because you already HAVE read it.

Ditto something like the Hash Map question. I'm not inclined to write several paragraphs on the subject for someone who may not have done any research on his/her own.

Have a look at this link. Should be required reading for forums. You'll get better responses if you follow the advice in it.

http://www.catb.org/~esr/faqs/smart-questions.html

Thanks for the suggestion.I will keep in my mind hereafter for asking any queries.

You'll do ok Kasis if you remember that you can find most of your answers on the Internet. Post here when you are totally bumfuzzled (confused). We will be happy to help then.

FWIW, a map is a collection of key/value pairs that is indexed on the key. A hash-map is a map where the keys are hash values of something, possibly the value, but not necessarily. In any case a map consists of unique keys - duplicate keys are not allowed, although duplicate values are allowed if necessary.

Just to confuse you (bumfuzzle you) a bit, there are also multi-maps... Maps with duplicate keys for one or more values. Here is the cplusplus.com page on them: http://www.cplusplus.com/reference/map/multimap/
Myself, I try to keep away from them - FAR away! :-)

Thanks so much Rubberman

This thread is tagged Java, so let's assume that is the context.
HashMap in Java is a class in the standard API and it's definitive documentation is here
It's one implementation of the Java API's Map interface - documented here
The definitive Java Tutorials also explain Maps in more detail, and include a section on how to create a multimap. (I don't know why C+ multiimaps should be problematical. In Java they are very common and straightforward.)
Finally - don't ask about the internal implementation of Java Maps - only the public interface is defined so the implementation can, and does, change without notice between Java versions.

commented: Seems the internet can't find the C++ definition of multi-map now... :-( +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.