Basic beginner questions

S

Sathyaish

Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.

1. Like there is a Reflector (developed by Lutz) for .NET that
disassembles Microsoft Intermediate Language and displays a tree view
heirarchy of object inheritence within the .NET framework, is there a
tool that does the same for Java byte code?

2. Like an Object Browser exists in Microsoft Visual Studio .NET
2002/2003/2005 and Microsoft Visual Studio Enterprise Edition 6.0, that
lets you browse objects contained in all the libraries your project
references, with the inheritence heirarchies and the
<documentation_comments/>, is there such a tool that exists for Java?

3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

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?

5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?

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?

7. If I did:

import java.awt.Graphics;
import java.awt.*;

would it produce duplicate definitions for the Graphics class?

Basically, the above questions 5 to 7 are just one question -- is that
import thingy a pre-processor?


8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.



Thanks.
 
E

Eric Sosman

Sathyaish said:
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;
 
D

dalouis

Sathyaish said:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.

1. Like there is a Reflector (developed by Lutz) for .NET that
disassembles Microsoft Intermediate Language and displays a tree view
heirarchy of object inheritence within the .NET framework, is there a
tool that does the same for Java byte code?

2. Like an Object Browser exists in Microsoft Visual Studio .NET
2002/2003/2005 and Microsoft Visual Studio Enterprise Edition 6.0, that
lets you browse objects contained in all the libraries your project
references, with the inheritence heirarchies and the
<documentation_comments/>, is there such a tool that exists for Java?

I have heard good things about eclipse, it may or may not do the things
you said. I have used oracle jdeveloper in the past which did some of
what you said.
3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

http://java.sun.com/j2se/1.5.0/docs/api/ this is your my main
resource to find out such things in java.

For String its getBytes()

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?

I have only every used or seen i++ which is the same as i=i+1, i have
never seen i++5 it may or may not work.
5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?

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?

You could import every class ever written by anybody in the whole
entire world
import everything1.*
import everything2.*
.....etc
And it will only make a difference in the compile time while it looks
for those packages. Ie it will not impact your code performance but
for the sake of speeding up your compiler it is better to import what
you need. If you need a lot of things from a package it is fine to
import the awt.* for example.

There is no dynamic check at runtime for those packages, anything that
was not used will not be compiled into the bytecode.
7. If I did:

import java.awt.Graphics;
import java.awt.*;

would it produce duplicate definitions for the Graphics class?

Basically, the above questions 5 to 7 are just one question -- is that
import thingy a pre-processor?

same bytecode produced as before.

8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.

the keyword 'final' in front of a primitive will make it set at
compiletime and not changeable by any code operations.

final int MYABSOLUTENUMBER = 10;
 
D

dalouis

Sorry and to clarify, there is no preprocessor.

The import statements are not like c #include statements. The code in
C would be directly pasted (effectively) into where you put the
include, and import is different. It just tells the java compiler
where to find the definitions of classes that might be used in your
code.
 
J

Juha Laiho

Sathyaish said:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.

.... answers to some of your questions.
3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

The String class defines getBytes() method, which returns a byte[] , so
you can write like this:

String foo="Foo";
byte[] bytes foo.getBytes();

.... or (as "Foo" already is an instance of a String class) like this:

byte[] bytes="Foo".getBytes();

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?

Definitely should be "i += 5".
5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?

No, there isn't a pre-processor. Import is just to let you avoid writing
fully qualified class names in your code - so, instead of

java.util.ArrayList list = new java.util.ArrayList()

you can write

import java.util.ArrayList;
...
ArrayList list = new ArrayList();

.... or, if you use a number of classes from a given package, instead of

java.util.List list = new java.util.LinkedList();

you can write

import java.util.*;
...
List list = new LinkedList();


.... so, it's a tool for source code readability. As far as I know, the
bytecode generated by the compiler is equal for both forms of the source.
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?

Hmm.. it wil let you reference all classes and interfaces in the java.awt
package by their plain class names, instead of spelling out the full
package each time. Note however, that this does not import any names from
the subclasses of java.awt (so, f.ex. you don't get a shorthand for anything
in package java.awt.geom). And no, there's no syntax for "full-tree import",
and multiple consecutive asterisks (like "java.awt.*.*" is not allowed).
7. If I did:

