一个简单的Java代码,运行良好但功能有点难以理解 [英] A simple Java code that works well but functions in a way that is somewhat difficult to follow

查看:76
本文介绍了一个简单的Java代码,运行良好但功能有点难以理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单Java程序似乎通过语句 System.out.println(Hello World)显示字符串 Hello World ; 但事实并非如此。它只是用另一个字符串替换它,在这种情况下, Good Day !! 并将其显示在控制台上。根本不显示字符串 Hello World 。让我们看看Java中的以下简单代码片段。

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);
        }
    }
}






这里只有一个关于此代码的问题。它完全符合预期,但我不能减少字符串的长度美好的一天!! 。如果尝试这样做,则会导致 java.lang.ArrayIndexOutOfBoudsException 。如果长度增加,程序运行良好但显示字符串中的其余字符被截断意味着两个字符串的长度应该有些相同。为什么?这是我无法理解的事情。


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.

推荐答案

field是 char [] ,它在内部存储字符串用作其后备存储的字符数组。其他字段表示字符数组的初始偏移量以及字符串的长度。 (所以要取一个子字符串,它只是创建一个新的 String 对象,该对象引用相同的 char [] 但是一个不同的起始偏移和长度。)

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));
    }
}

这篇关于一个简单的Java代码,运行良好但功能有点难以理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