Dads View

February 7, 2009

Java String Compare Headaches

Filed under: Java — Administrator @ 8:00 am

Every language has its quirks and Java is no different.
I just spent a couple hours discovering Java’s string comparison quirk.
To illustrate the issue, take the following Pop quiz:
With the following code what statement gets printed?
String a = “hello”;
String b = “there”;
String c = a + b;
if (c == “hellothere” ) {
System.out.println(“c is hellothere”);
}
else {
System.out.println(“c is not hellothere”);
}

If you answered: c is hellothere
You just stumbled over Java’s string comparison quirk.
The correct answer is: c is not hellothere

Boolean operators (like ==) should not be used to compare strings in Java.
The above comparison would result in “true” only if the two operands pointed to the exact same string in memory — not a likely scenario.
The following code produces the expected result:

String a = “hello”;
String b = “there”;
String c = a + b;
if (c.equals(“hellothere” )) {
System.out.println(“c is hellothere”);
}
else {
System.out.println(“c is not hellothere”);
}

What about greater than an less than comparisons?
You may have already guessed that the > (greater than) and < (less than)
operators don't work on Strings.
Instead use the String compareTo method.

String a = "hello";
String b = "there";

if (a.compareTo(b) < 0) {
System.out.println("a comes before b alphabetically");
}
else {
System.out.println("a comes after b alphabetically");
}

This code fragment will print "a comes before b alphabetically"

The call a.compareTo(b) will return the following values depending on strings a and b

A negative integer value if string a comes before string b when sorted alphabetically
A zero integer value if a and b are equal
A positive value of string a comes after string b when sorted alphabetically

For more information see:
Java String Practice Problems

February 1, 2009

Java CLASSPATH headaches

Filed under: Java — Administrator @ 11:21 am

Continuing my ongoing saga documenting my path to learning Java I discovered an annoying but probably very common problem building and running Java programs.

The application I’m developing will utilize Xalan to process some XML files.

That’s not important to this discussion except for the fact that I am planning to use XALAN predefined libaries contained in .jar files in my application.

Per the installation instructions I dutifully added the directory containing the libaries(as .jar files) to my exisitng CLASSPATH system variable in Windows XP as follows:

Right click My Computer -> Properties -> Advanced tab -> Environment
Variables button -> Under System Variables click CLASSPATH

I added the following to the end:
;c:\path_to_the_libraries\
and saved it.

Important note:
Always make sure that CLASSPATH begins with . – that’s a period.
This will insure that Java can always find things in the current directory.

So I try to build my application:

javac myapplication.java

and I get a screen full of errors!

myapplication.java:32: package name_of_library does not exist
import name_of_library;

Looks like it isn’t finding the libraries! I check and double check the CLASSPATH. There’s the directory! What’s the problem!

So I discover that both javac and java have command line parameters to specify a path to look for .jar files.

I try

javac -cp c:\path_to_the_libraries\library.jar myapplication.java
java -cp .;c:\path_to_the_libraries\library.jar myapplication

And It works! But Why?

JAVA can only find actual .class files in the directories listed in CLASSPATH. It won’t look inside the .jar files to find classes!
Each .jar or .zip file must be listed separately.

After removing the original addition to my CLASSPATH system I added

;c:\path_to_the_libraries\library.jar

And now

javac myapplication.java
java myapplication

Works like a charm!

A significant aspect of this is that JAVA applications must always be distributed as a complete directory containing programs and libaries, with a script (or batch file) containing the commands to execute it.

Since you probably don’t want to alter CLASSPATH on another system (unless its’ yours), specifying the complete classpath on the javac and java command lines is probably the way to go.

Java for beginners

Filed under: Java — Administrator @ 8:04 am

I’ve started a small project where I’ll need a program that I can run on both Windows system and Linux.  I had the choice of using either C++ or Java, and decided to jump into learning Java.   This is a short log of my tribulations in getting Java and Netbeans working on my Windows XP system.

My first task was to figure out what to install.  A quick search around the net makes it clear that Sun is THE authority  on Java.

Sun’s Java Page is clear as mud about what I need to download.

After a little research (since this wasn’t described on sun.com) , I discovered the following:
Java SE – is standalone Java for common platforms such as PCs
Java EE – is Java for large systems with web servers and multi system setups
Java ME – is Java for small standalone boxes (like cable boxes)

My needs more closely matched Java SE, so that’s what I chose under the “Popular Downloads” menu on the right side of the Sun page above.
This is the download screen for Java SE

A bit about how Java works:

Some computer languages such as C++ use a special program called a compiler to convert your program into the native language of your computer’s CPU(machine language).
Software companies must then develop a different compiler for every type of CPU, in some cases, dozens of different versions.
Since each of these compilers is slightly different, your C++ program may not behave the same on all these different computers.
Because of these issues the developers of Java decided to produce a compiler that would convert your program to run on only ONE type of CPU . They called this CPU the Java Virtual Machine.
However, the Java Virtual Machine is not a real piece of hardware.
In order for your Windows XP machine to execute the compiled Java program it needs another piece of software called the Java Runtime Environment (or JRE). The JRE is a program that simulates another CPU (the Java Virtual Machine) on your Windows XP (or other system).

Back to the downloading process…
Therefore, I need to download two separate components:
The Java Runtime Environment
The Java Development Kit (the compiler)

I chose JRE 6 Update 11 and JDK 6 Update 11 with NetBeans 6.5

The install went fairly smooth and then it was time to try to run a sample Java program from the command line:
c:\myprograms\java my_program_name

I get the ugly message:
Could not find the main class: my_program_name . Program will exit.

My first Java Installation problem.

I little more searching on the net gives me my answer:

My source file my_program_name.java must first be compiled into a .class file with the javac utility as follows:

javac my_program_name.java

this gives me the error message:
‘javac’ is not recognized as an internal or external command, operable program or batch file.

Turns out that Sun’s Java install doesn’t do everything it’s supposed to!

Javac is an .exe file that needs to be on the Windows XP system path to be found.

I located javac.exe in C:\Program Files\Java\jdk1.6.0_11\bin

To add a directory to the Windows XP path, do the following:

My Computer -> right click properties -> Click Environment Variables button -> Scroll down list of System variables and select “Path”

Click Edit and use right arrow to scroll to the end. Add the following:
;C:\Program Files\Java\jdk1.6.0_11\bin
Note the semicolon at the beginning of the line.
If you are using a different version of the JDK change the path to match your installation.

Click OK twice.

Close and reopen your command window

Now trying:
javac my_program_name.java
java my_program_name

and I got results!

Remember, Java is case sensitive. Make sure you are typing your Java program names correctly.

Powered by WordPress