• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

array doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output is 10. I think output is 1. As small is a local to a main method. So it pass by value. ow the answer is 10. Can any one explain?
==========================================================================

What will be the output?
A1 14:0-answer
A2 0:14
A3 0:0
A4 Compiler Error

--------------------------------------------------------------------------------

What will be the output?
A1 13:0 -answer
A2 14:0
A3 0:14
A4 0:13
I dont under stand output.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the SOURCE???
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one: keep in mind that all arrays are objects, so you're passing it by reference. Even if the element type is a primitive, the array itself is an object.

Then here:

The first evaluation of i is 5, so you will be setting arr[5]. The ++ suffix will then set i to 6.
The expression on the right is evaluated like (++i) + (i++).
First thing, i is incremented to 7. Then you add that to i, which is 7, and assign that result to arr[5]. Finally you increment i to 8, with no effect on arr[]. So you end up with 7+7 which is 14.

Then:


Again you're setting arr[5] and increment i to 6.
Then it's (i++) + (i++).
First operand is 6. Then i is incremented to 7. Second operand is 7. Finally i turns to 8, but the sum is 6+7 which is 13.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi dolly,

check and go through the following:

PBValue or PBRef Comprehensive Detail
https://coderanch.com/t/261764/java-programmer-SCJP/certification/PBValue-or-PBRef-Comprehensive-Detail


Gitesh
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
say it with me:

Java is pass by value.

you have to understand what you are passing, though. you are passing the value of the reference (for what are thought of as objects, but are really object references). So, "small" holds basically the way to get to the object, not the object itself. It's like the variable "small" holds your home address, but isn't your actual house.

so, when you make this call:

t.method(small);

you are saying "take the way to get to the object, and hand that to the method". so, in the method, you have a copy of the way to get to the object. or, you give someone a photocopy of your home address.

the method then says "go to the object i'm talking about, and change something". naturally, if i tell you to go to my house and paint it blue, it doesn't matter that you just had a copy of my address, you still went to my house.

if, in the method, you did this:



you change the address written down. If after I give you my address, you write someone else's address replacing mine, then you're not gonna paint my house when you go to that address.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg & gitesh. I got it now.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome Dolly.
 
What's a year in metric? Do you know this metric stuff tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic