Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Call by Value

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question relates to java basics, im currently studyiung towards my SCJP cert. This is about a passage ive read in a book that doesnt make sense. Its not about the actual cert so ive put it here in the begginer section. The passage states:


If the argument passed into the method is a primitve type, it is impossible in Java for the method to alter the value of the original primitive (SCJP Study Guide, Richard F Raposa, page37, passing primitives vs passing references).

The section is on Call by Value.

My question is, doesnt the code i have given below make this passage incorrect?

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In what way do you think that your example makes the statement about how Java passes primitive types incorrect?
 
David Houghton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you comile and run the code, the following print statment show that the x value has changed from 0 to 5 due toi being changed by the method



 
David Houghton
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the netbeans output

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at it this way. if you had int n=5; in your main method, and you passed n instead of 5, e.g. m.setX(n);

then no matter what happens in the method setX, n would still be equal to 5 in your main method. That is what is meant by "the method can't change the the value of the original primitive"
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are not changing the value of the passed parameter. you change the value of a class variable. Try changing your code to something like this:



Here, I pass temp into a method, and change it's value. But back in main, themp says as 5 (assuming i wrote this correctly - I didn't test or verify it even compiles).
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic