aspose file tools*
The moose likes Java in General and the fly likes How to store method return type in primitive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to store method return type in primitive " Watch "How to store method return type in primitive " New topic
Author

How to store method return type in primitive

Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106



Q1. How to Use return type of methods?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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: 106

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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: 106

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: 4490
    
    8

It's nothing to do with primitives. It's just to do with how you can call methods.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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: 106

Thanks Now am clear with this topic...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106

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: 106

Anyone please tell that where this return value is being stored?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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: 106

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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 8 API documentation
Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106

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: 19783
    
  20

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store method return type in primitive