Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:11:16+00:00 2026-05-29T07:11:16+00:00

I am reading about Pass by Value and Pass by reference in java, I

  • 0

I am reading about Pass by Value and Pass by reference in java, I got many articles,some of them saying Java is following only ‘Pass by value ” and some of them saying some difference between primitive and object. so I wrote following sample code.
and putting output also.
Please comment and share what is exactly the answer is.

I checked for Int, String , StringBuffer and Employee class, now Its working as pass by reference for Employee class only.

package test;
class Emp {
    public String name="";
    public int age=0;

    public Emp(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return "Name: "+ this.name + "....Age: "+ this.age;

    }
}
public class Class1 {
    public Class1() {
        super();
    }

    public void doChange(int i) {
        i = i +10;
        System.out.println("Value of Integer in Method:>"+ i);
    }

    public void doChange(Emp i) {
        i.age=29;
        i.name="rishu";
        System.out.println("Value of Employee In Method "+i.toString());
    }

    public void doChange(String i) {
        i = i + " Hello";
        System.out.println("value of i->"+ i);
    }


    public static void main(String[] args) {
        int i =10;
        String str="XXX";
        Class1 c= new Class1();
        StringBuffer sb= new StringBuffer();
        Emp e= new Emp("abhi",28);       

        sb.append("ABC ");
        System.out.println("");
        System.out.println("Value of Integer before Method:->"+ i);
        c.doChange(i);
        System.out.println("Value of Integer after Method:->"+ i);
        System.out.println("");
        System.out.println("Value of String before Method:->"+ str);
        c.doChange(str);
        System.out.println("Value of Integer after Method:->"+ str);
        System.out.println("");
        System.out.println("Value of StringBuffer before Method:->"+ sb);
        c.doChange(sb.toString());
        System.out.println("Value of StringBuffer after Method:->"+ sb);
        System.out.println("");

        System.out.println("Value of Employee before Method:->"+ e.toString());
        c.doChange(e);
        System.out.println("Value of Employee after Method:->"+ e.toString());
    }
}

Output:

Value of Integer before Method:->10
Value of Integer in Method:>20
Value of Integer after Method:->10

Value of String before Method:->XXX
value of i->XXX Hello
Value of Integer after Method:->XXX

Value of StringBuffer before Method:->ABC 
value of i->ABC  Hello
Value of StringBuffer after Method:->ABC 

Value of Employee before Method:->Name: abhi....Age: 28
Value of Employee In Method Name: rishu....Age: 29
Value of Employee after Method:->Name: rishu....Age: 29
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:11:17+00:00Added an answer on May 29, 2026 at 7:11 am

    Java is pass-by-value only.

    However, for objects, what is passed by value is a reference to the object. No, that’s not the same as pass-by-reference. The difference is this:

    If in you do this:

    public void doChange(Emp i) {
        i = new Emp("changed!", 42);
    }
    

    It has absolutely no effect outside the method – because the reference i is a copy of the reference used outside the method. However, it refers to the same object, so if you use the reference to change fields of the object, these changes are visible outside the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i was reading about how you can only pass by value in java and
I was reading some things about exception handling in Java, to be able to
I've been reading about SQL injection attacks and how to avoid them, although I
I've been reading about how great difference lists are and I was hoping to
After reading this article about validating with service layer I have some doubts. First:
When reading a question about testing a Java program several times with random seeds
I am reading about MapReduce and the following thing is confusing me. Suppose we
Reading about the G.729 codec , I found this interesting tidbit about Comfort Noise
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.