So you want to write some Java — now what?
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.
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.
- 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.
- 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.
- 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.
- Case Matters!
Java is case sensitive. Two variables named “Bar” and “bar” are entirely separate and have no inherent relationship to each other.
- 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.
By robertk July 17, 2015 - 8:08 pm
And one other thing– if you need Scene Builder, you have to get it from Gluon now, not Oracle. The download is at http://gluonhq.com/products/scene-builder/
By Java Development on Windows | Code Lift July 20, 2015 - 10:17 am
[…] is a followup to Prepping for Java, with specifics about setting up a Java development environment on […]