Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2006, 04:33 AM
Registered User
 
Join Date: Apr 2006
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.
 
Old June 10th, 2006, 01:18 AM
Registered User
 
Join Date: Jun 2006
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 .




 
Old June 26th, 2006, 09:16 AM
Authorized User
 
Join Date: Jun 2006
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
 
Old June 28th, 2006, 06:18 AM
arp arp is offline
Authorized User
 
Join Date: Jun 2003
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

 
Old July 1st, 2006, 08:33 PM
Friend of Wrox
 
Join Date: Jan 2006
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/





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





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