Java Certification Example

What is considered an object in this matter? The array has 7 doubles plus the array itself.

How many objects will be present after the next code fragment is executed?

double[] ann = new double[ 7 ];
double[] bob;
bob = ann;

2 7 14 1

+5
source share
5 answers

only one object new double[ 7 ];

double [] bob; also refers to the same object created in the previous step.

+7
source

There will be only one object. The one you create with new double[7]. boband ann- these are just references to this object, and 7 doubles are primitives.

+2
source

: double[7], 2 (ann bob). , Object,

+2

double[] ann = new double[ 7 ];

Array Object, Reference ann.

double[] bob;
bob = ann;

In the line above, you create a reference variable for the array object bob. And assigning a link to Array Objectthat is also mentionedann

+2
source

Thank. I tested it with some code.

 public class TestCode {

        /**
         * @param args
         */
        public static void main(String[] args) {
            double[] ann = new double[ 7 ];
            double[] bob;
            bob = ann;
            System.out.println(bob.getClass().getSimpleName());
            System.out.println(bob[6]);

            //there are 7 double references initialized to 0.0
        //array refs point to same object
        //1 object of type double[]
    }

}
0
source

All Articles