| Author |
How to store method return type in primitive
|
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
Q1. How to Use return type of methods?
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
The methods return some value- How do you assign a value to a variable?-
|
Mohamed Sanaulla | My Blog
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
Mohamed Sanaulla wrote:The methods return some value- How do you assign a value to a variable?-
The code I have commented in that way I want to assign return value of the method in a variable.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
Somprakash Rai wrote:The code I have commented in that way I want to assign return value of the method in a variable.
You cannot do that way- Its not a legal way to invoke the methods. Did you look at what error the compiler reported?
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
Mohamed Sanaulla wrote: ^
Test13.java:7: ')' expected
int d=a1.m1(int a,int b);
^
2 errors
It means we assign only simple method return values to primitive.and We can can not assign argumented method value to primitive?
Somprakash Rai wrote:The code I have commented in that way I want to assign return value of the method in a variable.
You cannot do that way- Its not a legal way to invoke the methods. Did you look at what error the compiler reported?
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3862
|
|
|
It's nothing to do with primitives. It's just to do with how you can call methods.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
|
You cannot declare the arguments in the method call- You need to pass either variables which are initialized to some value or some value itself.
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
|
Thanks Now am clear with this topic...
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Somprakash Rai wrote:
Mohamed Sanaulla wrote:
Somprakash, do you see a big difference between the way you call the method and the way Mohamed calls the method?
Edit: waaaayyyy to late...
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
Rob Spoor wrote:
Somprakash Rai wrote:
Mohamed Sanaulla wrote:Yeah Sure but it can called this way also
int i=m1(10,20);
Somprakash, do you see a big difference between the way you call the method and the way Mohamed calls the method?
Edit: waaaayyyy to late...
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
|
Anyone please tell that where this return value is being stored?
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
You might want to carefully use the "Quote" tag? Confusing to find out your reply from the text in the above post. But I think it should be this:
Somprakash Rai wrote:
Yeah Sure but it can called this way also
int i=m1(10,20);
Yes it can be called. Please try to experiment with the code and the compiler will provide you with the required error messages.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2946
|
|
Somprakash Rai wrote:Anyone please tell that where this return value is being stored?
What is the confusion? Can you show us the code you are trying out?
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
|
I have been taught somewhere that what ever return type we are using in side a method that return is being send to OS. But As I seen that return type can stored in primitive and that can used.At this point I wanted to know that the return type we are assign to primitive variable where that value is being stored in method or...........
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
When you call a method and you assign the result of the method to a variable, then the return value of the method is stored in the variable. It doesn't matter if the method returns a primitive value or a reference (to an object) - it works the same way. If you don't assign the return value of a method to a variable, then the value just disappears - it's not stored anywhere, it's just thrown away.
Nothing is being sent to the operating system. The operating system doesn't have anything to do with the return values of methods directly.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 101
|
|
Thanks ...
Jesper de Jong wrote:When you call a method and you assign the result of the method to a variable, then the return value of the method is stored in the variable. It doesn't matter if the method returns a primitive value or a reference (to an object) - it works the same way. If you don't assign the return value of a method to a variable, then the value just disappears - it's not stored anywhere, it's just thrown away.
Nothing is being sent to the operating system. The operating system doesn't have anything to do with the return values of methods directly.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Jesper de Jong wrote:Nothing is being sent to the operating system. The operating system doesn't have anything to do with the return values of methods directly.
True. In C and C++ the return value of the main function was sent to the OS, but in Java that must be done using System.exit(int).
|
 |
 |
|
|
subject: How to store method return type in primitive
|
|
|