Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Recommended way to make shallow copy of array Date: Tue, 13 Jan 2015 18:00:08 -0500 Organization: A noiseless patient Spider Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 13 Jan 2015 22:59:47 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="2797"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ksz2GFcrTd1FJUe0D5r0t" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: Cancel-Lock: sha1:bQl75wQ6hjLjKU3Tr8JT8hLIXdM= Xref: csiph.com comp.lang.java.programmer:30699 On 1/13/2015 5:04 PM, Sebastian wrote: > Am 13.01.2015 20:50, schrieb Volker Borchert: >> Given an array of runtime component type T, I want to create a new >> array of the same runtime component type referencing the same objects. >> The two ways I know to achieve this are Arrays.copyOf() and T[].clone(). >> Target VM is Hotspot Client of Sun JDK 1.7.0_72, will move on to Sun >> JDK 1.8.0_ some time not so far in the future. Which >> method should I prefer? Any other suggestions? Primary use case are >> defensive copies, where T is some immutable type or a primitive. >> >> TIA >> >> > > There are several ways to copy an array : >[...] > - call its clone method, and do a cast (the simplest style, suitable for > a shallow clone) Why cast? "The return type of the clone method of an array type T[] is T[]." http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.7 -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM