473,425 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Pass by Reference in java

107 100+
Hai All,

I have to set of Programs, Both deals with Object, One is with Predefined Object and another one is User Defined Object.

Program #1

Expand|Select|Wrap|Line Numbers
  1. class MyPass
  2. {
  3.     Integer i=new Integer(100);
  4.     public void pass(){
  5.  
  6.         System.out.println(i);
  7.         go(i);
  8.         System.out.println(i);
  9.     }
  10.     public void go(Integer x){
  11.         System.out.println(x);
  12.         x=200;
  13.         System.out.println(x);
  14.     }
  15.     public static void main(String arg[]){
  16.         MyPass mp=new MyPass();
  17.         mp.pass();
  18.     }
  19. }
  20.  
  21. OUTPUT:
  22.  
  23. 100
  24. 100
  25. 200
  26. 100
  27.  
  28.  

Program #2

Expand|Select|Wrap|Line Numbers
  1. class Refer
  2. {
  3.     String name;
  4. }
  5. class PassByReference
  6. {
  7.     void calling()
  8.     {
  9.         Refer r=new Refer();
  10.         r.name="abc";
  11.         System.out.println("Before"+r.name);
  12.         called(r);
  13.         System.out.println("After"+r.name);
  14.     }
  15.     void called(Refer s)
  16.     {
  17.         s.name="xyz";
  18.         System.out.println("Inside"+s.name);
  19.     }
  20.     public static void main(String args[])
  21.     {
  22.         PassByReference p=new PassByReference();
  23.         p.calling();
  24.     }
  25. }
  26.  
  27. OUTPUT:
  28.  
  29. Beforeabc
  30. Insidexyz
  31. Afterxyz
  32.  
  33.  
In Prg #1 object value doesnt changed
In Prg #2 Change the value
Jun 24 '10 #1
4 2982
jkmyoung
2,057 Expert 2GB
Sorry, what exactly is the question?
Jun 24 '10 #2
rajujrk
107 100+
@jkmyoung
My Question is, Both Programs Pass the reference, but in first program the value doesn't changed, in second one it changed, what happening inside in this programs? how these two code executes?
Jun 25 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. System.out.println("1" +i);
  2. go(i);
  3. System.out.println("4" +i);
  4.  
i = 100.
Prints 100
Goes to go function
Prints 100
Makes x = 200
Prints 200
Comes out of loop
Prints 100

200 value of x is available inside the function go. Not in the function who is calling it.

Regards
Dheeraj Joshi
Jun 25 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
In second example, You are passing the object to the function whose scope is at class level.

Regards
Dheeraj Joshi
Jun 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
4
by: Omar Llanos | last post by:
Recently, I posted a question on how to invoke a textbox control in Form1 (parent form) from within Form2 (which is inherited from Form1). Someone suggested to pass a reference of the Form1 to the...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
3
by: John E. | last post by:
I am trying to find a way to not have to reference an object in all my projects, since it is initialized & instantiated in my Common class. I have a 4 tier project (presentation, rules, dal,...
9
by: Edward Diener | last post by:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference to a reference in C#. I know one can use it on a value type to create a reference to that value.
9
by: misdst23 | last post by:
Hi, I know I can call a static java method within javascript by using the <% ... %tags. But how can pass a javascript variable ? function thefunction() { var = javascriptvariable ;
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
8
by: getmeidea | last post by:
Hi, I am using JDK 1.5. I have a program like this. Here i am directly assigning value to one object. It does'nt give me any compile time or run time error. In java we dont have access to any...
15
by: J. Peng | last post by:
Hello, I saw this statement in Core Python Programming book, All arguments of function calls are made by reference, meaning that any changes to these parameters within the function affect the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.