Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 7 developers from Canada and 18 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


Java----C/C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
48 replies to this topic

#21 Photonman   Members   -  Reputation: 174

Like
0Likes
Like

Posted 04 May 2006 - 02:31 PM

To clear up the ambiguity before people FLAME ME, I was referring to POD types. Sorry if I thought that would be caught from context, considering the examples in my post only dealt with ints........

I just find not being able to change multiple POD (happy?) values with a function and have those changes apply to the scope that called the function without anything more complex than adding a couple of ampersands too limiting for my taste. I didn't expect the Spanish Inquisition......

Sponsor:

#22 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 04 May 2006 - 03:32 PM

Primitives (int, float, arrays etc ) are passed by value. Objects are passed by reference.

Ste

#23 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 09:10 PM

Quote:
Original post by stecal849
Primitives (int, float, arrays etc ) are passed by value.
Ste


Are you sure about arrays? Somehow I don't think so.



#24 rohde   Members   -  Reputation: 432

Like
0Likes
Like

Posted 04 May 2006 - 10:00 PM

Quote:
Original post by Anonymous Poster
Quote:
Original post by stecal849
Primitives (int, float, arrays etc ) are passed by value.
Ste


Are you sure about arrays? Somehow I don't think so.


In Java arrays are pure objects and hence also passed by reference.

Actually when comparing Java and C++ it's not enough to say that Java passes objects by reference since we have pass by reference in C++ as well, but the semantics are different than in Java.

The 100% correct terminology is to say that Java passes references by value.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog

#25 Emmanuel Deloget   Members   -  Reputation: 1381

Like
0Likes
Like

Posted 04 May 2006 - 10:37 PM

Quote:
Original post by Anonymous Poster
Basically, follow these rules with pointers:
-initialise all pointers to NULL, or to a value returned from 'new'.
-allways check if a pointer is not NULL before using it.
-after an object has been deleted, set all pointers which were pointing to it to NULL.
- When using 'new', ALLWAYS document who will keep copies of the pointer to the object, who is responsible for deleting the object, and how all pointers will be set to NULL when the object is deleted.

These are very good hints for C++ newcomers. I'd like to emphasis them more.

Quote:
Original post by Anonymous Poster
Going from Java to C++ is a LOT harder than going from C++ to Java.
Too many people who come from managed languages to C/C++ end up doing things like this -
char* myString;
char* otherString = "some text";
strcpy( otherString, myString );
Which will crash, or worse, wont crash and will just corrupt random memory.

Yes - that's why C++ newcomers should be teached std::string before C-style ASCIIZ strings. When I give C++ course (yeah, it happens), that's what I do.

You are the kind of AP I really like - the reason why AP should not be banned from this forum. Thanks a lot :)

#26 Fred304   Members   -  Reputation: 382

Like
0Likes
Like

Posted 05 May 2006 - 01:37 AM

There is no pass-by-reference in Java, Java only knows pass-by-value.

This means that changes to the contents of the formal parameters do not affect the actual parameters. An example:

public class Test
{
public static void main(String[] args)
{
int a = 10;
LinkedList l = new LinkedList();
javaPassesByValue(a, l);
assert a == 10;
assert l.isEmpty();
}

public static void javaPassesByValue(int x, Collection c)
{
x = 5;
c = new ArrayList<String>(100);
c.add("test");
}
}

In other words, you cannot change the reference. As already pointed out, the reference is passed by value.


#27 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 05 May 2006 - 03:49 AM

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Pass by Value

Primitive arguments, such as an int or an array, are passed by value, the rest are passed by reference. When invoked, a method or a constructor receives the value of the variable passed in and the method cannot change its value.

No pass by reference?


According to java doc there is and its used on ints or an array.

Simply google something if your not sure instead of posting half truths.

Ste

#28 RayNbow   Members   -  Reputation: 191

Like
0Likes
Like

Posted 05 May 2006 - 04:40 AM

Like Fred304 said, arguments in Java are pass-by-value.
Everything you want to know about arguments in Java can be read in this article.


Quote:
Original post by Anonymous Poster
Java and C++ both have the 'new' keyword, but in java, the only way to create an object, is with 'new'. 'new' dynamically allocates memory on the heap...