import java.awt.Graphics;
import java.awt.*;

would it produce duplicate definitions for the Graphics class?

No. Some IDEs might flag the java.awt.Graphics as superfluous.
8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.

There are no constants (except for literals). There are "final" variables,
though -- but they're not without their surprises. You have to be
careful in what is declared final. For simple types final is final -- but
for a class with methods that allow changing the state of an object, final
may guarantee that no-one changes the object reference (so, the variable will
always point to the same object instance), but not that the object state
would remain the same (so, whatever data is contained within the object is
not constant - unless forced so by the class itself).
 
M

Matt Humphrey

Sathyaish said:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.
3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

A couple of people have answered your question but I'll add just a bit more.
Java strings are Unicode, not ASCII and representing them as bytes presumes
you know which character encoding is being used. string.getBytes() uses the
default, which isn't necessarily what you want. string.getBytes(String
charSetName) will give you bytes under the named encoding, such as
"US-ASCII", "UTF-16", "UTF-8" etc. You can check
http://mindprod.com/jgloss/encoding.html for details and encodings.
 
A

Adam Maass

Sathyaish said:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.

1. Like there is a Reflector (developed by Lutz) for .NET that
disassembles Microsoft Intermediate Language and displays a tree view
heirarchy of object inheritence within the .NET framework, is there a
tool that does the same for Java byte code?

Most modern Java IDEs will accept .jars as well as source to generate such
things.
2. Like an Object Browser exists in Microsoft Visual Studio .NET
2002/2003/2005 and Microsoft Visual Studio Enterprise Edition 6.0, that
lets you browse objects contained in all the libraries your project
references, with the inheritence heirarchies and the
<documentation_comments/>, is there such a tool that exists for Java?

Again, most modern java IDEs do this.
3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

String s = ...
byte[] bytes = s.getBytes("encoding");

Note that there is no single byte representation of a String. What sequence
of bytes you get depends on what encoding is in use. (Yes, there is a
version of getBytes() that takes no arguments. This simply defaults to a
default encoding.) Not all strings are representable in all encodings. This
is why getBytes() may throw an exception.

Note that the library javadocs are freely available on the public internet,
from Sun.
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?

That's an old book. I presume that the author meant +=.
5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?

No, and no. 'import' is merely a way of telling the compiler that the code
following may use unqualified type names -- IE, without the package names.

import a.b.*

merely says that should the compiler find an unqualified type name in the
code, it should look in the package a.b for a type by that name. As a
programmer, you can think about 'import' statements as creating a shorthand
for the types from other packages you use.
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?

Java does not do static binding. Types are dynamically bound at runtime.

See the discussion above about the 'import' directive.
7. If I did:

import java.awt.Graphics;
import java.awt.*;

would it produce duplicate definitions for the Graphics class?

Basically, the above questions 5 to 7 are just one question -- is that
import thingy a pre-processor?

No. This is generally bad form, but not a compiler error.
8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.

The closest equivalents are 'static final' variables.


-- Adam
 
S

SBC News

Hello Gurus,

These are great information. I'm a newbie in Java world as well and I'm
learning a lot reading your posts in forum like this.

Now, to extend the question that Sathyaish asked, can you help to open the
world for me on mine.
Since you've mentioned that the 'import package as import javax.swing.*;'
does not actually import the package (and its sub-tree) into the program
like C, what about the 'extends' and 'implements'.

One example is if I have

import javax.swing.*;

public class test extends JPanel implements ActionListener
{
something here
}

What the extends means and what it does?
What the implements means and what it does?

I refer to the Sun Java API and it shows the relationships as follow
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel

I'm not quite sure when should I use extends and when should I use
implements
or should I always use implements ActionListener whenever I use extends?

Thanks in advanced!
PETER NGUYEN


Adam Maass said:
Sathyaish said:
Be gentle. I am new to Java. I did some VJ++ in 1999 for a few months
but that was about it.

