• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String indexOf method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

The documentation for indexOf method shows that it can take an int. Yet I can pass in a character and it works just fine. What is going on here? Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.

Thanks
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yet I can pass in a character and it works just fine. What is going on here?



A char can be implicitely casted to an int, so it works just fine.

Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.



Apparently, there are some unicode characters that don't fit into a char -- something called a codepoint, that is the size of an int. Maybe someone else can explain this, as I never used codepoints before.

Henry

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clint Smith wrote:Hi folks,

The documentation for indexOf method shows that it can take an int. Yet I can pass in a character and it works just fine. What is going on here? Why wouldn't they build that method with a signature with a char which seems more appropriate for when you are searching a string for a specific character.

Thanks



It's a good question. I think if you better understand the relationship between int and char you will be ok.

for example you can have a line

int n = 'Q';

Then system.out.println(n); will display the number 81.

I found the following page to be very helpful as a reference. The key columns for what we are discussing is DEC and Symbol

http://www.ascii-code.com/


p.s. ok Henry snuck in ahead of me.
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic