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 775411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:17:03+00:00 2026-05-14T19:17:03+00:00

I mostly write in C but am using Java for this project. I want

  • 0

I mostly write in C but am using Java for this project. I want to know what Java is doing here under the hood.

ArrayList<Integer> prevRow, currRow;
currRow = new ArrayList<Integer>();
for(i =0; i < numRows; i++){
    prevRow = currRow;
    currRow.clear();
    currRow.addAll(aBunchOfItems);
}

Is the prevRow = currRow line copying the list or does prevRow now point to the same list as currRow? If prevRow points to the same list as currRow, I should create a new ArrayList instead of clearing….

private ArrayList<Integer> someFunction(ArrayList<Integer> l){
    Collections.sort(l);
    return l;
}

main(){
    ArrayList<Integer> list = new ArrayList<Integer>(Integer(3), Integer(2), Integer(1));

    list = someFunction(list);  //Option 1
    someFunction(list);  //Option 2
}

In a similar question, do Option 1 and Option 2 do the same thing in the above code?

Thanks-

Jonathan

  • 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-05-14T19:17:04+00:00Added an answer on May 14, 2026 at 7:17 pm

    If you come to Java from C/C++ remember this golden rule which will answer almost all these doubts: in Java a variable (except primitives) is always a reference (sort of pointer) to an object, never an object in itself.

    For example

     prevRow = currRow;
    

    is assigning a reference, so that prevRow and currRow now refer to the same object. Objects are not copied, neither in assignments, nor in argument passing (what is copied/assigned is the reference). And so on.

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

Sidebar

Related Questions

I'm trying to write this code using mostly arrays. I'm trying to get Correct
I mostly develop using C#, but I think this question might be suitable for
I'm using django for a website app, mostly. I need to write a canvas
Working in IE 8, mostly, but trying to write a portable solution for modern
My application is mostly Java but, for certain calculations, uses a C++ library. Our
I made a web project in Java, using Java-WS. How can I invoke service
Since I have been mostly using script languages in the past time but now
Whenever I write shell scripts (mostly software development utilities or build tools) I've generally
I need to write software that will do a lot of math. Mostly it
I mostly use vim from the terminal by vi command. I want to create

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.