Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

some java concepts query

0 views
Skip to first unread message

rahu...@gmail.com

unread,
Sep 24, 2005, 3:02:41 AM9/24/05
to
hello,
I am learning Java. I come across some understaning queries
1)are there really pointers in Java because i read some texts that have
words "references(pointers)" and also when we compare strings == checks
for same memory location of both operands to ==. does that mean its
checking characters in strings by memory location?
2)what is mean by "Except for visibility issues there is no difference
between C function and Java static method"
3)All arguments to methods are passed by value not by references then
how can i return changed value in method that is previously declared in
main mathod?
4)Can i have option to declare integer array as
int num[50]; or i have restriction by JAVA to use
int[] num=new int[50]; only.

Roedy Green

unread,
Sep 24, 2005, 5:18:57 AM9/24/05
to
On 24 Sep 2005 00:02:41 -0700, rahu...@gmail.com wrote or quoted :

>1)are there really pointers in Java because i read some texts that have
>words "references(pointers)"

A reference is a polite pointer. You can't do arithmetic on them.
They always point to the start of objects not into the middle of an
array for example or to a field in an array.. This mean they avoid the
dangers associate with C style pointers which are also effectively
machine addresses.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Hemal Pandya

unread,
Sep 24, 2005, 11:01:17 AM9/24/05
to

rahu...@gmail.com wrote:
> hello,
> I am learning Java. I come across some understaning queries
> 1)are there really pointers in Java

I too have wonder about the same. I keep getting these
NullPointerException. Now, I know what is Null and I know what is
Exception, but haven't quite figured out what is a Pointer.

:-)

blm...@myrealbox.com

unread,
Sep 24, 2005, 11:47:09 AM9/24/05
to
In article <1127543410.8...@g47g2000cwa.googlegroups.com>,

<rahu...@gmail.com> wrote:
>hello,
> I am learning Java. I come across some understaning queries
>1)are there really pointers in Java because i read some texts that have
>words "references(pointers)"

I guess it depends on what you mean by "pointers" and "really" ....

Java terminology for the kind of variable that's not a primitive is
"reference". The basic idea is very similar to pointers in other
languages, so "references(pointers)" is meant to suggest to people who
know about pointers that references are similar. Not identical, since
you can't do arithmetic with them, and they can only point to objects
(or be null), rather than pointing to arbitrary memory locations
(as for example in C). So Roedy's phrase "polite pointers" is good.
I often call them "cleaned-up pointers" (meaning pointers without
some of the features than can make a mess).

>and also when we compare strings == checks
>for same memory location of both operands to ==. does that mean its
>checking characters in strings by memory location?

Applied to references, "==" checks whether they point to the same
object -- same idea as "same memory location", but I have an idea
that the truly pedantic may not like the latter wording for Java.
There's no check as far as I know of anything within the objects, just
whether the references point to the same one. Classes that want to
provide a way for comparing contents of objects (e.g., for String it
makes sense to compare the two Strings' characters) override "equals"
to do that.

>2)what is mean by "Except for visibility issues there is no difference
>between C function and Java static method"

Java static methods are like C functions, except for .... I'm not sure
I can spell out all details of what's meant by "except for visibility
issues", but visibility refers to what variables and methods are
"visible" where. For example, within a Java class, all the static
variables are visible to all the methods of the class. In C,
variables declared not-within-any-function are visible to everything
within the same source file, past the declaration of the variable.

>3)All arguments to methods are passed by value not by references then
>how can i return changed value in method that is previously declared in
>main mathod?

Well, if by "return changed value" you mean "change the values of the
arguments" (rather than returning a value with "return"), the short
answer is that you can't: Arguments are passed by value, which means
there's an implicit copying, and no changes to the called method's
copy are passed back to the calling method's copy.

For example,

void clearFIrst(int d) { d = 0; }

does nothing very useful.

However, if the argument in question is a reference, both copies
(the caller's copy and the called method's copy) *point to the same
object*, and the called method can make changes to that object.

So the following method, while a bit ugly, *does* do something
vaguely useful:

void clearFirst(int[] x) { x[0] = 0; }

since an "int[]" is an object.

This is a perennial topic of discussion in Java newsgroups; searching
for "pass by value" and/or "pass by reference" will probably give you
at least as much material as you care to read.

