zoukankan      html  css  js  c++  java
  • 2种方法改变String 值

        @Test
        public void test1() throws Exception {
            String s = "ttttttt";
            Unsafe u = getUnsafeInstance ();
            Field valueInString = String.class.getDeclaredField ( "value" );
            long offset = u.objectFieldOffset ( valueInString );
            long base = u.arrayBaseOffset ( char[].class );
            long scale = u.arrayIndexScale ( char[].class );
            char[] values = (char[]) u.getObject ( s, offset );
            u.putChar ( values, base + scale, 'c' );
            s = "ttttttt";
            String s1= "ttttttt";
            System.out.println ( "s:" + s );
            System.out.println ( "s1:" + s1 );
        }
       public static Unsafe getUnsafeInstance() throws Exception {
            Field unsafeStaticField =
                    Unsafe.class.getDeclaredField ( "theUnsafe" );
            unsafeStaticField.setAccessible ( true );
            return (Unsafe) unsafeStaticField.get ( Unsafe.class );
        }
    

      

        @Test
        public  void test2() throws Exception {
            String s = "eeee";
            Field valueInString = String.class.getDeclaredField ( "value" );
            valueInString.setAccessible ( true );
            char[] chars ={'a','c','c'};
            valueInString.set ( s, chars);
            System.out.println (s);
            String s1 = "eeee";
            System.out.println (s1);
        }

     

  • 相关阅读:
    JSON基础知识
    Java 环境配置
    接口测试基础知识
    Fiddler初学笔记
    es6数组方法findIndex()
    sass+less相关
    前端库/框架/插件相关
    知名博主相关
    CSS相关
    移动Web相关
  • 原文地址:https://www.cnblogs.com/JohnBoy/p/10342149.html
Copyright © 2011-2022 走看看