Results 1 to 9 of 9
  1. #1
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Question How to write it in Java

    Here I have simple code in C++:

    Java Code:
    include <iostream>
    using namespace std;
    
    //method that just changes numbers;
    int change(int &initialNumber, int finalNumber){
    initialNumber=finalNumber;
    }
    
    int main()
    {
    int anyNumber=5;
    change(5, 10);
    //now anyNumber refers to 10 not to 5;
    cout<<anyNumber;//output will be 10
    }
    }

    The problem is How Can I write this in Java:
    Java Code:
    public class Test{
    private static int change(int initialNumber, int finalNumber){
    return initialNumber=finalNumber;
    }
    public static void main(String [] args){
    int anyNumber=5;
    change(5, 10);
    //anyNumber till refers to 5; How to make it refer to the new value
    System.out.println(anyNumber);//output is 5
    } 
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You cannot do this in Java. Objects are passed by reference, primitives are not. An int is a primitive, therefore the values of 5 and 10 are passed to the function, not the object anyNumber and value 10.

    Further reading: Parameter passing in Java - by reference or by value?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Java uses the call by value parameters passing mechanism; always. What you did in your C++ program can't be done in Java. b.t.w. 5 can not be a reference and any decent C++ compiler will protest against your use.

    kind regards,

    Jos

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    Objects are passed by reference, primitives are not.
    Nonsense, objects are also passed by value and Java just uses pointers (references) to objects so those references are passed by value.

    kind regards,

    Jos

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Nonsense, objects are also passed by value and Java just uses pointers (references) to objects so those references are passed by value.

    kind regards,

    Jos
    Yes, the references are passed by value, but that's all that matters. The point is, you can treat it is a pointer, more or less. While you cannot set its value in the old function, calls and data setting methods can be made on it.

    Example:
    Java Code:
    	public static void test(someIntClass x, someIntClass y, int z) {
    		x = new someIntClass(5);
    		y.m_Int = 5;
    		z = 5;
    	}
    	public static void test2() {
    		someIntClass a = new someIntClass(4);
    		someIntClass b = new someIntClass(4);
    		int c = 4;
    		test(a,b,c);
    		System.out.printf("%d %d %d\n",a.m_Int,b.m_Int,c); // Prints 4 5 4
    	}
    	private static class someIntClass {
    		public int m_Int = 0;
    		someIntClass(int x) { m_Int = x; }
    	}
    Apologies for the messy code. Wrote it in 30 seconds... just call test2() first.

  6. #6
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    About C++ code, there is no problem when I compile it with BloodShed Dev-C++; And about Java:
    If there is situation where I have anyNumber=5;
    and I have methods:
    change(), anyMethod(), and etc... I want to pass anyNumber to these methods and finally want to return new anyNumber value, Whan I need to do? Or I have to initialize new variable each time after passing it to the method:
    anyNumber=5;
    int newAnyNumber=change(anyNumber);
    int newNewAnyNumber=anyMethdo(newAnyNumber);????

  7. #7
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    About C++ code, there is no problem when I compile it with BloodShed Dev-C++; And about Java:
    If there is situation where I have anyNumber=5;
    and I have methods:
    change(), anyMethod(), and etc... I want to pass anyNumber to these methods and finally want to return new anyNumber value, What I need to do? Or I have to initialize new variable each time after passing it to the method??:
    anyNumber=5;
    int newAnyNumber=change(anyNumber);
    .............//any code......
    int newNewAnyNumber=anyMethod(newAnyNumber);????

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by agajantorayev View Post
    I want to pass anyNumber to these methods and finally want to return new anyNumber value, Whan I need to do? Or I have to initialize new variable each time after passing it to the method:
    anyNumber=5;
    int newAnyNumber=change(anyNumber);
    int newNewAnyNumber=anyMethdo(newAnyNumber);????
    Java Code:
    public int F(int x) { return x + 1; }
    public void Test() {
        int anyNumber = 5; // anyNumber == 5
        anyNumber = F(anyNumber); // anyNumber == 6
        anyNumber = F(anyNumber); // etc...
        anyNumber = F(anyNumber);
        anyNumber = F(anyNumber);
        anyNumber = F(anyNumber); // anyNumber == 10
    }
    All you have to do is assign it to the return value, just as you would in C++.

    PS: There is an edit button, no need to repost your post to add a line to it. ;)

  9. #9
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Thanks a Lot!! I have just started Learnig Java programming language!! Thanks Guys for your Help!

Similar Threads

  1. how to write except in java??
    By napi1234 in forum New To Java
    Replies: 1
    Last Post: 08-09-2010, 04:49 AM
  2. Replies: 57
    Last Post: 07-15-2010, 04:10 AM
  3. how to write a messenger with using java?
    By kira4 in forum New To Java
    Replies: 2
    Last Post: 07-30-2008, 08:15 PM
  4. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 01:28 PM
  5. How do i write this in java...?
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •