• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hows is string value being returned for print?  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return value in m1 method  used to cause execution to branch back to the caller of the method. That's why you will get 10
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to return the String value, you can write like this

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

John,

Thanks for the reply, my query was how is string value being printed along with the integer return value. The code in the topic when executed gives my Java and 10 as output. My query is how Java is being displayed which is string value and am storing the returned int value in int variable "i".

Thanks
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure is this what you mean ?



If you want to return String, the caller must have String datatype.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:. . .
It is much quicker to write
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john and Ritchie for the reply.

The code sample that i have posted is returning string value apart from the integer value. My question is why is the string value being returned here along with integer which is being stored in int i.

Thanks
 
John Joe
Ranch Hand
Posts: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:My question is why is the string value being returned here along with integer which is being stored in int i.


Simple, because you have this line in m1 method

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:The code sample that i have posted is returning string value apart from the integer value. My question is why is the string value being returned here along with integer which is being stored in int i.

I think the confusion is around your saying that the code is returning a String value.  It's not; it printing a String value to the screen.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 5 prints "Java", line 14 prints 10. Were you expecting something else?
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Line 5 prints "Java", line 14 prints 10. Were you expecting something else?


Hi

Lacar,

i was expecting an error to be thrown(correct me if am wrong!). in the given code sample prints java on the console screen and then returns integer value 10. Now when i am typing in this line in the main function, am storing the returned integer value and then printing it . Though i know that the sop in the
prints the string value "JAVA". Please do correct me if my understanding is wrong.

Thanks

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
krishnadhar Mellacheruvu wrote:The code sample that i have posted is returning string value apart from the integer value. My question is why is the string value being returned here along with integer which is being stored in int i.

I think the confusion is around your saying that the code is returning a String value.  It's not; it printing a String value to the screen.


Hi

Snortum yes your right the sop function is printing it. But am executing the function via printing the returned value of the int main function which is 10. This is where my doubt was.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing a String has nothing to do with returning the int value. Your method is declared to return an int value so when you say return 10, that does exactly what it's supposed to do.  Like Knute was saying, you're mistaken in conflating the System.out.println("Java") statement with returning an int value. They are independent actions.

And please, don't use acronyms like "sop".  Your posts are not clear when you do that, especially since you use lowercase letters because it becomes even less apparent that "sop" is an acronym.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:. . . Snortum yes your right the sop function is printing it. . . .
Also, System.out.println isn't a function. A function would always return the same result for the same input. Since the print() methods have void return type, they don't produce any results.

Ths may appear petty, but the jargon is there for a reason., and shou‍ld be used correctly.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for making me understand and Ritchie thanks for the pointer.....
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our pleasure to help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!