[HD Pg 182, Sec. 8.1.3 - varargs]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 182, Sec. 8.1.3 - varargs]

Post by OCAJO1 »

Code: Select all

public double average(int[] values){
    /* by the way, can you tell what will happen if sum is declared as int?
    double sum = 0;
    for(int i=0; i<values.length; i++) sum += values[i];
        return values.length==0?0 : sum/values.length;
}
Can't test this at this time, but I don't see that anything will change if sum is declared as an int.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 182, Sec. 8.1.3 - varargs]

Post by admin »

Yes, nothing much. sum/values.length will perform an integer division instead of double. So, the average value will not be accurate.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 182, Sec. 8.1.3 - varargs]

Post by OCAJO1 »

keep missing the obvious :oops:

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests