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 8610939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:14:50+00:00 2026-06-12T04:14:50+00:00

I have been trying to understand how some of API methods work below is

  • 0

I have been trying to understand how some of API methods work

below is a snippet of equals method of java.lang.String class

Can someone out there tell me how actually the code is comparing two strings.
I get the significance of count, but what does offset signify. how are these variables getting values ?

Like when i create a String. how are these initialized.

a detailed line by line description and also how and when the instance variables, value, count, offset etc are initialized ??

 public boolean equals(Object anObject) {
  1014           if (this == anObject) {
  1015               return true;
  1016           }
  1017           if (anObject instanceof String) {
  1018               String anotherString = (String)anObject;
  1019               int n = count;
  1020               if (n == anotherString.count) {
  1021                   char v1[] = value;
  1022                   char v2[] = anotherString.value;
  1023                   int i = offset;
  1024                   int j = anotherString.offset;
  1025                   while (n-- != 0) {
  1026                       if (v1[i++] != v2[j++])
  1027                           return false;
  1028                   }
  1029                   return true;
  1030               }
  1031           }
  1032           return false;
  1033       }
  • 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-06-12T04:14:51+00:00Added an answer on June 12, 2026 at 4:14 am

    Logically

    while (n-- != 0) {
    if (v1[i++] != v2[j++])
        return false;
    }
    

    is the same as

    for (int i = 0; i < n; i++) {
        if (v1[i] != v2[j])
            return false;
        }
    }
    

    Why the JVM designers have done it this way I am not sure. Perhaps there is a performance improvement using a while loop than a for loop. It looks quite C like to me so maybe the person who wrote this has a background in c.

    Offset is used to locate where the string starts within the char array. Internally Strings are stored as char arrays. This is value

    if (v1[i++] != v2[j++])
        return false;
    

    checks the characters in the string’s underlying char array.

    and line by line it is

    if the refernce is pointing to the same object is must the equals

    1014           if (this == anObject) {
    1015               return true;
    1016           }
    

    if the object is a string then check they are equal

    1017           if (anObject instanceof String) {
    

    cast the parameter passed in as String.

    1018               String anotherString = (String)anObject;
    

    remember the length of this.string

    1019               int n = count;
    

    if the two string’s lengths match

    1020               if (n == anotherString.count) {
    

    get an array of the characters (value is this array)

    1021                   char v1[] = value;
    1022                   char v2[] = anotherString.value;
    

    find out where in this array the string starts

    1023                   int i = offset;
    1024                   int j = anotherString.offset;
    

    loop through char array. if the values are different then return false

    1025                   while (n-- != 0) {
    1026                       if (v1[i++] != v2[j++])
    1027                           return false;
    1028                   }
    

    everything else must be true

    1029                   return true;
    1030               }
    1031           }
    

    if not of type String then they cannot be equals

    1032           return false;
    1033       }
    

    To understand offset and value look at the String class

    /** The value is used for character storage. */
    private final char value[];
    
    /** The offset is the first index of the storage that is used. */
    private final int offset;
    
    /** The count is the number of characters in the String. */
    private final int count;
    

    The constructors initialises these variables. The default constructor code is below. You should see something similar for the other constructors.

    /**
      * Initializes a newly created {@code String} object so that it represents
      * an empty character sequence.  Note that use of this constructor is
      * unnecessary since Strings are immutable.
      */
     public String() {
        this.offset = 0;
        this.count = 0;
        this.value = new char[0];
     }
    

    This is quite a good link to look at

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

Sidebar

Related Questions

I'm currently trying to understand some of the fundamentals with LINQ. I have been
I have been trying to understand the use of primitives in Java and C#
I have been going through tutorials, and have been trying to understand some things,
I have been trying to understand the way ActionScript's events are implemented, but I'm
okay i have been trying to understand this for hours i am learning VB
So, I have been trying to understand Socket.io lately, but I am not a
Have been trying to encrypt an xml file to a string so that I
I have been trying to setup git for our web development team unsuccessfully. Some
I have been trying to get some basic dynamic code compilation working using the
I have been trying to follow some WCF Data Services examples and have 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.