Strings...immutable?

J

jt

Joshua said:
JT said:
Lew said:
Wojtek wrote:
Chris Uppal wrote :
Wojtek wrote:

No it is a call by reference. If it was a call by value, then a clone
of the StringBuffer (in the following code) would be placed in the
stack. The method would then use the clone and the calling main()
would
still use its private copy.

So, taking the two points together, most Java programmers prefer to
stick very tightly to the low-level view of parameter passing and
state that Java /only/ has call by value, but that some of those
values are references to objects. Anything else is known to lead to
massive confusion.

When I read SCJP study materials, they have always asserted
unequivocally "call-by-value" as the only correct answer to "how does
Java pass method arguments?"

-- Lew
Practical Java Programming Guide Peter Haggar
Praxis 1: Understand that parameters are passed by value, not by
reference

import java.awt.Point;
public class PassByValue {
public static void modifyPoint(Point pt, int j){
pt.setLocation(5,5);
j=15;
System.out.println("During modifyPoint " + "pt = " + pt t " and j
= " + j);
}
public static void main(String [] args) {
Point p = new Point(0,0);
int i = 10;
System.out.println("Before modifyPoint " + "p = " + p + " and i =
" + i);
modifyPoint(p, i);
System.out.println("After modifyPoint " + "p = " + p + " and i = "
+ i);
}
}

Here's what I get as output (after changing 'pt t' to 'pt +'):
Before modifyPoint p = java.awt.Point[x=0,y=0] and i = 10
During modifyPoint pt = java.awt.Point[x=5,y=5] and j = 15
After modifyPoint p = java.awt.Point[x=5,y=5] and i = 10

Primitive types are passed by value, *but* Objects are passed by
reference. Or, if you want to think of it like this, an Object is really
a pointer, so when you pass Objects, you are passing the address of the
Object.
hmmm... I thought I copied and pasted that code in line for line. I
must have messed up when I was trying to format the code to be included
inline. my bad....
 
J

jt

Mark said:
*chuckles* Sorry, I didn't see your example above. What I meant is that
someone new reading "StringBuffer" might think a StringBuffer is a type
of String, and therefore shares behavior. I know myself I didn't truly
grok the difference the first time through. It was later that I realize
that StringBuffer and String were very different types of objects, and
the names just coincidence really.


All docs, and all tutorials also. Java is a big language. Even
O'Reilly's _Learning Java_, as huge as it is, just scratches the surface.
I have about 10 books now on Java Programming, including _Learning Java_
but the one I like the best (over the Sun Java Tutorial now that I'm
reading it and Roedy Green's site) is Head First Java. However, no
amount of reading, to me, is a substitute for using practical examples
as study aids. I spend hours looking at the posts in this NG and when I
see a topic that interests me I watch it and try the code that gets
posted in it.
 
C

Chris Uppal

Mike said:
Are there languages where this isn't true? I don't know of any C or C++
implementations that don't use machine addresses as pointers, but I
presume an implementation that used something else would be strictly
conforming.

I have been told that C for early Macs used some sort of indirection
arrangement for its pointers instead of raw addresses. Certainly, and by
design, there is nothing built in to the C std that makes raw addresses
mandatory (I don't think "raw addresses" even exist in all target
architectures).

But returning to an earlier observation of Patricia's. C's pointers differ
from Java's references in more than one fundamental way -- not only do they
have pointer arithmetic, but the universe of "things" to which they can point
is different. In C (as, iirc, in Pascal) a C pointer can point to any
variable (caveats about the obsolete "register" qualifier aside). Hence, in C,
pass-by-reference can be implemented at the application-code level (or emulated
if you prefer to call it that):

void
swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}

called as:

void
some_function()
{
int x = 0;
int y = 100;
swap(&x, &y);
}

will exchange the values held in x and y (as, of course, people in this thread
will be perfectly well aware). But Java's references don't even come close to
providing that kind of ability -- it's a different concept of what a "pointer"
/is/. A Java reference is a reference to a manipulable /value/ (always
heap-allocated as it happens), whereas a C (or Pascal for that matter) pointer
is a reference to a variable /or/ value.

-- chris


P.S. Terminology: in the above I'm considering "objects" to be special kinds
of values, rather than -- as is more often the case -- something distinct from
values.
 
S

Sherm Pendley

Chris Uppal said:
I have been told that C for early Macs used some sort of indirection
arrangement for its pointers instead of raw addresses.

Early PCs certainly did. The reason is simple: 16-bit registers and 24-bit
address space. Apps that needed to use more than 64K of memory had to use
"far" pointers, which had both a segment ID and an offset within the segment
to address it.

See: <http://en.wikipedia.org/wiki/C_memory_model>

Modern processors still use segments, but register size and address width
are now equal, so segments are no longer needed to make more memory available
than a register-sized pointer can address.

sherm--
 
M

Mike Schilling

Chris said:
I have been told that C for early Macs used some sort of indirection
arrangement for its pointers instead of raw addresses. Certainly,
and by design, there is nothing built in to the C std that makes raw
addresses mandatory (I don't think "raw addresses" even exist in all
target architectures).

Which, I suspect, involved a base + offset implementation for pointers, with
the base being the indirected part. (Think of the number of indirect
pointers that would otherwise be required for all the members of an array.)
But returning to an earlier observation of Patricia's. C's pointers
differ from Java's references in more than one fundamental way -- not
only do they have pointer arithmetic, but the universe of "things" to
which they can point is different. In C (as, iirc, in Pascal) a C
pointer can point to any variable (caveats about the obsolete
"register" qualifier aside). Hence, in C, pass-by-reference can be
implemented at the application-code level (or emulated if you prefer
to call it that):

void
swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}

called as:

void
some_function()
{
int x = 0;
int y = 100;
swap(&x, &y);
}

In C, yes. Pascal, like Java, doesn't have an "address-of" operator. In
both, pointers always point to dynamically allocated objects. I think it's
a true statement that the only difference between Pascal and Java pointers
is that Java allows some data types to be pointed to (and, in fact, never
accessed except via pointers), while Pascal allows all data types either to
be pointed to or to be accessed directly.
 
M

Mike Schilling

Sherm said:
Early PCs certainly did. The reason is simple: 16-bit registers and
24-bit address space. Apps that needed to use more than 64K of memory
had to use "far" pointers, which had both a segment ID and an offset
within the segment to address it.

Though since segment and offset were translated to an absolute address in
hardware, I think it's within the meaning of the act to call the combination
of the two a "machine address".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top