• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why metod print before invocation?

 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why metod print before invocation?
i mean why result is:
one
1010


not:
10
one
10

or:
1010
on.
I think that expression performed from left to right. What is rule for this behavior?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to understand what is meant by a static block and the sequence in which the runtime looks at your code!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:You have to understand what is meant by a static block and the sequence in which the runtime looks at your code!

Why ? There is no static block in the code.

Sergej Smoljanov wrote:I think that expression performed from left to right.

Yes. And any expressions in a method call parameter list are evaluated before the method is called.
So what you actually have is the equivalent of
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it as logically the parts of the string have to be assembled into the complete string before the string can be printed. The m function prints "one" before it returns the value 10, before the string to print is fully assembled. It doesn't print the string one part at a time. This will always hold true.

 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sergej and all renchers ,
When i look your code i also fuss in many problems , below are modified codes from your code .

All members please tell me also where am i going wrong in understanding following compiler output ?



The above code is as same as Sergej code just wanted to see how it behaves.

But in following code I am getting error in System.out.println(m()+" "+m1()); on method m1() saying void type not allowed in SOP why is it so.? please tell me where i am doing mistake .??

 
Sergej Smoljanov
Ranch Hand
Posts: 468
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can`t ever use void method in print/println, also you can`t use it in expression where value is expected:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic