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

  • Home
  • SEARCH
  • 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 1090895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:26:27+00:00 2026-05-16T23:26:27+00:00

package javaapplication8; public class Main { public static void main(String[] args) { int[] list1

  • 0
package javaapplication8;

public class Main {

   public static void main(String[] args) {
     int[] list1 = {1, 2, 3,4};
     int[] list2 = {5, 6, 7,8};

     for (int i = 0; i < list2.length; i++){
       System.out.print(list2[i] + " ");
     }
     System.out.println("");

     list2 = list1;

     for (int i = 0; i < list2.length; i++){
       System.out.print(list2[i] + " ");
     }
     System.out.println("");

     //Change list1
     list1[0] = -1;

     //Change list2
     list2[3] = -4;

     //List1 output
     for (int i = 0; i < list1.length; i++){
       System.out.print(list1[i] + " ");
     }
     System.out.println("");

     //List2 output
     for (int i = 0; i < list2.length; i++){
       System.out.print(list2[i] + " ");
     }
     System.out.println("");

     //Set list1
     list1 = new int[2];
     list1[0] = 100;
     list1[1] = 99;

     //List1 output
     for (int i = 0; i < list1.length; i++){
       System.out.print(list1[i] + " ");
     }
     System.out.println("");

     //List2 output
     for (int i = 0; i < list2.length; i++){
       System.out.print(list2[i] + " ");
     }
     System.out.println("");

   }
}

run:

5 6 7 8 
1 2 3 4 
-1 2 3 -4 
-1 2 3 -4 
100 99
-1 2 3 -4 
  • 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-16T23:26:28+00:00Added an answer on May 16, 2026 at 11:26 pm

    List 1 and List 2 are pointing to same array object after you do List2 = list1;

    so doing List2[3] = -4; actually does it for array object that earlier was pointed by List1.

    and remember the array object that was earlier related to List2 now stands for garbage collection.

    So earlier when u did List1 = x and List2 = y there were two array objects in the memory pointed out by list1 and list2 variables. However after doing list2 = list1 u made both variables point to x array and the other array is free now and java will need to recollect this memory sometime and hence whatever changes ur doing is done to object x rather than y

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

Sidebar

Related Questions

package com.test01; public class test01 { public static void main(String[] args) { System.out.println(hi); }
package hw3; public class Main { public static void main(String[] args) { final int
package org.study.algos; public class Study { public static void main(String[] args) { A a
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package matlab; import com.mathworks.toolbox.javabuilder.*; import com.eigenface.Eigenface; public class Test { public static void main(String[]
Here's my JFrame code: public static void main(String[] args) { JFrame jf = new
package com.valami; public class Ferrari { private int v = 0; private void alam()
package javaapplication1; import java.io.*; public class Main { /** * @param args the command
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static

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.