In Java, the "new" keyword doesn't necessarily allocates memory on the heap. It depends on the JVM how memory allocation is done. While (most?) current JVMs allocate memory on the heap, future JVMs will most likely also have stack allocation. For more info, please read Java theory and practice: Urban performance legends, revisited; Allocation is faster than you think, and getting faster.


#29 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 May 2006 - 05:04 AM

Quote:
Original post by Fred304
In other words, you cannot change the reference. As already pointed out, the reference is passed by value.


It's perfectly correct to say that in Java all objects are passed by reference. If you change some attribute of the passed class that you'll be changing it in the same instance passed. This is useful in real world.

Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method? The only people I found complaining about this were C++ developers trying to find a pretext for bashing Java.






#30 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 May 2006 - 05:10 AM

Quote:
Original post by RayNbow
Like Fred304 said, arguments in Java are pass-by-value.
Everything you want to know about arguments in Java can be read in this article.


Technicalities. In practical terms, you will get the reference to the same object.

So the claim that originated this flow of e-mails that this wasn't possible and you always needed to use "return" for getting any value is wrong, and in real world work is that what matters.



#31 _goat   Members   -  Reputation: 804

Like
0Likes
Like

Posted 05 May 2006 - 05:11 AM

Quote:
Original post by Anonymous Poster
Quote:
Original post by Fred304
In other words, you cannot change the reference. As already pointed out, the reference is passed by value.


It's perfectly correct to say that in Java all objects are passed by reference. If you change some attribute of the passed class that you'll be changing it in the same instance passed. This is useful in real world.

Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method? The only people I found complaining about this were C++ developers trying to find a pretext for bashing Java.


No, it's not. Objects are passed by value. The only reason your changes in the function affect the passed instance of the class is that both pointers, the one in the function and the one outside the function, point to the same instance of the class. The pointer is passed by value, but it's value is the class instance. If you, inside the function, made the passed pointer point to something else, the pointer on the outside of the function would still be pointing to the original instance. If you passed by reference, it would be pointing to whatever you stated in the function.

There's a few examples I can think of that are "real world", but references don't do anything more than pointers could do. And since everything in Java is a pointer, the lack of references is basically insignificant.

[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]

#32 RayNbow   Members   -  Reputation: 191

Like
0Likes
Like

Posted 05 May 2006 - 05:38 AM

Quote:
Original post by Anonymous Poster
Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method?

Someone wanted to do this... But alas, it didn't work because things were passed by value.

#33 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 05 May 2006 - 05:42 AM

It would seem that the "its pass by value only" guys are correcct. I think one of the major problems with this type of argument / discussion is that the powers that be use the wrong terms. A reference in java isnt actually a reference at all, its a copy of a pointer to an object - which is passing by value, not by passing by reference. Until today I too fell for - It says pass by reference so it must be it passes a reference to the actual object. Not so!

Thanks for correcting me. You learn something new everyday.


Ste

#34 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 May 2006 - 06:15 AM

Quote:
Original post by _goat
No, it's not. Objects are passed by value. The only reason your changes in the function affect the passed instance of the class is that both pointers, the one in the function and the one outside the function, point to the same instance of the class. The pointer is passed by value, but it's value is the class instance. If you, inside the function, made the passed pointer point to something else, the pointer on the outside of the function would still be pointing to the original instance. If you passed by reference, it would be pointing to whatever you stated in the function.

There's a few examples I can think of that are "real world", but references don't do anything more than pointers could do. And since everything in Java is a pointer, the lack of references is basically insignificant.


Ok, did you actually read what I have written?

Who cares if you call it by value and I by reference if ALL I NEED is to work with the class inside of the function???????????????????????

Are we done with that? Do you agree that's possible and used in 100% of the projects you will ever work with in Java?

Tell me just one single case where "changing the instance" inside of a function was terribly needed. Just one...


#35 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 05 May 2006 - 06:19 AM

Quote:
Original post by RayNbow
Someone wanted to do this... But alas, it didn't work because things were passed by value.


I am sure that was part of a vital functionality in a 1 million dollars project. :)

The guy obviously was a beginner. Beginners commit mistakes but it doesn't mean this a sorely needed "feature" we will die if we don't have it. Seriously, in all my life as a programmer I have seen only C++ developers complaining about it. Can't you adapt to Java's paradigm?