1. Like there is a Reflector (developed by Lutz) for .NET that
disassembles Microsoft Intermediate Language and displays a tree view
heirarchy of object inheritence within the .NET framework, is there a
tool that does the same for Java byte code?

Most modern Java IDEs will accept .jars as well as source to generate such
things.
2. Like an Object Browser exists in Microsoft Visual Studio .NET
2002/2003/2005 and Microsoft Visual Studio Enterprise Edition 6.0, that
lets you browse objects contained in all the libraries your project
references, with the inheritence heirarchies and the
<documentation_comments/>, is there such a tool that exists for Java?

Again, most modern java IDEs do this.
3. How do I get the byte representation of an ASCII string in Java? If
I had a tool like the ones described in my question number 1 and 2
above, it would have take me a few seconds to figure that out. In the
absence of such a tool, I can only grope on the Internet and Google
would be my best bet.

String s = ...
byte[] bytes = s.getBytes("encoding");

Note that there is no single byte representation of a String. What
sequence
of bytes you get depends on what encoding is in use. (Yes, there is a
version of getBytes() that takes no arguments. This simply defaults to a
default encoding.) Not all strings are representable in all encodings.
This
is why getBytes() may throw an exception.

Note that the library javadocs are freely available on the public
internet,
from Sun.
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?

That's an old book. I presume that the author meant +=.
5. Does java have a pre-processor? Is that 'import' thingy a
pre-processor directive?

No, and no. 'import' is merely a way of telling the compiler that the code
following may use unqualified type names -- IE, without the package names.

import a.b.*

merely says that should the compiler find an unqualified type name in the
code, it should look in the package a.b for a type by that name. As a
programmer, you can think about 'import' statements as creating a
shorthand
for the types from other packages you use.
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?

Java does not do static binding. Types are dynamically bound at runtime.

See the discussion above about the 'import' directive.
7. If I did:

import java.awt.Graphics;
import java.awt.*;

would it produce duplicate definitions for the Graphics class?

Basically, the above questions 5 to 7 are just one question -- is that
import thingy a pre-processor?

No. This is generally bad form, but not a compiler error.
8. How do you declare a constant in Java? Does Java have constants? I
am guessing it should.

The closest equivalents are 'static final' variables.


-- Adam
 
L

Lew

SBC said:
import javax.swing.*;

Makes available the javax.swing namespace as a prefix for all class names in
that particular package.
public class test extends JPanel implements ActionListener
{
something here
}

What the extends means and what it does?

"extends" indicates that the class inherits from a superclass, in this case
named "JPanel". "Inherits" means that your class "test" (should be named
"Test", not "test") /is-a/ JPanel. That means every object of type "test"
("Test") has nearly all the behaviors and attributes of a JPanel. It also
means that JPanel is a class.
What the implements means and what it does?

"implements" means that the class implements every method declared in the
corresponding interface, in your example "ActionListener".
I refer to the Sun Java API and it shows the relationships as follow
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel

I'm not quite sure when should I use extends and when should I use
implements

Use "extends" when you design a class that is a subtype of the class that is
extends. Use "implements" when you design a class that implements all the
methods of one or more interfaces. A class may extend no more than one class.
(No fewer than one, either, but you don't need to say "extends" when the
supertype is Object.) A class may implement zero, one or more interfaces.

Use "extends" and "implements" to make types that can be treated as if they
were their supertypes. A classic example is the Collections framework, where
ArrayList implements List

So you can declare a variable

List<Foo> stuff = new ArrayList<Foo>();

Because ArrayList implements List, 'stuff' can be treated as if it were of
type List, even though its underlying type is actually a subtype of List.

Google "inversion of control", "design by contract".
or should I always use implements ActionListener whenever I use extends?

No. Use "extends Whatever" when the class is a subtype of a class 'Whatever'.
Use "implements Whatever" when the class is a subtype of an interface
'Whatever'. Otherwise don't use either one.

These are fundamental concepts to Java. Have you read anything on these
topics? What?

Read the Sun Java (JSE) tutorial. Read Bruce Eckel's book /Thinking in Java/,
available in its earlier editions for free on line.

Read good books on object-oriented analysis, design and coding.

- Lew
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top