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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:24:59+00:00 2026-05-26T18:24:59+00:00

The following simple Java program appears to display the string Hello World through the

  • 0

The following simple Java program appears to display the string Hello World through the statement System.out.println("Hello World"); but it doesn’t. It simply replaces this with another string which is in this case, Good Day !! and displays it on the console. The string Hello World is not displayed at all. Let’s look at the following simple code snippet in Java.

package goodday;

import java.lang.reflect.Field;

final public class Main 
{
    public static void main(String[] args) 
    {        
        System.out.println("Hello World");
    }

    static 
    {
        try 
        {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            value.set("Hello World", value.get("Good Day !!"));
        } 
        catch (Exception e)
        {
            throw new AssertionError(e);
        }
    }
}

Just one question about this code here. It works exactly as expected but I can not reduce the length of the string Good Day !!. If an attempt is made to do so, it causes a java.lang.ArrayIndexOutOfBoudsException. If the length is increased, the program runs well but the rest of the characters in the displaying string are truncated means that the length of the both of the strings should somewhat be the same. Why? This is the thing that I couldn’t understand.

  • 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-26T18:24:59+00:00Added an answer on May 26, 2026 at 6:24 pm

    The value field is a char[] which internally stores the character array that the string uses as its backing store. Other fields indicate the initial offset into the character array, and the length of the string. (So to take a substring, it just creates a new String object which refers to the same char[] but with a different starting offset and length.)

    If you modify those fields as well, you can do pretty much whatever you like with the string. Sample:

    import java.lang.reflect.Field;
    
    public class Test
    {
        // Obviously in real code you wouldn't use Exception like this...
        // Although hacking string values with reflection is worse, of course.
        public static void main(String[] args) throws Exception
        {        
            System.out.println("Hello World");
            replaceHelloWorld("Goodbye!");
            System.out.println("Hello World");
            replaceHelloWorld("Hello again!");
            System.out.println("Hello World");
        }
    
        static void replaceHelloWorld(String text) throws Exception
        {
            // Note: would probably want to do hash as well...
            copyField(text, "Hello World", "value");
            copyField(text, "Hello World", "offset");
            copyField(text, "Hello World", "count");
        }
    
        static void copyField(String source, String target, String name)
            throws Exception
        {
            Field field = String.class.getDeclaredField(name);
            field.setAccessible(true);
            field.set(target, field.get(source));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a simple program for RMI. But, I'm getting the following
i just started learning swings. And thought of trying out a simple program, but
The following simple program in Java uses the java.util.Random class such that it always
Say I have the following simple java bean: class MyBean { private Date startDate;
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have a very simple Java RMI Server that looks like the following: import
I have done simple java app for blackberry, while building am getting following error.
I'm getting the following error when I try to run a simple Java JDBC
Should be simple and quick: I want a C# equivalent to the following Java
I am trying to build a simple java program which creates a db file,

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.