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

  • Home
  • SEARCH
  • 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 875555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:17:22+00:00 2026-05-15T11:17:22+00:00

Possible Duplicate: Constant abuse? I’ve seen -1 used in various APIs, most commonly when

  • 0

Possible Duplicate:
Constant abuse?

I’ve seen -1 used in various APIs, most commonly when searching into a “collection” with zero-based indices, usually to indicate the “not found” index. This “works” because -1 is never a legal index to begin with. It seems that any negative number should work, but I think -1 is almost always used, as some sort of (unwritten?) convention.

I would like to limit the scope to Java at least for now. My questions are:

  • What are the official words from Sun regarding using -1 as a “special” return value like this?
  • What quotes are there regarding this issue, from e.g. James Gosling, Josh Bloch, or even other authoritative figures outside of Java?
  • What were some of the notable discussions regarding this issue in the past?
  • 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-15T11:17:23+00:00Added an answer on May 15, 2026 at 11:17 am

    This is a common idiom in languages where the types do not include range checks. An "out of bounds" value is used to indicate one of several conditions. Here, the return value indicates two things: 1) was the character found, and 2) where was it found.
    The use of -1 for not found and a non-negative index for found succinctly encodes both of these into one value, and the fact that not-found does not need to return an index.

    In a language with strict range checking, such as Ada or Pascal, the method might be implemented as (pseudo code)

       bool indexOf(c:char, position:out Positive);
    

    Positive is a subtype of int, but restricted to non-negative values.

    This separates the found/not-found flag from the position. The position is provided as an out parameter – essentialy another return value. It could also be an in-out parameter, to start the search from a given position. Use of -1 to indicate not-found would not be allowed here since it violates range checks on the Positive type.

    The alternatives in java are:

    • throw an exception: this is not a good choice here, since not finding a character is not an exceptional condition.
    • split the result into several methods, e.g. boolean indexOf(char c); int lastFoundIndex();. This implies the object must hold on to state, which will not work in a concurrent program, unless the state is stored in thread-local storage, or synchronization is used – all considerable overheads.
    • return the position and found flag separately: such as boolean indexOf(char c, Position pos). Here, creating the position object may be seen as unnecessary overhead.
    • create a multi-value return type

    such as

    class FindIndex {
       boolean found;
       int position;
    }
    
    FindIndex indexOf(char c);
    

    although it clearly separates the return values, it suffers object creation overhead. Some of that could be mitigated by passing the FindIndex as a parameter, e.g.

    FindIndex indexOf(char c, FindIndex start);
    

    Incidentally, multiple return values were going to be part of java (oak), but were axed prior to 1.0 to cut time to release. James Gosling says he wishes they had been included. It’s still a wished-for feature.

    My take is that use of magic values are a practical way of encoding a multi-valued results (a flag and a value) in a single return value, without requiring excessive object creation overhead.

    However, if using magic values, it’s much nicer to work with if they are consistent across related api calls. For example,

       // get everything after the first c
       int index = str.indexOf('c');
       String afterC = str.substring(index);
    

    Java falls short here, since the use of -1 in the call to substring will cause an IndeOutOfBoundsException. Instead, it might have been more consistent for substring to return "" when invoked with -1, if negative values are considered to start at the end of the string. Critics of magic values for error conditions say that the return value can be ignored (or assumed to be positive). A consistent api that handles these magic values in a useful way would reduce the need to check for -1 and allow for cleaner code.

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

Sidebar

Related Questions

Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Avoiding repeated constants in CSS We have some theme colors that are
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the

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.