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 5 of 5

Thread: What is going on here?

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is going on here?

    Hey all,

    So about two weeks ago I decided that I was going to dive in and finally learn a programming language. Of course I am here because I chose Java. Aside from numerous online resources, I also purchased Head First Java, which I think has been really amazing at explaining concepts. I have to admit some of the assignments it give are really difficult for a beginner, but I have been managing up until now. Can someone please tell me what is going on here? The assignment is to determine which of the reference variable refer to which objects. Not all reference variables will be used.
    class HeapQuiz {
     
       int id = 0;
     
       public static void main (String [] args) {
          int x = 0
          HeapQuiz [] hq = new HeapQuiz[5];
          while ( x < 3) {
              hq[x] = new HeapQuiz();
              hq[x].id = x;
              x = x = 1;
           }
     
           hq[3] = hq[1];
           hq[4] = hq[1];
           hq[3] = null;
           hq[4] = hq[0];
           hq[0] = hq[3];
           hq[3] = hq[2];
           hq[2] = hq[0];
        }
    }
    Here are the answers...

    hq[1] -----> id = 1
    hq[3] -----> id = 2
    hq[4] -----> id = 0


    I understand that an array is created. I think I understand the hq[x] represents the the array positions, but how is x being assigned? How does the JVM progress through the loop? The answers are posted above, but I have not idea how the loop is producing those answers. Any help is greatly appreciated. Thanks in advance.

    The Noobiest of all Noobs
    Last edited by LB74; December 16th, 2020 at 09:40 PM.

  2. #2
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with problem: Reference Variable and while loop question

    Hey all,

    So about two weeks ago I decided that I was going to dive in and finally learn a programming language. Of course I am here because I chose Java. Aside from numerous online resources, I also purchased Head First Java, which I think has been really amazing at explaining concepts. I have to admit some of the assignments it give are really difficult for a beginner, but I have been managing up until now. Can someone please tell me what is going on here? The assignment is to determine which of the reference variable refer to which objects. Not all reference variables will be used.

    class HeapQuiz {
     
       int id = 0;
     
    public static void main (String [] args) {
          int x = 0; 
         HeapQuiz [] hq = new HeapQuiz[5];
              while ( x < 3) {
              hq[x] = new HeapQuiz();
              hq[x].id = x;
             x = x = 1;
         }
     
            hq[3] = hq[1];
            hq[4] = hq[1];
            hq[3] = null;
            hq[4] = hq[0];
            hq[0] = hq[3];
            hq[3] = hq[2];
            hq[2] = hq[0];
        }
    }
    Here are the answers...

    hq[1] -----> id = 1
    hq[3] -----> id = 2
    hq[4] -----> id = 0


    I understand that an array is created. I think I understand the hq[x] represents the the array positions, but how is x being assigned? How does the JVM progress through the loop? The answers are posted above, but I have not idea how the loop is producing those answers. Any help is greatly appreciated. Thanks in advance.

    The Noobiest of all Noobs
    Last edited by LB74; December 16th, 2020 at 09:42 PM. Reason: title change

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with problem: Reference Variable and while loop question

    Have you tried to compile the posted code? Were there any errors?

    I don't see where x is declared. That should cause a compiler error.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is going on here?

    I fixed the code and x is now declared.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: What is going on here?

    What happens when you compile and execute the posted code?
    How do you know what is in any of the variables? There aren't any print statements to show the contents of the variables.
    If you don't understand my answer, don't ignore it, ask a question.

Tags for this Thread