Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store method return type in primitive

 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Q1. How to Use return type of methods?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods return some value- How do you assign a value to a variable?-
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's nothing to do with primitives. It's just to do with how you can call methods.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Now am clear with this topic...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone please tell that where this return value is being stored?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic