Posts Tagged ‘java’

This is why nobody likes Java

Saturday, June 17th, 2017 | Programming

Recently, I wanted to pass a random number into a unit test. Sounds simple, right? It probably would be if I wasn’t writing it in Java.

The problem is that the class expected a BigDecimal. But the random utils returns a string. And you can’t convert a string to a BigDecimal. So I had to convert it to a Long, and then convert that to a BigDecimal. Here is the code I ended up with:

BigDecimal pageNumber = BigDecimal.valueOf(Long.valueOf(RandomStringUtils.randomNumeric(1)));

Which started me wondering: how many ways to represent a number are there in Java? So, I looked it up. And came up with this list:

  1. AtomicInteger
  2. AtomicLong
  3. BigDecimal
  4. BigInteger
  5. Byte
  6. double
  7. Double
  8. float
  9. Float
  10. int
  11. Integer
  12. long
  13. Long
  14. short
  15. Short

Some of these are understandable. It makes sense to have separate storage for decimals and integers, for example. But do we really need a short and a Short? And a total of 15 different types of number? It’s madness.

It wouldn’t be so bad if you could just compare the two of them. Or pass in a number to a function. But it is a strongly typed language. Which means a world of pain when people use different types.

But that is what you get for trying to use a proper language, I guess.

Running CruiseControl on Gentoo

Thursday, November 22nd, 2012 | Life, Tech

If you are trying to run CruiseControl on Gentoo Linux, you may find that you get an error similar to the following.

line 109: /bin/java: No such file or directory

There is due to the Java path. You can find it using the following command.

whereis java

You can then create a symlink to it.

ln -s /usr/bin/java /bin/java

Try running the command again and it should work.

Your first Java applet

Friday, September 16th, 2011 | Programming, Tech

In previous columns we have fiddled about with some basic javascript so I thought this month we we do something a little different. We’re going to write a simple java applet. Yes you guessed it, its going to be one of those annoying “Hello World!” applets that every single damn language makes you do to get started. The sad fact is though is that they are great a teaching people the basics.

Article Overview

* Before we start
* The applet overview
* Creating the Java Source Code
* Compiling the source code
* Running the program

Before we start

Since we are going to be compiling the java code for the applet it means your going to need The JavaTM 2 Platform, Standard Edition. Its about 37 MB so it may take a while if your on a 56k modem. Use a download manager such as Flash Get. Also – make sure you download the SDK and not the JRE. You can download it from the following website.

http://java.sun.com/j2se/1.4/download.html

Now no one can say I don’t work hard for you. I wish someone had sold me I needed that while I was wondering why it wasn’t working. Your also going to need to set the PATH perminantly unless you know what your doing. Do to the documentation below and follow the steps to settings your path. You need to add some text to the path command rather than replace it by the way. Again I wish someone had told me that. The documentation on how to do this can be found at the following website.

http://java.sun.com/j2se/1.4/install-windows.html

The applet overview

An applet requires a java enabled browser to work rather than being a stand alone java application. Most major browsers supporrt java now although it may be an optional extra on your copy of Netscape or Opera. Internet Explorer automatically supports it. There are three steps in creating your first java applet:

* Creating the Java source code.
* Compiling the source code.
* Running the program.

Creating the Java source code

Open your text editor (Notepad will be fine but I prefer EmEditor) and type in the following:

import java.applet.*;
import java.awt.*;

/**
* The HelloWorld class implements an applet that
* simply displays "Hello World!".
*/
public class HelloWorld extends Applet {
public void paint(Graphics g) {
// Display "Hello World!"
g.drawString("Hello world!", 50, 25);
}
}

Save this code to a file called HelloWorld.java.

You also need an HTML file to accompany your applet. Type the following code into a new text document:

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

Save this code to a file called Hello.htm.

Compiling the source code

Open up command promt (Start > Run > “Command”). Once your at the command promt, go to the directory where you have saved the source code. For expample if you saved them in C:\code you would type cd C:\code. At the prompt, type the following command and press Return:

javac HelloWorld.java

The compiler should generate a Java bytecode file, HelloWorld.class. This file will appear in the same directory.

Running the program

Although you can view your applets using a Web browser, you may find it easier to test your applets using the simple appletviewer application that comes with the JavaTM Platform. To view the HelloWorld applet using appletviewer, enter at the prompt:

appletviewer Hello.htm

Now you should see an application come up with “appletviewer” or something similar at the top. It should then say applet loaded at the bottom and hello world in the middle. Congratualtions! Your applet works.

Don’t work if you got a error from command promt. I got that too saying it was using the default settings etc, but it didn’t seem to cause a problem. For more information on java applets go to http://java.sun.com.