• 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

Can some one help me ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program :

public class Test {
public static void main(String[] args){
int i=1;
int[] ia = {1};
incr(i);
incr(ia);
System.out.println(" i = " + i + " ia = " + ia[0]);
}
public static void incr(int n){n++;}
public static void incr(int[] n){n[0]++;}
}

Output : i = 1 ia = 2

Output according to me: i = 2 ia[0] = 2

In method public static void incr(int n){n++;} n = 1 and n is getting incremented by 1. So why its generating i=2 ?
Can someone please explain ?
Thank you
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shahid
please quote your sources and use the code tags when posting the code
 
Shahid Ansari
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:Shahid
please quote your sources and use the code tags when posting the code



Hi Prasad,

I am new here so don't have much idea how everything works here.
I am preparing for a mock test and there I found this program.

Let me know If you want to know any thing else.

Thanks
 
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am new here so don't have much idea how everything works here.


Shahid, welcome to JavaRanch
It is required to UseAMeaningfulSubjectLine, QuoteYourSources (let us know where you got this question from), and UseCodeTags.
You can edit your post by clicking the button.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shahid
well its not a mistake at all that you did not post your code in code tags and the source
it happens to everyone new here
well I want you to go through some of our ways of posting and helping
and these are for getting better help
you can find them here
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shahid Ansari wrote:I am preparing for a mock test and there I found this program.


We need you to tell us the name of that mock test where you found this program. This is required at javaranch. You might be overwhelmed by the number of rules you were pointed out to, but you'll get used to it and you'll realize that they are what makes this site a friendly place ...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shahid,

This is my first post here too, i'm studying for SCJP. This is what i think is going on


When "incr(i)" is executed the local variable n is indeed increased but nothing is passed through because you are only passing thourgh a value and the value isn't assigned back to "i", when "incr(ia)" is executed you are passing through a reference not the object itself so the value that is at the address "ia[0]" is in fact incremented.
To better see the difference try it with this:

Now it will return an int, notice that i've changed the postfix operator to an unary operator because if it would be a postfix it would first return the value of n and not even get to increment it, so it would be a completly useless operation.
I hope i explained ok.
 
reply
    Bookmark Topic Watch Topic
  • New Topic