• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A classic java problem. Why?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Why the result is House 1 2 but 1 House 2???
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because

System.out.print("House ");


is evaluated before

System.out.println(b.i + " " + b.getI());



b.getI() makes a call and prints the statement "House..", at this point of time, this line

System.out.println(b.i + " " + b.getI());

is still waiting a return from getI() method.

Once it gets return it will print out the whole line as "1 2".

Now the doubt that I have is why is output not

House
1 2


since there is a println and not print in

System.out.println(b.i + " " + b.getI());



Thanks
Chintan
 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why

System.out.print("House ")

should be evaluated before

System.out.println(b.i + " " + b.getI())

? Why doesn't it be executed when it gets called by b.getI() ?

The output for the code is . Because if what you said is right, the statement for printing "House" is print not println, why it should have a newline?
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my understanding for execution of steps

System.out.println(b.i+" "+b.getI());

1.Output to a new Line(so cursor goes to new line). At this point of time, the entire expression within println() needs to be executed.
However, the expression is dependent on another function call, so before it executes the entire expression, it has to wait for return of "b.getI()".

2.When b.getI() is called, the first statement in that function performs a print. So, cursor is already on a new line(from point1) and hence it prints "House " and returns 2.

3. So, now expression inside println() is complete and is "1 2" and from step 2, we already have "House ", hence the output "House 1 2".

My 2 cents.

Thanks
Chintan.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.Output to a new Line(so cursor goes to new line). At this point of time, the entire expression within println() needs to be executed.
However, the expression is dependent on another function call, so before it executes the entire expression, it has to wait for return of "b.getI()".


Correct me if I'm wrong, but the cursor won't go to the new line in the beginning. Try inserting before


2.When b.getI() is called, the first statement in that function performs a print. So, cursor is already on a new line(from point1) and hence it prints "House " and returns 2.

Cursor is still at previous location and prints "House " from that point, followed by the printing of 1 2 and newline

3. So, now expression inside println() is complete and is "1 2" and from step 2, we already have "House ", hence the output "House 1 2".

println() is complete.

HTH

_charles

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
excellent question....thanks Zhang for sharing this question with us....
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Step 1: b.getI() will be evaluated first (methods in expressions are
evaluated first)
Step 2: System.out.print("ClassA "); will be executed and it will
send the output to console
Step 3: 2 will be returned
Step 4: b.i + " "+ b.getI() will be printed (getI() statement already
has executed the System.out.print("ClassA "); now it has return value
only)

Underlying concept is here is to understand evaluation order of methods when used in expressions.

~ Abhay
 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhay Agarwal wrote:

Step 1: b.getI() will be evaluated first (methods in expressions are
evaluated first)
Step 2: System.out.print("ClassA "); will be executed and it will
send the output to console
Step 3: 2 will be returned
Step 4: b.i + " "+ b.getI() will be printed (getI() statement already
has executed the System.out.print("ClassA "); now it has return value
only)

Underlying concept is here is to understand evaluation order of methods when used in expressions.

~ Abhay



I see...that makes sense! Thanks!
 
Chintan B Shah
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so, newline happens at last....new thing learnt....Thanks Charles.

Regards,
Chintan.
 
reply
    Bookmark Topic Watch Topic
  • New Topic