Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

string concatenation with integer

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code :
the output of above program is :
dostuff x5
dostuff x 2nd6
main x=5
i am not clear that even the value of x is incremented to 6 , but it print 5 at the end why ?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because java is pass by value. There is no code there that changes the value of the x variable declared on line 4.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Puneet,

Does Java pass by reference or by value? says -

The key with pass by value is that the method will not receive the actual variable that is being passed - but just a copy of the value being stored inside the variable.



Regards,
Dan
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you pass a value of a primitive to a method and manipulate it inside the called method, the change is not reflected inside the calling method. This is because Java uses pass by value method.
If you want the incremented value in the calling mehod then you shound return the incremented value from the method called and store it in a variable inside the calling method(main, in your case) as below:

y will have a value 6.

and your method definition becomes:



Hope this helps..

Cheers!!!
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pass by value mechanism applies even if the parameter is of reference type, only means different variables respectively store the same referenced object.
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi puneet chaturvedi,

you have used pass by value so the change to variable x will not effect to variable x in main method.

Regards,
James
reply
    Bookmark Topic Watch Topic
  • New Topic