Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

indexOf(int a) why int not char

 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the String methods is the indexOf() and one of which has a constructor that accepts an integer.
When you look into its API, what it does actually is return the index of the character passed, and since you can store a char on int, it compiles and works fine. But why did they chose the bigger integer on this (compared to char which should just fit just fine). On top of that, characters are unsigned while integers are signed. And considering the current method signature, it's much more prone to error, since you can pass a negative number, if it's a char, it will produce a compile error which is easy to spot.
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yin Stadfield wrote:One of the String methods is the indexOf() and one of which has a constructor that accepts an integer.
When you look into its API, what it does actually is return the index of the character passed, and since you can store a char on int, it compiles and works fine. But why did they chose the bigger integer on this (compared to char which should just fit just fine). On top of that, characters are unsigned while integers are signed. And considering the current method signature, it's much more prone to error, since you can pass a negative number, if it's a char, it will produce a compile error which is easy to spot.


Yin,

Excellent question. I just looked up the indexOf(int char) method in the String class of the Java7 API and was shocked to see that it does take an int and not a char. This really is strange since as you point out char's are unsigned positive ints while the primitive int data-type has a much larger range from negative numbers to a positive range in the billions that is way beyond the scope of the char data type.

If you pass an enormous negative int to this method it will simply return -1 and not result in a runtime or compile time error. I re-checked the API and it does not throw any checked exceptions or runtime exceptions. So it seems the only thing will happen is negative one is returned to the caller if something bogus is passed as an argument to this method. Check out this program I made for this:


If you copy and paste this code, compile it, and then run it you will see that it simply prints "index of -2_100_300_200 is: -1".

Happy Coding,

Ted
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yin Stadfield wrote:One of the String methods is the indexOf() and one of which has a constructor that accepts an integer.
When you look into its API, what it does actually is return the index of the character passed, and since you can store a char on int, it compiles and works fine. But why did they chose the bigger integer on this (compared to char which should just fit just fine). On top of that, characters are unsigned while integers are signed. And considering the current method signature, it's much more prone to error, since you can pass a negative number, if it's a char, it will produce a compile error which is easy to spot.


The JavaDoc actually explains why -- but admittedly, it does require a little bit of explanation.

When Java was invented, the unicode set, used for characters, were 16 bits long. This is why the java char is an unsigned sixteen bit variable. Today, unicode is actually bigger than that, and could conceivably get bigger in the future.

So, the Character class, and some classes that uses characters (such as string), support a concept of a code point. Basically, a code point is a unicode character -- and it is a character that could be larger than what the char variable can hold. Yes, using an int to represent code points is incorrect, as code points are currently smaller than an int, and can't be negative... but changing the size of the char is *not* a good idea either, as it can break lots of legacy programs.

Henry
 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ted and Henry,

Thanks for your replies!
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yin Stadfield wrote:Hi Ted and Henry,

Thanks for your replies!


You are welcome. I learned more by replying, so thank-you for asking about an interesting method from the Java API.

Happy Coding,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic