string pool IV notes

String str = new String("Cat"); //[1] inefficiency

Based on https://www.quora.com/String-s-new-string-ABC-How-many-objects-are-created-after-the-above-statement .. Net effect of above statement — either 1 or 2 instantiations

> At class-loading time, JVM picks up all string literals in the class file and populates the string pool. IIF this “Cat” is the first occurrence then it would be instantiated in the string pool.

Net effect so far is 0 or 1 instantiation.

> At runtime, the new() forces JVM to instantiate a brand new object, in the regular heap, unrelated to the string pool.

Net effect now is 1 or 2 instantiations.

[1] Note this is (almost) never needed according to P20 [[effJava]]

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 登出 /  更改 )

Google photo

您正在使用您的 Google 账号评论。 登出 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 登出 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 登出 /  更改 )

Connecting to %s