Java Questions???

Status
Not open for further replies.
Since the thread got closed in the Lounge (guess there's a first time for everything).. here goes:

Okay, I'm taking a Data Structure class and have never programmed in Java before even though the class is based on it. I'm learning about all this as we speak but I thought the Ars crowd would be better at explaining certain points that some books just don't drive home. Is there anyone out there who can answer a few of these questions (I knew some of the answers last semester but need a few pointers to refresh my memory):
1) What are the main points of OOP? For example, how would the "Hello World" programs differ using OOP rather than functional programing (via LISP or something)?

2) What are constructors? To my belief, they create a new instance of something right? But how are they used in the program? How does constructors mix in with classes? What does it mean that a new instace of a class has been created?

3) What is this interface type? What purpose does it serve?

4) Are packages simply collections of classes? And in turn, are classes just collections of methods and definitions? And for example, if there is a private method bar in the class foo, does that mean foo.bar cannot access it since it is private?

5) What is an instance of a variable? I understand that variables are declared and created but what is an instance of it?

6) What is the signficance of foo and bar in programming?

7) What books do you guys recommend on data structures using java?

Thanks in advance to anyone who can help.

Quadmire
 

ssmith

Ars Scholae Palatinae
928
Hi,
I'll try and give brief answers which hopefully will help.
1) Tricky to answer, you will find out for yourself! Hopefully I will help to provide you with a basic understanding.

2) Learn the difference between Classes and Objects (this again will help with question 1). A class is a type of thing, an object is an instance of that thing. Each class must have a constructor, so when you create an instance of it, you call the constructor. It allows initialization of the object. In addition, you can define multiple constructors for the same class, and this allows you to pass different parameters into the class.
Consider a StringBuffer - it can be created in many ways. Here are two examples:
code:<HR>
Code:
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer("test");
The object variables sb1 and sb2 now contain instances of the class StringBuffer. StringBuffer is the class, and we have called two different constructors, which are defined in the same class. sb1 contains an empty StringBuffer, sb2 is a new StringBuffer, but the constructor has initialized it's value to the String, 'test'.

3) An interface is a description of a class. It defines a list of the methods and fields that an implementing class must have. This is extremely useful: The description of a class is seperated from it's implementation. When writing code, you can code to the interface. This means that the implementing object can change, but the code which is calling it need not.

4) Packages provide a hierarchical naming scheme. Each level of a package may contain classes, but it is not necessary. The import statement at the top of a class imports the namespace. If I don't import the namespace, I have to use the Fully Qualified name to reference a Class - i.e.
code:<HR>
Code:
java.util.Hashtable ht = new java.util.Hashtable();
Compared to:
code:<HR>
Code:
import java.util.*;<P>// blather blather blather<P>Hashtable ht = new Hashtable();

5) Do you mean instance variables? An instance variable is a variable contained within an object.

6) They are nonsense words to make code fun to read (as opposed to say ABC=XYZ). My understanding of it is from the word FUBAR, which stands for Fucked Up Beyond All Repair. Whether that is the origin of foobar though, I don't know...

7) Dunno really. Sorry.

Couple of good resouces (if you don't already know).
I highly recommend Sun's Java tutorials, they are excellent.
http://java.sun.com
JavaWorld, especially the Hints and Tips.
http://www.javaworld.com
IBM, who do great things.
http://www.ibm.com/developer
http://www.alphaworks.ibm.com/
Get some open source stuff and see how it works..
http://www.mortbay.com
Hope that was a bit useful.

 

lunarn

Seniorius Lurkius
26
Data Structures and Algorithm Analysis in Javaby Mark Weiss. I have only read the C++ version, but I liked it. (Used it at college last year). I found it focused on theory and not code, which I liked.

I believe that the obligatory read in this field is Knuth's algorithm book. I believe you can find it at Adison Wesley Longman. (Search for Donald E. Knuth)

As a more general Java book, I would recomend Bruce Eckel's "Thinking in Java" (two editions). You can download them here.

Hope it helps
- Arna
 
Status
Not open for further replies.