Results 1 to 9 of 9
Thread: How to write it in Java
- 08-11-2010, 07:13 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
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 } }
- 08-11-2010, 07:16 AM #2
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?
- 08-11-2010, 07:16 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 08-11-2010, 07:18 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
- 08-11-2010, 07:25 AM #5
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:
Apologies for the messy code. Wrote it in 30 seconds... just call test2() first.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; } }
- 08-11-2010, 07:30 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
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);????
- 08-11-2010, 07:32 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
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);????
- 08-11-2010, 07:32 AM #8
All you have to do is assign it to the return value, just as you would in C++.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 }
PS: There is an edit button, no need to repost your post to add a line to it. ;)
- 08-11-2010, 07:37 AM #9
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
how to write except in java??
By napi1234 in forum New To JavaReplies: 1Last Post: 08-09-2010, 04:49 AM -
New to java and does not know how to write the code ...
By poisson in forum New To JavaReplies: 57Last Post: 07-15-2010, 04:10 AM -
how to write a messenger with using java?
By kira4 in forum New To JavaReplies: 2Last Post: 07-30-2008, 08:15 PM -
need help to write Program in JAVA
By maneibr in forum New To JavaReplies: 1Last Post: 03-13-2008, 01:28 PM -
How do i write this in java...?
By johnny7white in forum New To JavaReplies: 1Last Post: 11-28-2007, 07:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks