Prepping for Java

Java logo and wordmark

So you want to write some Java — now what?

Tools

First you need some tools. Every Java developer is going to have their own preferred tool set, so don’t be upset if my list doesn’t include your favorite. It’s nothing personal. Here’s how I’d set things up.

First, you have to install the Java Developer Kit (JDK). You can download it from Oracle at this link. Install the version appropriate for your platform.

Next you need a development environment. Well, technically, you don’t need one, but you’ll want one. Java source files are just plain text files, so you can edit them in notepad or vi or emacs or whatever you like. But that’s like rubbing two sticks together to start your campfire when there’s a lighter in your pocket. So install an IDE. I like the IntelliJ IDE from JetBrains. There is a free version and a paid version, and you can try the paid version before you buy it. When you install, you can just take the default settings. You don’t need to install any of the optional components, at least not at this point.

The next thing you’re going to want is Maven. Maven is a build tool and package manager, among other things, and you can get it from the Maven site. If you’re installing on Windows, you might want to take a look at this page for information specific to Windows. If you’re on Mac, you can install it from the site, or via Homebrew or Macports. For Linux, use your package manager (ie, apt-get install maven).

With those tools you’ll have a good foundation to start making Java projects. If you’re going to deploy to Windows, you can make it easier for your users with a tool like Launch4J, which will wrap your jar file in a Windows executable, prompting the user to install the required Java version if necessary.

Tutorials

Next, if you haven’t already, you should work through some Java tutorials covering the basics. Just start at the top and work your way down. Don’t be tempted to skip ahead — especially if you’re not already familiar with object-oriented development. If some of it is review for you, so much the better — you’ll get through it quickly. You don’t need to do every tutorial, but you should do all the basics up until you get to the GUI section. You can then skip the Swing section and go straight to JavaFX for GUI work.

Here are some random things that may bite you if you’re coming from VB or similar languages.

  1. Everything is an object
    If you’re coming from a procedural language or background, you’ll want to get your head around Object Orientation. In Java, programs are built from objects. There are no standalone procedures, and there are no global variables. Programs are entirely built from objects talking back and forth to each other.
  2. Everything has a type
    All variables and parameters have a type, and that type has to be declared in the source file. You can’t just say “foo = 5” without first having said what foo is. For example, “int foo;” declares foo to be a variable of type int (integer). It’s the same with parameters. You can’t declare “function bar(arg1, arg2)”, but you can declare “String bar(int arg1, String arg2)”. That says that “bar” is a function which takes two arguments, an integer and a string, and returns a String. Everything is declared at compile time.
  3. Equals is not a comparison!
    In some languages, most notably VB and VB.Net, the equals sign is both an assignment operator and a comparison. You can say “foo = 5” to assign foo the value of 5, or you can say “if foo = 5 then …” which will compare foo to 5. Java does not work that way. The equals sign is always for assignment. If you want to compare, you use two equals signs. Saying “if (foo = 5)” in Java will always evaluate as true, even if foo is never 5, because that statement assigns 5 to foo. You would need to say “if (foo == 5)”.It is not uncommon to see assignments written “backwards” because of this. For example, “if (5 == foo)”. The reason is to prevent accidental errors. If you mean to code “if (foo == 5)” but you accidentally type “if (foo = 5)” then you will have a bug. And depending on where it happens, those kinds of bugs can be pretty hard to spot later. By typing it as “if (5 == foo)” you will cause a compile error if you leave out one equals sign, since 5 isn’t a variable and can’t be assigned to.Also note that for more complex objects (including Strings), you’ll want to use the .equals() operator instead anyway. See your Java tutorials for more detail about that.
  4. Case Matters!
    Java is case sensitive. Two variables named “Bar” and “bar” are entirely separate and have no inherent relationship to each other.
  5. Exceptions are the rule!
    Error handling in Java is done with exceptions. There is no such construct as “on error goto …” (In fact, not only is there no “on error goto”, there is no “goto” of any kind.) Not only that, but if you call a method which is declared as throwing an exception, you must handle that exception. You can turn around and re-throw it if you like, but you can’t just ignore it.
Posted in Fundamentals, Programming | Tagged , | 2 Comments

Thinking about Objects

Last time we looked at cleaning up the global space. We cleaned up the clutter in the global namespace by encapsulating lots of little bits of data into user defined types.

But even after doing that, there are times when there are lots of different routines that need access to that data. You might be tempted to go ahead and make the user-defined type global so you don’t have to keep passing it around to everything. But there is a better way. Continue reading

Posted in Fundamentals, Programming | 1 Comment

Cleaning Up the Globe (als)

Let’s get one thing straight up front:  global variables are a bad idea.  Yes, there are some exceptions, but for the most part, they’re a bad idea.

There are lots of reasons why they’re a bad idea.  Here are just a few: Continue reading

Posted in Fundamentals, Programming | 1 Comment

Vague Generalities

In “Programming and Cold Pizza” I talked about the DRY principle and avoiding repeating the same code over and over.   That’s a fundamental principle that should always be part of how you write code.

Here’s another one.

Avoid specific cases.

That is, don’t write five routines to do slightly different things, if you can write one routine that will do them all.

Here’s a real example. This is a method in a larger object that is used Continue reading

Posted in Fundamentals, Programming | Tagged | Leave a comment

Programming and Cold Pizza

PIZZAWhat self-respecting programmer doesn’t love a good pizza?  The melty cheese, the zesty spices, the chewy crust — what’s not to love?   Maybe you love it so much that you decide to make your own.  You decide that nothing but the best will do, so you go all out.  You grind the flour for your dough from organic wheat, cook up your own sauce from fresh tomatoes and herbs, make your own mozzarella cheese (yes, really), and add other fresh goodness from the local farmers market.   You spend hours in the kitchen, tossing the dough by hand, spreading the sauce just right, and laying down layers of perfectly placed toppings and cheese.  You wait an hour for the pizza stone to heat up in your oven, slide the pie into place, then after a few more minutes, finally, you bite into the first bite of pure pizza heaven.

But three slices later (or four, or five…) you realize Continue reading

Posted in Fundamentals, Programming | Tagged | Leave a comment

Hello World!

Hello World seems to be the common example to introduce programming, so maybe it’s appropriate for introducing a programming blog.  This blog is about improving code.  Depending on where you are on your programming path, some may be old news, some may be rocket science.  It doesn’t matter.  What matters is learning, improving, and writing better code.  Software drives our world, and if you’re reading this, there’s a good change you’re contributing to that global code pool.   Maybe you will find something here that will help or inspire.   Like Bruce Lee said, “Take what is useful, and discard the rest.”

 

Posted in Uncategorized | Leave a comment