Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:13:56+00:00 2026-06-01T15:13:56+00:00

I have a very small program: public static void main(String[] args) { Queue<String> queue

  • 0

I have a very small program:

    public static void main(String[] args) {

Queue<String> queue = new LinkedList<String>();
queue.add("one");
queue.add("two");
queue.add("tree");

printQueue(queue);
customizeQueue(queue);
printQueue(queue);
}

private static void customizeQueue(Queue<String> queue) {
queue.add("four");
queue.add("five");
printQueue(queue);
}

private static void printQueue(Queue<String> queue) {
for(String s: queue){
    System.out.print(s + " ");
}
System.out.println();
}

I’m expecting an output of:

one two tree
one two tree four five
one two tree

However I’m getting:

one two tree
one two tree four five
one two tree four five

I’m not sure why this is happening. Am I passing the reference of the LinkedList instance? Can somebody please clarify why I’m not getting my expected output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:13:58+00:00Added an answer on June 1, 2026 at 3:13 pm

    All types are passed by value in Java. However, what you are passing is not the object, but the reference to the object. This means that a copy of your whole queue is not created when you pass the reference, instead only a copy of the reference is created. The newly created reference still belongs to the same object and therefore when you do a queue.add() , elements are added to the real object. On the other hand, reassigning the reference in the function queue = new LinkedLIst<String>()would have had no effect on the reference in the calling function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very small c++ program that consists of two files: main.cpp and
I'm very new to PHP and I have a small task of displaying a
I have written a very small C# program, that uses a very small SQL
I have written a small program: void showrecord() { char *a[]={ O_BILLABLE_ACCOUNT,O_CUSTOMER_TYPE_INDICATOR, O_A_PARTY_MSISDN_ID,O_A_PARTY_EQUIPMENT_NUMBER, O_A_PARTY_IMSI,O_A_PARTY_LOCATION_INFO_CELL_ID,
I have a program where I deal with a lot of very small numbers
I am very new to assembly, and I'm trying to build a small program.
i am working on a small java program, its very basic and one of
I have a very small program in Xcode only displaying a label and changing
i have a very small utilty app written in c# that works fine on
I have a very small form where a user can enter their zip code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.