Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5974961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:00:59+00:00 2026-05-22T21:00:59+00:00

I am wondering why the parameter to indexOf method an int , when the

  • 0

I am wondering why the parameter to indexOf method an int , when the description says a char.

public int indexOf(int ch)

Returns the index within this string of the first occurrence of the specified **character**

http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#indexOf%28int%29

Also, both of these compiles fine:
char c = 'p';
str.indexOf(2147483647);
str.indexOf(c);

a]Basically, what I am confused about is int in java is 32bit , while unicode characters are 16 bits .

b]Why not use the character themselves rather than using int . Is this any performance optimization ?. Are chars difficult to represent than int ? How ?

I assume this should be simple reasoning for this and that makes me know about it even more !

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:00:59+00:00Added an answer on May 22, 2026 at 9:00 pm

    The real reason is that indexOf(int) expects a Unicode codepoint, not a 16-bit UTF-16 “character”. Unicode code points are actually up to 21 bits in length.

    (The UTF-16 representation of a longer codepoint is actually 2 16-bit “character” values. These values are known as leading and trailing surrogates; D80016 to DBFF16, and DC0016 to DFFF16 respectively; see Unicode FAQ – UTF-8, UTF-16, UTF-32 & BOM for the gory details.)

    If you give indexOf(int) a code point > 65535 it will search for the pair of UTF-16 characters that encode the codepoint.

    This is stated by the javadoc (albeit not very clearly), and an examination of the code indicates that this is indeed how the method is implemented.


    Why not just use 16-bit characters ?

    That’s pretty obvious. If they did that, there wouldn’t be an easy way to locate code points greater than 65535 in Strings. That would be a major problem for people who develop internationalized applications where text may contain such code points. (A lot of supposedly internationalized applications make the incorrect assumption that a char represents a code point. Often it doesn’t matter, but increasingly often it does.)

    But it shouldn’t make any difference to you. The method will still work if your Strings consist of only 16 bit codes … or, for that matter, of only ASCII codes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString
I was wondering which is better style to return a parameter from a method:
I'm wondering what exactly the status parameter in the pthread_join is for int pthread_join(pthread_t
I am wondering what the atomically: parameter stands for in the writeToFile:atomically: method (
I'm just wondering what it means to attach a number as a parameter to
I was wondering if it's possible to pass in a parameter to methods when
I'm wondering if it's possible to conditionally add a parameter in a call to
i was wondering, can a parameter be used more then once in the same
I was wondering if there was a more elegant way than this example to
I'm currently wondering if there is a better solution than passing this scope to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.