练习题:
Consider the following code:
String entree = new String (“chicken”);
String side = “salad”;
entree = “turkey”;
String dessert;
entree = side;
String extra = entree + side;
dessert = “pie”;
How many String objects were created, and how many are accessible at the end?
How many aliases are present, and is there any garbage?
我的逻辑:创建了 3 个文字,一个带有 new 运算符的字符串,以及一个连接主菜和边,所以总共 5 个对象。
甜点和额外是 2 个对象,侧面和主菜的第三个分配。因此,在创建的 5 个对象中可以访问 4 个对象。
1别名,entre指side。
垃圾,主菜失去了对“火鸡”和“鸡肉”的提及。
你能帮我评估一下我对这个问题的思考过程吗?