Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why use printdata instead of System.out.println?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if my questions are a little dumb. I'm young and I'm still learning.
I have been asking around and I dont understand the printData method? What does it really do? How is it different from System.out.println? Also what is 'return;' for? As in why do I need it?
Sorry if there are too many questions.
All help is greatly appreciated <3
 
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What class is the printData method in?  I do not see it in the Java SE API doc.
 
Taylor Quinn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PrintWriter? im not entirely sure tho
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you copy the import statements for the java program that uses the printData method and paste it here?  They will show what classes the code uses.
Also what version of java are you using?

Are you sure the printData method is not from some third party or user's program?
 
Bartender
Posts: 2782
135
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that "printdata" is a method and not a variable name?
Do you see code like this ?
 
Taylor Quinn
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code shows the printData method is in the Student class.  Your question is: why did the author write that method instead of just using the print method.  It looks like the method prints out the contents of 3 variables in the Student class object.
Since some of the variables are private the code to print them needs to be in the Student class.  Often writers of a class will add a toString method to a class that will return the Strings that are printed in the three print statements that code outside of the class can  use to display the Student class's variables.
 
salvin francis
Bartender
Posts: 2782
135
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Questions now make sense after you have posted the code above. Answering your second question:

Taylor Quinn wrote:... Also what is 'return;' for? As in why do I need it?


You don't need it in this case, the code should run fine even if you omit it. Are you aware about how the return keyword works ?
 
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Writing a printData() method like that isn't really in the Java® idiom. The Java® idiom is to override Object#toString(). Because the Object class implements such a method, every object ever seen has an implementation of it. Unfortunately the Object version is too basic for most use, so you have to override the method:-Alternative version, but you might not be familiar with the % tags:-Now you can write the following anywhere:-Note the warning about subsequent changes: you don't want users thinking that format is fixed for ever. If you return those three data from toString(), be sure to provide getXXX() methods for all three, too.
I prefer not to end what I return from toString() with a line end.

Your line 25 is incorrect. Use a getXXX() method instead, and give all fields private access. Don't write a no‑arguments constructor. If you have three data, use a three‑argument constructor.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic