Numbers and Strings

The Numbers and Strings trail is also pretty much a reference trail, but there is a catch, to catch out the unwary beginner. Some of the lessons need to be read very carefully, before you begin programming.

In the languages I've used previously, a number is a number and a string is a string, and that's all you really need to know, besides how to convert one to the other. Not so in Java. Java is the language where all objects have to belong to classes, classes belong to superclasses, and superclasses belong to an object. So to expect a number simply to be a number and a string simply to be a string in Java would be naive.

No, in Java, numbers belong to a number class (with subclasses for the number types), strings belong to a string class, and even formatting belongs to a formatting class.

The implications for programming with numbers don't seem to be too great because "most of the time" you are allowed to use the "primitive types". So when you declare number variables, the following code (quoted from the number class page) is acceptable:

int i = 500;
float gpa = 3.65f;
byte mask = 0xff;

But for some reason, best known to the language designers, you can't do this for strings. They allow it for single characters (quoted from the character page):

char ch = 'a';
char uniChar = '\u039A'; // Unicode for uppercase Greek omega character
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };
// an array of chars

As if anyone would ever code with singles characters!

But for strings you have to create an object from the string class. Here is the code (quoted from the strings page), which they use to introduce the idea:


char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
System.out.println(helloString);

I thought this was so silly, the first time I read it, I completely ignored it, to my cost when I started coding. I think I was put off by the unlikely (to the point of absurdity) construct of building a string from an array. But the fact is you can't declare a string as a primitive type, like you do in VB and other languages. You have to create an object:

String myString = new String("put anything here");

Having done this you can treat it more or less as you you treat the primitive string type in other languages.

The other crucial lesson in this trail is "Converting Between Numbers and Strings".

Converting from numbers to strings is fairly straightforward. It seems you can force it simply by concatenating a number with a string or plonking it into a container intended for strings. I'll illustrate with my own code:

int num1 = 2;
int num2 = 2;
MathItemlabel.setText(num1 + " + "
+ num2 + " = " );

Here I have declared two primitive integers, num1 and num2, and converted them to text by concatenation with strings, so that they can be placed in a test field. That was pretty easy.

Converting from strings to numbers is a bit harder. First of all it seems that you have to convert to a float. Then if you want say an integer, you convert the float to an integer. Again, I'll use my own code:


String answerString = new String(Anstextfld.getText());
float ansf = 0;
int ansi = 0;
ansf = (Float.valueOf(answerString) ).floatValue();
ansi = (int)(ansf);

First I created a string object and filled it with text from a field. Then I declared primitive float and integer variables. Then I converted the string to a float and the float to an integer. Then I could do what I wanted with the integer.

Comments

Popular posts from this blog

A few notes on JavaScript

Forum Comments on Java Applets

Creating a Custom Swing Component