Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 4th, 2006, 04:33 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ousys Send a message via Yahoo to ousys
Default java's argument is passed by value or by reference

i am puzzled , somebody said java only exits one passing argument rules, that is passed by value , what does that mean?




public void fn1(String a){
  a="aaaa"
 }
 public static void main(String []args){
  String b="bbb";
  fun1(b);
  System.out.println(b);
 }
in the screen , it will show "bbb" ,why?
if passed by reference ,i think it is modified , so it should print out "aaaa"

by the way, is there a way we can get the integer value in the method whose return type is boolean?

Thank you.
Reply With Quote
  #2 (permalink)  
Old June 10th, 2006, 01:18 AM
Registered User
 
Join Date: Jun 2006
Location: Bhiwani, Haryana, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default


I think this code will not work

first changed function name that is fn1() and fun1()
well it may be mistake.

second if u will call this function in main() u must make
it static as main() is static and can call only static methods.

u r prblems solution ....
In java objects are passed as, by reference so
make a class with construcure and declare object in main() of
that class and then pass object as parameter in delared class .




Reply With Quote
  #3 (permalink)  
Old June 26th, 2006, 09:16 AM
Authorized User
 
Join Date: Jun 2006
Location: BANGALORE, Karnatak, India.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default

Code:
class PassByValEx
{
    int i;
}
class Test
{
    static void fn(PassByValEx obj)
    {
        obj.i = 10;
    }

    public static void main(String a[])
    {
        int b = 20;
        fn(b);
        System.out.println(b);
    }
}
The above code will display 10. Datatypes like String, int, char are passsed by value. The class we create are passed by reference.

Ajit
Reply With Quote
  #4 (permalink)  
Old June 28th, 2006, 06:18 AM
arp arp is offline
Authorized User
 
Join Date: Jun 2003
Location: Mumbai, Maharashtra, India.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In Java primitive datatypes(byte, char, int etc..) are Passed By Value whereas objects are Passed By Reference.

Regards,
arp

Reply With Quote
  #5 (permalink)  
Old July 1st, 2006, 08:33 PM
Friend of Wrox
 
Join Date: Jan 2006
Location: San Francisco, CA, USA.
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Arp: Wrong. All parameters are passed by value. It's just that Objects (non-primitives) are passed as pointers, but the pointer is passed as a value. No function can change the value that's passed. That's why in the String example originally proposed, the output is still 'bbb'. The pointer that is "String b" still points to 'bbb' in memory, as is consistent with passing by value. If pass by reference WAS possible (as in Pascal, and with C by using the & character), then the String example would output 'aaaa'.

Jon Emerson
http://www.jonemerson.net/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sessions not being passed Chapter 2 akaplan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 6 June 27th, 2007 03:28 PM
Objects are always passed by reference or value? deb_kareng C# 2 November 2nd, 2006 03:53 AM
Value in Querystring not being passed u_heet Classic ASP Professional 2 September 16th, 2004 07:15 AM
All My Checkboxes are passed as "on", whether or n WebDevel Javascript How-To 1 December 9th, 2003 10:40 AM



All times are GMT -4. The time now is 01:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.