Re: some java concepts query
- From: blmblm@xxxxxxxxxxxxx (blmblm@xxxxxxxxxxxxx)
- Date: 24 Sep 2005 15:47:09 GMT
In article <1127543410.815322.316810@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<rahul8143@xxxxxxxxx> 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.
.
- Follow-Ups:
- Re: some java concepts query
- From: Kenneth P. Turvey
- Re: some java concepts query
- References:
- some java concepts query
- From: rahul8143
- some java concepts query
- Prev by Date: Re: some java concepts query
- Next by Date: Re: Is it possible for IE6 under W2K to run JAVA1
- Previous by thread: Re: some java concepts query
- Next by thread: Re: some java concepts query
- Index(es):
Relevant Pages
|