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 6939243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:35:51+00:00 2026-05-27T12:35:51+00:00

New to Java here, please help. How arguments are passed in java? Why am

  • 0

New to Java here, please help. How arguments are passed in java? Why am I unable to change argument value in the calling method from within called method?

Code

public class PassTest {
    public static void changeInt(int value)
    {
         value=55;
    }

    int val;
    val=11;
    changeInt(val);
    System.out.println("Int value is:" + val);// calling modifier changeInt 
}

Output

Int value is: 11

why it is not 55..?

  • 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-27T12:35:52+00:00Added an answer on May 27, 2026 at 12:35 pm

    Java passes ByValue, meaning the value of the object you put as a parameter is passed, but not the object itself, therefore

    val=11;
    changeInt(val);
    

    does the exact same thing as

    int val=11;
    int val2=val
    changeInt(val2);
    

    int is a primitive, primitives don’t “wrap” a value, you could try to use an Integer class, or make your own class that stores an integer, and then change that classes integer value. Instances of an object are sometimes passed ByReference if setup right. here is an example

    MyStringClass.java

    public class MyStringClass{
    
        private String string = null;
    
        public MyStringClass(String s){
            string = s;
        }
        public String getValue(){
            return string;
        }
        public void setValue(String s){
            string = s;
        }
    }
    

    and then the workings

    public static void addTo(String s){
        s += " world";
    }
    public static void addTo(MyStringClass s){
        s.setValue(s.getValue() + " world");
    }
    public static void main(String[] args){
        String s = "hello";
        MyStringClass s1 = new MyStringClass("hello");
        addTo(s);
        addTo(s1);
        System.out.println(s);//hello
        System.out.println(s1);//hello world
    }
    

    I would wonder why you need to change the value instead of just returning it? isn’t it easier?

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

Sidebar

Related Questions

I am new to java regex.Please help me. Consider the below paragraph, Paragraph :
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
I am currently calling the following line of code: java.net.URL connection_url = new java.net.URL(http://<ip
Please help me understand the following: I create a CharBuffer using CharBuffer.wrapped(new char[12], 2,
here is full code from algorithm and data structure in java written in c++
Please help me. When i try to create new row in database i receive
This is driving me crazy. Please help. I am new to Spring and I
Can you please advise what is wrong here. This is how my method in
I am new to java.util.concurrent package and wrote a simple method which fetches some
Please help with my assignment. Here is the question: Create a separate test driver

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.