wood burning stoves*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes indexOf(int a) why int not char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "indexOf(int a) why int not char" Watch "indexOf(int a) why int not char" New topic
Author

indexOf(int a) why int not char

Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 37

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

Joined: Jan 02, 2012
Posts: 192
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 37

Hi Ted and Henry,

Thanks for your replies!
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: indexOf(int a) why int not char