Re: some java concepts query



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.
.



Relevant Pages

  • Re: How long to learn C++ if fluent in Java?
    ... I know this "Java has no pointers" mantra. ... The point is, however, that Java reference behave much more like C++ ... unlike with democracy, for some purposes there is indeed a better tool to ...
    (comp.lang.cpp)
  • Re: Opinions on the Law Of Demeter
    ... all that line of code is doing is declaring a reference of type A ... There are only references in Java, no pointers in the C/C++ sense. ... of allowing others to instantiate an object of type A and set it via the ...
    (comp.object)
  • Re: Style of coding
    ... (That was Ada, not Java.) ... name »name« within this method declaration. ... reference is passed to it, whether or not the object reference ...
    (comp.lang.java.programmer)
  • Re: Trouble with Java concepts
    ... >>references when you see a variable which names an object in Java think of it as ... I would try to explain to an incoming C/C++ programmer that passing objects ... is functionally identical to passing pointers in C. ... use of pointers is implicit (you cannot have an Object, only a reference) ...
    (comp.lang.java.programmer)
  • Re: why java does not support pointers
    ... "An object is a class instance or an array. ... The reference values are pointers to these ... Java supports pointers. ...
    (comp.lang.java.programmer)