#36 Kevinator   Members   -  Reputation: 229

Like
0Likes
Like

Posted 05 May 2006 - 06:20 AM

Quote:
Original post by Fred304
There is no pass-by-reference in Java, Java only knows pass-by-value.

This means that changes to the contents of the formal parameters do not affect the actual parameters. An example:

public class Test
{
public static void main(String[] args)
{
int a = 10;
LinkedList l = new LinkedList();
javaPassesByValue(a, l);
assert a == 10;
assert l.isEmpty();
}

public static void javaPassesByValue(int x, Collection c)
{
x = 5;
c = new ArrayList<String>(100);
c.add("test");
}
}

In other words, you cannot change the reference. As already pointed out, the reference is passed by value.




I'm confused why l is empty.

When javaPassesByValue is called, what happens in memory? There is an instance of LinkedList l in memory while in the scope of main. That's a given. But when the method is called and l is passed to it, is the instance l cloned so there are two distinct (but identical) instances in memory, their difference being scope? So the stuff you're doing in the method only changes the copy and not the original?

OR...

Is something else happening in memory? If so, I can't imagine what. What I described is pure pass by value... (I think).

#37 RayNbow   Members   -  Reputation: 191

Like
0Likes
Like

Posted 05 May 2006 - 06:43 AM

Quote:
Original post by Kevinator
I'm confused why l is empty.

Have you read the article mentioned in this reply?
Quote:
Original post by RayNbow
Everything you want to know about arguments in Java can be read in this article.



#38 Fred304   Members   -  Reputation: 382

Like
0Likes
Like

Posted 05 May 2006 - 06:44 AM

Quote:
Original post by Kevinator
I'm confused why l is empty.

Let's start at the beginning ;)

What does this mean?

int a = 10;

This is a declaration of a variable named a of type int.
What is the value inside a? Easy one, the number 10.

Now let's move on to the next declaration:

LinkedList l = new LinkedList();

This is a declaration of a variable named l of type LinkedList.
What is the value inside l? Maybe a LinkedList Object?










Wrong! Non-primitve types never hold objects themselves, but rather references to objects. So when you examine the value of l, you will find a reference which points to an empty LinkedList.

Now what happens if you call a method with a and l as actual parameters? a and l are evaluated (their value is obtained), and this value gets passed to the method. in case of a, this value is 10. At this point, this 10 has NOTHING to do with a anymore! And in case of l, this value is a reference to an empty LinkedList which does not know anything about a variable called l.

Now when the method changes its formal parameters, these changes cannot be reflected to the calling method.

BTW imagine what would happen if this actually was a call by reference. After the method call, l (which is declared as LinkedList) would hold a reference to an ArrayList... all hell would break loose ;)


#39 Bregma   Crossbones+   -  Reputation: 6359

Like
0Likes
Like

Posted 05 May 2006 - 06:53 AM

Quote:
Original post by Kevinator
I'm confused why l is empty.

When javaPassesByValue is called, what happens in memory? There is an instance of LinkedList l in memory while in the scope of main. That's a given. But when the method is called and l is passed to it, is the instance l cloned so there are two distinct (but identical) instances in memory, their difference being scope? So the stuff you're doing in the method only changes the copy and not the original?

OR...

Is something else happening in memory? If so, I can't imagine what. What I described is pure pass by value... (I think).


You've got it. Java, like C, has only pass-by-value. Since all references to first-class objects are actually pointers to instances, when you pass an object you're really passing a pointer to a class instance by value. It's not pass by reference (as in C++) or pass by name (as in ALGOL) or pass-by-descriptor (FORTRAN). It's pass-by-value.

Each of these argument passing mechanisms have a different name because they're different. When it comes to programming, semantics DOES matter.
Stephen M. Webb
Professional Free Software Developer

#40 Kevinator   Members   -  Reputation: 229

Like
0Likes
Like

Posted 05 May 2006 - 06:57 AM

I still don't understand. In the example above, l and c both point to the same object, correct? I don't understand how it's possible to change the object through the reference called c and then NOT having this be reflected when I use the object through the reference called l. [depressed]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS