Prof Bill Dry Technical Note

DTN #4 - Java Starter for C++ Coders

So you know C++, welcome to Java-land


There are three sections here:

  1. Java support... these are links to Java sites to get you up and coding. Now, if you're working on campus, then most of these won't be necessary. If you want to establish a nice and cozy Java environment at home or somewhere else, then these links may be of help.
  2. Other opinions... some links to the opinions of otraining
  3. thers on the transition from C++ to Java
  4. My thoughts... um, my thoughts on the whole topic

The current version of Java that we are using has two names: Java 1.5 and Java 5.0. I think it's an R&D versus marketing war or something. Anyway, it's funny that a beautiful, simple language has an ugly, difficult naming scheme. So be it!

1. Java Support

1.1. Help from Sun Micro

The biggie is the Java API at: java.sun.com/reference/api/ where you will find web pages defining all the classes in the Java API.

If you want to download Java for your home computer, start here: java.sun.com/javase/downloads/index.jsp ... the top-most link (it currently says "Get the JDK Download" is usually what you want. Java is already on all the lab computers.

OK, on to the help part:

And you know better than I, don't forget google. Googling something like "java thread examples", or "java serversocket examples" will often yield a great starting place to surf for more information.

1.2 Integrated Development Environments (IDE's)

You can (usually) improve your productivity by using an Intergrated Development Environment (IDE) when coding in Java. Most of our lab computers have two available: JGrasp and NetBeans. You have to click on "Start" and search.

Here are links to JGrasp, NetBeans and Eclipse (another really popular IDE):

  •  www.jgrasp.org/ - JGrasp is my favorite because it's so tiny:
  •  java.sun.com/javase/downloads/index.jsp - Many people like Sun's NetBeans, it offer more functionality at the expense of complexity and control... it can be downloaded with the Sun Java compiler/libraries at:
  •  www.eclipse.org/ - Eclipse is really big, but really popular in industry:

1.3 Books on Java

Here's an open source Java textbook: en.wikibooks.org/wiki/Java_Programming . I haven't used it or read it all, but a cursory glance yields lots of examples and clear explanations. It looks nice.

"Real" books that I like (and have a copy of, if you'd like to peruse) include:

  • "Head First Java" by Sierra and Bates - really great examples and a nice writing style... they assume you know how to program as well (I think)
  •  "Java Software Solutions" by Lewis and Loftus - this text was used for one year at North Central. I like their emphasis on teaching Swing right out of the box.
  • "Java Program Design 1.5" by Cohoon and Davidson - nice book
  • "Starting out with Java" by Gaddis and Muganda - written by our own Dr. Muganda.

I have two niche Java books as well:

  • "Java 2 V5.0 Tiger - New Features" by Schildt - a great overview of what was added to Java in the V1.5 major, major release. Very nice, especially if you're used to older versions of Java.
  • "A Little Java, A Few Patterns" by Felleisen and Friedman - I'll admit this isn't stuff you'll probably use every day, but it's a fun book on using Java in a slightly different way than you're probably used to. Great nerd reading!

2. Other Opinions

Some nice (hopefully) links to the thoughts of others on this topic:

That's it.

3. My Thoughts

I've made two Java transitions:

  • I knew C++, and then learned Java
  • I taught C++, and then taught Java

There are fewer decisions to make in Java, making it fundamentally easier to use than C++. The price is efficiency and a lack of fine control over your program's execution.

Here's a list of some of the key decisions that Java makes for you:

  1. Everything's a pointer - Java calls object pointers "references".
    • Dog d;   // like Dog *d; in C++
    • Dog d = new Dog();  // creates a new Dog object
  2. Member access is only through "." - since everything in Java is a reference, there is no need to have "->" and "." as in C++.
  3. Garbage collection - Java will decide when the memory from your old objects is reclaimed. There is no need to delete.
  4. No stand-alone functions or globals - all Java functions (methods) are defined in the context of a class. Even main() must appear in a class; you tell Java what class to run and it'll find the main(). That's actually nice. Each class can have it's own unit-testing main() if you like.
  5. All method parameters are pass-by-value - There is no pass-be-reference. There's no const or & decision to make. Also note that since all objects are references, passing an object reference as a parameter means that a method can change the contents of an object, but not the reference itself.
  6. All methods are polymorphic - There is no virtual modifier as in C++.
  7. The standard API/library is much nicer - always check the Java API ( java.sun.com/reference/api/ ) for code you can reuse. Swing, threads, collections... it's all clean as a whistle.
  8. Characters are unicode - so each character will cost you 2 bytes.
  9. No operator overloading - You can only create regular, old methods in Java. Teaching operator overloading sucked. There, I said it.
  10. No arbitrary memory access - Array access is dynamically checked in Java, so overwriting array boundaries will raise an exception right when you do it... not hundreds of statements later, as in C++.
  11. Only single inheritance - Eliminating multiple inheritance removes some nasty language issues and syntax. You can, in Java, inherit multiple interfaces. There's no convoluted constructor syntax to specify the order of constructors and their parameters and all those issues you find in C++.
  12. Java generics are beautiful - C++ templates, um, are not.
  13. No header files - When you import a Java file, you get the whole thing. There isn't the separation of function header and implementation as in C++.
  14. Java has a file convention - Use the Java file convention of one class per file (not counting embedded classes) and naming your Java file <class>.java. Also, folders are used to group Java files into packages, but if you are working on a project so large that you need packages, then you probably don't need to be reading this page.
  15. Multi-threading is supported in the language - The synchronized keyword can be used to guard potentially hazardous multi-threading code. Also, the Thread class and Runnable interface are excellent examples of point 6, super nice and easy to use library code.
  16. Strongly-typed - no generic pointers ala C/C++
  17. Arrays are objects too - As a results, in Java, arrays always know their size and other goodies.

Hope this helps.

thanks... yow, bill

wtkrieger@noctrl.edu ... william.krieger.faculty.noctrl.edu/