Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Does Varargs create a new object when given an array?

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    119
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Does Varargs create a new object when given an array?

    Hi guys.

    My question is: If I have a method which takes varargs, for example this:
    public class Buffer {
     
    	public void put(float ... data) {
    		// do stuff!
    	}
     
    }
    And I input an array, like this:
    public class Application {
     
    	private Buffer buffer;
    	private float[] temp;
     
    	public void submit() {
    		buffer.put(temp);
    	}
     
    }

    Would a new object be generated when "put" is called? Or would the array "temp" be used instead?

    Thank you very much.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Does Varargs create a new object when given an array?

    Quote Originally Posted by Cornix View Post
    Would a new object be generated when "put" is called? Or would the array "temp" be used instead?
    The temp array is directly passed (a copy of the reference, obviously), no new array in this case.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    119
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Does Varargs create a new object when given an array?

    Thank you for the answer. This is very helpful indeed.

Similar Threads

  1. How to create an array of Map<String, Object>
    By lalossa in forum Collections and Generics
    Replies: 1
    Last Post: February 17th, 2013, 06:31 AM
  2. Create Vector of Object
    By bobbyk in forum Collections and Generics
    Replies: 3
    Last Post: March 13th, 2012, 11:20 AM
  3. Create Object
    By TitanVex in forum Object Oriented Programming
    Replies: 8
    Last Post: December 29th, 2011, 10:24 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. How do I create an object(function)?
    By amarettoCoffee in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2011, 06:54 PM