• 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

print vs println

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain the difference between print vs println? I know that println prints to a new line, but on the code below, when I use System.out.println(i);, the loop does not print 2,4 and 6 ( prints from 8 to 100) where System.out.print(i); does this from 2. Can anybody tell me what is exactly going on here?

Thank you.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be no difference except of new lines being added.
Maybe the whole output exceeds limits of your console?
 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both ways it works fine for me.....(starting from 2)

Paweł Baczyński --- might be correct with exceeding the console output limit.
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried again and again. Same thing happens all the time. (I do not change anything else)
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you run this? What OS?
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if it was console limit then it shouldn't prit the last numbers, am I wrong?
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hans Baba wrote:if it was console limit then it shouldn't prit the last numbers, am I wrong?


Yes, you are. In case of exceeding the limit all consoles I know remove the oldest lines.
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using BlueJ IDE on MAC
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to eliminate any console buffer issues, turn your upper loop boundary down from 100 to something smaller, say 20.

That will allow you to observe the difference in behaviour between print() and println().
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah I used a smaller number and what you guys said is true. (Just found out line-limit on my consol is 47 )

Well, this is what being is a beginner. You will hear lots of silly questions like this from me
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hans Baba wrote:Well, this is what being is a beginner. You will hear lots of silly questions like this from me


There are no silly questions. Silly answers, perhaps...

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hans Baba wrote:yeah I used a smaller number and what you guys said is true. (Just found out line-limit on my consol is 47 )

Well, this is what being is a beginner. You will hear lots of silly questions like this from me


I would say this question has come up at least once a year since I joined the Ranch. It's not silly at all...it's really just one of experience. Things you never think of until they happen to you. The trick is to not ask this question yourself a SECOND time.
;-)
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just FYI, if you're using CMD on Windows you can increase the buffer by right-clicking on the console window's title bar, selecting Properties from the right-click menu and then on the Layout tab increase the value of the Height property for Screen Buffer Size.
For instance, by default my CMD window is set to a buffer width of 180 and a buffer height of 9999, while the window size is set to width 180 and height 80.
 
Greenhorn
Posts: 18
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method "print" leaves the insertion point after the last character of
the value of an expression, while the method "println" positions the insertion point at
the beginning of the next line
 
reply
    Bookmark Topic Watch Topic
  • New Topic