• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help in method return type

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I'm having difficulty in understanding..... if a method is defined with some kind of return type other than void then is it necessary to assign the called method which has returned something to it's type specified in the method....
for eg.... take this method

class A{
public int aMethod(){ int i; i++; return i;}
}

should this be called like this....

A a = new A();
int j = a.aMethod();--------------------------->1

or like this
a.aMethod();------------------------------------>2

or you can call it any way you want???
According to me no.1 is correct but i've found in most of the mock tests saying that 2 works either..... you can call it any way you want... I have seen the questions where in they have called no.1 and no.2 together and answer is something different from compilation error.... Can anyone please explain it to me...
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If a method has a return type, it is perfectly valid to ignore it. For example, say a method that does something and returns a status, shouldn't there be cases where you just want the something to happen (and at a best effort), and you don't care about the success status?

On the other hand, if the method doesn't return anything (void), then assigning it to something, or using it in an expression, should yield a compilation error.

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry,
Actually i was confused since this does not happen in c or c++ and i was comparing it with that.... but now it's clear! ! !
Thank you.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually i was confused since this does not happen in c or c++ and i was comparing it with that



What "does not happen in c or c++"?

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Actually i was confused since this does not happen in c or c++ and i was comparing it with that



What "does not happen in c or c++"?

Henry



i think ignoring the return type is not allowed in c or c++
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erum Momin wrote:
i think ignoring the return type is not allowed in c or c++



That's not true.... Heck, even printf() returns an int. It would be annoying to have to check the return type of calls to printf()...

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm.... you are rite....
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic