请问下面题目的运行结果是什么?为什么?

public class Test {
    String str = "good";
    char[] ch = {'a','b','c'};
    public static void main(String[] args){
        Test t = new Test();
        t.change(t.str, t.ch);
        System.out.print(t.str+" and ");
        System.out.print(t.ch);
    }
    public void change(String str,char ch[]){
        str="test ok";
        ch[0]='g';
    }
}


老衲有一只猪
浏览 1303回答 2
2回答

慕瓜9220888

GLXMRKEBUBJSHGCZWWOGYDXYEPGJRWAGTWZSSGIBDKDKTGQAGQESPVAPACFSBOLMNPLRVBYIYGLEZZQRITKNWYCFCLGZLAGJMZEWHUGIRYLOJVKXQDXKLJUQZVRUYHTXZVNQUUDUCUTPMAEXVZPADZCZWOGJVKXDISKOXDFIYTNIGGVVZMKHWLAQSDGDNDRTJRYBUOIDVAGPCZWPKFYKDVPSXRFOWICSCQUMFQNHMIJOBOQKYOLKMVIWQURMQCPVLFWQUOJPLLFTGUXPFHJQURRBJWSYSMKFAIKZBLIBYIJVBTNZQDNYAKBURVGLPXXFIPMMILAVBYRUHQPJFOHEUEIAZJPMOWDMNTFSSSMFAFOQBXREOKBVQEBKHRITAMRENAWQLNMOEXRKKFFXJCZYLXMQWPJGFZXVVXJWZCVZYRAPSMSMUMTLURKAXFTWBNHHUXIWTVUKXRAGCVNDWZMMIUQKGQQUVUXWSBOKBOYPRTTMZMZVQZHDQTILSWMOXDAJGKIAHDWDQTAKCUAWNEOJSLXNBLRECWLHDAWTWSATVHMZSPRWLHWGTPTCZRVEREBKDEGGHHKHAGBDJORVIHLDTZFJDKKTOYAPCZJGFAWRCPIBIULZOKDDXTHJRLRKHEXUEXSBBUBEGYDQWPCFSOQHTJVAYYHMJFHKQDAMHMHJZMZZMNJFEEXEHGYLDPIZMSMCKGCCCVYBJIOKUXEAMOBAPJFSZBTOHDMJSPQVUZBIBFYWODPVCZMWHWJBDKVIMNCKQFFODDSWPHWTDTGJYZPFSWIZHVPCIVUYLZUMBXHNAVMXJSDAFFSRTLNZGJQZTHTZHUXEOPXFBXAAQHQYBAARBFJPNGVHKHGGHMXTSAKNXFQPVKKHKQVJIEJWYLVMGMLGNUXAETVNGJPERFXZZVEYBFHSUGTDGNJXVQCYVENWNEGIOIVZTPUOQIBVBOMCRURBKXROPZUGDQPSUDIIGTXOZRJRNJMFANRPBRVOLHHVAACXBANUEZAVRUXEXHWRHCJCWCNSKQFZVZPHZODSFPQNPADFAJSZWLNFAZGHRUQLXJEYOAJXPPRTCGMLAVEZVYOFWMRUWYROYEGRQMOBRBENVZUADADQRVXQITGAMONFRWJGATUJHNMWZFIJBAPHMQMQJCYTWIMFVVNTJPZGKUUPKWFSPKNPUMEADXSVPBRJLLFCVNLHZWTOEYKYOGOMNDTDHJPOEOKORDDJMZMJMLZPOAAEXQNVXYQNQTGUQGINQGTZNYOZHUXAQXFYMOVOBUASNSERVYBUYQCYBNJWJEDZUHOBKEWSNGJJWGCIAMVBLILBFOKWZWCGOJBHCGTZWLQYIOBYLOOFYDXELEBKEXSELOLVYIBQZGJTZVXKJVKNNXDLBEUPKRYVAPOHLIWWIKQCMMQUHSUYSTNURFIGCIOLFTNVEXRQEYCGVTUDFVYVYSNRMPIILOHCOWSPOBIQVKPBLYVBWUDFRVYERYJFNDWZCPYGDIHUKDUSQWWMRBFGZAEZLOIFINUQVPJCSMIWFEXKXNKNEALUHUBOXEGIURCZWLXMYNUXAEBQOUJWQWQVGSUKNGTITVVHJGJQTCKCYKDJWWGKJVNKNUDNFNCFPPZM

慕标5263832

String类比较特殊,创建对象后值不会改变,改变的只是地址的引用,那些新的string对象需要在堆中重新开辟内存空间,所以值的本身不会变,只是引用地址发生变化而已

慕莱坞1975833

运行结果:good and gbc涉及到作用域的问题,因为t是Test的对象,t.str指向的就是是类变量也就是good,方法里面的str为局部变量,如果在方法里输出str,就近原则则输出test ok,因为在方法里面没有重新声明ch,所以ch[0]被重新复制为g,要注意类变量是全局的,也就是你在这个类的任何地方都可以访问
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java