Wednesday, September 13, 2006

Java Tutorials: Why no 'unsigned'?

In the java programming language, we dont have the concept of 'unsigned' integers. Once question that arises in the minds of most beginners is this - Why no 'unsigned' integers in Java.

In languages like C, C++, might have used the unsigned integers extensively for declaring variables like 'age' which can never be negative. The programmers prefer ths same style of declaring the non-negative values as unsigned. But Java doesnt allow that.

Before answering why unsigned is not supported in Java, let us see the problems that can happen because of 'unsigned' type.

Let us say we store the age of a person.

unsigned int age;

Now, we have a problem, what if the age of the person is not known? we are comming up with a great plan of using a value '-1', which means 'not known'.

The requirement is, only if the users age is above 18 they should be allowed to perform some action. If they didnt specify the age, he should be blocked. Let us see, how to write the code...


if(age<18){>
printf("You should be above 18 years to access this feature");
return;
}


On seeing the code, it is common to assume that '-1' is less than '18' if the user didnt specify the age, so he will automatically blocked.

But if you check the C specifications, you can find out that any 'signed' variables will be converted to 'unsigned' automatically if both appear in the same expression.

It means, the value '-1' will be converted into its unsigned counter part i.e. 0xFFFF. This is definitly more greater that '18' thus allowing the user to continue the blocked activity.

This is just a trivial example, but this will make it very difficult to trace the problem in the real life projects. To avoid such potential problems, java removed the 'unsigned' keyword.

This reason may look weird, but there is not much to achieve introducing 'unsigned' when compared to the drawbacks. The omission of unsigned data types is controversial. There are reasons where unsigned could help. Example, having unsigned will increase the maximum limit for the data type. All other reasons revolve around this reason. But this could be solved by using a bigger numeral. Example, instead of unsigned int, we can use long. similarly for unsigned short, we can use 'int'. Still, 'long' doesnt have any alternative other than 'BigInteger' wrapper classes.

There are lots of 'Request For Enhancements (RFE)' to add unsigned. Here is one http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504839. They have listed a lots of reasons for having an unsigned integers, but almost all the reasons revolve around the 'size'.

Let us look at, whether it is possible to add the new feature without the drawback that we mentioned.

It looks possible for adding the new keyword without losing the type safety of Java - without losing the title 'stongly typed language'. Use the same level of type checking. Make it stict to use explicit casts to convert between unsigned and signed.

Let us say for 'unsigned int', java adds the keyword 'uint'. Then,


uint a = 10;
int b = 20;

uint c = a + b // should throw compilation error.


We should instead use,


uint a = 10;
int b = 20;

uint c = a + (uint)b // should work


This will guarantee the strong type checking nature of Java and satisfying the needs of the programmers - A win-win situation for both the Java architects and for the Java Programmers.

4 Comments:

Blogger Jagbir said...

Having no unsigned ints leads to shitty code where you end up having bitwise &s and inefficient performance and memory usage.

long sum = (q * delta) & 0xFFFFFFFFL; // All I wanted was a 4 bytes unsigned

I guess you never used java for encryption. Its people like you who will defend java to the point where if someone corners you into admitting that java is shit then you will go on to count the MERITS OF SHIT and why SHIT IS GOOD.

But I gotta agree to one thing that as long as there are people like you as programmers - we definitely do need java.

7:25 AM  
Blogger Shivanand Velmurugan said...

@Jayaprabakar so much for writing a blog and conveying your opinion on the internet, eh? You wrote something useful, and get slammed for it ;)

@Jagbir
1. I think the author is simply stating why Java doesn't support unsigned, and how it can fail, and a possible way of implementing it. I still think it is unnecessary.

2. why is the code sample you wrote shit? It sure is ugly, or even uncomfortable, but I see no reason why you would consider this shit? It is simply the mechanics of the language you use. The real question is, why do you need sum to be unsigned? I see no reason why leaving sum as signed, wouldn't express what you have written. In fact, writing code that does *simply* state what you intend is *SHIT*

3. The only real reason for specifying unsigned was to use it for bitsets. Java has a perfectly good way of doing this using, java.util.BitSet. Secondly, by adding unsigned to the C- spec, the designers have made more overflow, and error conditions that have to be known and constantly checked for by the developer -- which is never a good thing.

4. What is the point of attacking the author, personally. Isn't the internet already populated with garbage? When someone writes something useful/thought-provoking, either add something, by contributing something of value, or simply stay mum. If you do want to go on a diatribe, please do so in your own space. Discouraging a fellow hacker by being verbally abusive is not helpful, and reflects more on you, that him.

(It's better to be a lesser developer, than a lesser human being)

Sorry for the rant, but I thought that was needed

7:24 PM  
Blogger Basic said...

@Shivanand

... And even better still to be a good developer and human being.

However, to the point, the Java designers examined the set of things they could see Java being used for and decided to remove any complexity they thought they could get away with.

Unfortunately, none of them seem to have considered network code, driving hardware, cryptography or interacting with any non-Java language [except PHP as far as I can tell which doesn't really put it in illustrious company].

On the other hand, they could've provided a rich toolset which developers could use to spend more time solving problems and less time working around Java's deficiencies.

10:04 AM  
Blogger Basic said...

@Shivanand

... And even better still to be a good developer and human being.

However, to the point, the Java designers examined the set of things they could see Java being used for and decided to remove any complexity they thought they could get away with.

Unfortunately, none of them seem to have considered network code, driving hardware, cryptography or interacting with any non-Java language [except PHP as far as I can tell which doesn't really put it in illustrious company].

On the other hand, they could've provided a rich toolset which developers could use to spend more time solving problems and less time working around Java's deficiencies.

10:05 AM  

Post a Comment

<< Home