Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Array
sandhi mridul
Ranch Hand
Posts: 71
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am weak in array. Please do help me with this question
public static void main(String[] args){ int[] a = {1}; Test5 t = new Test5(); t.increment(a); System.out.println(a[a.length - 1]); } void increment(int[] i){ System.out.println(i[i.length - 1]++); }
the output is 2.
I am not getting how?
thanks
sandhya
Deepak Chopra
Ranch Hand
Posts: 433
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Sandhi,
you need to understand two logic here..!!
when you wrote this:
int[] a = {1};
What is the meaning of this:
this means:
a ---> refers to an int array on heap ---> size of array is 1 ---> a[0] refers to 1
when you call method by passing
a
that means you are passing the reference to that array Object, if you pass
a[0]
you are passing the value 1.
Inside Method:
Now i is also referring to one array object.
i ---> refers to an int array on heap ---> size of array is 1 ---> i[0] refers to 1
In fact both a and i refers to same object.
you increment the i[0] by 1, so now..
i ---> refers to an int array on heap ---> size of array is 1 ---> i[0] refers to 2
Or we can say that:
a ---> refers to an int array on heap ---> size of array is 1 ---> a[0] refers to 2
As both were referring to same object.
Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%),
The Jovial Java
,
java.util.concurrent tutorial
sandhi mridul
Ranch Hand
Posts: 71
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks sunny. i got it now.
sitaram
Greenhorn
Posts: 26
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public static void main(
String
[] args){
int[] a = {1};
Test5 t = new Test5();
t.increment(a);
System.out.println(a[a.length - 1]); // o/p is 2
}
void increment(int[] i){
System.out.println(i[i.length - 1]++); // o/p is 1
}
sridhar row
Ranch Hand
Posts: 162
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I think because of the post increment operator it first prints 1 and then increments by 1. since both a and i are referring to the same object, a[0] will be 2 and hence prints 2 the second time.
Gitesh Ramchandani
Ranch Hand
Posts: 274
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In simple words,
System.out.println(i[i.length - 1]++);
increments the value of the element in the array, by 1.
Gitesh
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
++ Operator.......
assignment of final member variables
cannot find symbol
I need Help
SOS: i have some questions about Thread
More...