Re: Basic beginner questions
Sathyaish wrote:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.
I'll attempt gentle answers to some of your questions.
4. One book from the old edition I am using [COBLEY, ANDREW COMPLETE
GUIDE TO JAVA [Paperback] . - COMPUTER STEP, 1997 . - 1874029482] says
there is this operator in Java such that:
i++5 results in i = i + 5;
I tried it and i++5 didn't compile. Was that an operator in one of the
older versions of Java? Or, was it a typo and the author meant to say i
+= 5 instead of i++5?
Looks like a typo. Java has a ++ operator (in fact, it
has two: ++x and x++), but the description you give fits +=
a lot better.
5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?
No and no, for suitable values of "no." Java does not have
a token-manipulating preprocessor of the sort found in C and
some other languages. On the other hand, there are frameworks
like JSP that generate Java source code from a mixture of Java
fragments and bits of markup, and in some sense these can be
thought of as "preprocessors."
As for import: No, it's not like #include. The import
statements just tell the compiler the names of the packages
that contain some of the classes and interfaces you'd like to
mention in the source code, so you can use abbreviated names for
them instead of writing them out in full. It's a convenience,
that's all: You can write java.util.ArrayList all through your
code, or you can import java.util.ArrayList and then just write
ArrayList elsewhere. The compiler generates the exact same byte
code either way.
6. If I do:
import java.awt.*;
Will it get everything inside the awt package and bind it statically
into my code? Or, will it dynamically check for stuff I am using in my
code and then only get those objects I use out of awt?
Neither. The import tells javac what to do with any class
and interface names that aren't fully qualified in your source:
it looks for them in the java.awt package (and in any other
places you've imported). If your code mentions Color.RED, javac
will figure out that you meant java.awt.Color.RED and will put
a reference to it in the compiled byte code. At run time, the
java.awt.Color class will not be loaded unless and until some
piece of code actually calls for it, and that's when "binding"
takes place.
7. If I did:
import java.awt.Graphics;
import java.awt.*;
would it produce duplicate definitions for the Graphics class?
No.
Basically, the above questions 5 to 7 are just one question -- is that
import thingy a pre-processor?
No.
8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.
It depends on what you mean by "constant." Java certainly
has built-in constants of various kinds: 42, 42L, 42.0F, 4.2E1,
'\u0042', null, for example. It also supports classes whose
instances cannot be changed after construction; the String class
gets special support from the compiler in that you can get a
String object to be created by writing "42" in the source.
Java also has final, a keyword that can be attached to a
variable or class member to indicate that the value will not
be changed after construction. Note, however, that the final-
ness applies to the variable itself: if the variable is a
reference to an object of some kind, final says that the variable
will never point to any other object but does not say that the
"value" of the referenced object cannot change:
// arr always refers to the same ten-element array:
final int[] arr = new int[10];
...
// ... but the contents of the array can change:
arr[0] = 42;
arr[3] += 42;
--
Eric Sosman
esosman@acm-dot-org.invalid