>4)Can i have option to declare integer array as
>int num[50]; or i have restriction by JAVA to use
>int[] num=new int[50]; only.

As far as I know, only the latter syntax (with "new int[50]") works.
This makes sense, in that arrays are objects, and the only way (again
AFAIK) to create objects is with "new". (Caveat: The "new" might be
buried in a method call somewhere else, e.g., you might create a new
object by calling a factory or other method that creates the object
(with "new") and returns a reference to it.)

--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.

Roedy Green

unread,
Sep 24, 2005, 12:27:59 PM9/24/05
to
On 24 Sep 2005 08:01:17 -0700, "Hemal Pandya" <hemal...@gmail.com>
wrote or quoted :

>I too have wonder about the same. I keep getting these
>NullPointerException. Now, I know what is Null and I know what is
>Exception, but haven't quite figured out what is a Pointer.

It is a term used in C for an address. In C you can do address
arithmetic so you can modify your references to point to the third
element of an array or a particular field in the middle of a class.
C is closer to the way assembler works, but it dangerous. The
smallest slip can crash the app without much clue where the problem
was.

Kenneth P. Turvey

unread,
Sep 24, 2005, 9:07:59 PM9/24/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, 24 Sep 2005 15:47:09 +0000, blm...@myrealbox.com wrote:

> Applied to references, "==" checks whether they point to the same
> object -- same idea as "same memory location", but I have an idea
> that the truly pedantic may not like the latter wording for Java.

I don't think the pedantic will really mind this. The same object is
probably really only at one address in memory so it makes sense to say
the memory address is the same. I can't think of any time this doesn't
work.

The truly pedantic will have issue with saying references are memory
addresses. They differ in one important way that you didn't mention
earlier in your post. With garbage collection the actual address of an
object can change, but the reference value will always stay the same.
The system can move objects around in memory behind your back, but
promises that the value of a reference to an object will always stay the
same (as long as the object is in scope somewhere).

- --
Kenneth P. Turvey <kt-u...@squeakydolphin.com>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: kptu...@jabber.org
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDNfhk3naBnF2rJNURAinmAJ9PbCEPjlWPVRhjIQxewRlqK1AzJwCgkjK4
xkZtL2pj+WCh+EFR1GvOs5Q=
=qE3O
-----END PGP SIGNATURE-----

Roedy Green

unread,
Sep 24, 2005, 10:06:07 PM9/24/05
to
On Sat, 24 Sep 2005 20:07:59 -0500, "Kenneth P. Turvey"
<kt-u...@squeakydolphin.com> wrote or quoted :

>The truly pedantic will have issue with saying references are memory
>addresses. They differ in one important way that you didn't mention
>earlier in your post.

In the early JVMs and presumably in some of the non-Sun JVMs,
references are handles, not addresses. They are slot numbers in an
object pointer array. You index by slot number to get the address of
the corresponding object. This would allow objects to be moved
without changing the references in any way.

Anyone write Pascal code on the early Macs or Lisa? You used handles,
and temporarily freezing handles so you could temporarily use pointers
to access blocks of RAM.

blm...@myrealbox.com

unread,
Sep 25, 2005, 2:40:17 PM9/25/05
to
In article <pan.2005.09.25....@squeakydolphin.com>,

Kenneth P. Turvey <kt-u...@squeakydolphin.com> wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>On Sat, 24 Sep 2005 15:47:09 +0000, blm...@myrealbox.com wrote:
>
>> Applied to references, "==" checks whether they point to the same
>> object -- same idea as "same memory location", but I have an idea
>> that the truly pedantic may not like the latter wording for Java.
>
>I don't think the pedantic will really mind this. The same object is
>probably really only at one address in memory so it makes sense to say
>the memory address is the same. I can't think of any time this doesn't
>work.
>
>The truly pedantic will have issue with saying references are memory
>addresses. They differ in one important way that you didn't mention
>earlier in your post. With garbage collection the actual address of an
>object can change, but the reference value will always stay the same.
>The system can move objects around in memory behind your back, but
>promises that the value of a reference to an object will always stay the
>same (as long as the object is in scope somewhere).

I was trying to avoid directly saying "Java references are memory
locations" because I was fairly sure that this was not completely
accurate, for reasons I wasn't remembering. I was hoping that someone
else would explain those reasons, and -- someone did. Thanks!

0 new messages