+ 15

Java - Simplify System.out.print

G'afternoon, I posted this to someone else as a response, and figured that this may be useful to everyone that uses Java. Ask any questions if you need clarity. Hope it helps and feel free to add in any of your own tricks that you use for Java! What to expect: - Static import of System.out - Custom print() method - Custom println() method https://code.sololearn.com/cJD7V66TF3k7/#java // This import allows us to use out.println() // instead of having to use System.out.println() import static java.lang.System.out; public class Program { // custom function to simplify System.out.print public static void print(String str) { System.out.print(str); } // custom function to simplify System.out.println public static void println(String str) { System.out.println(str); } public static void main(String[] args) { // EXAMPLE OF PRINT FUNCTION print(":::PRINT FUNCTION EXAMPLE:::"); print("This "); print("Doesnt "); print("Print NewLine\n"); // EXAMPLE OF PRINTLN FUNCTION println(""); println(":::PRINTLN FUNCTION EXAMPLE:::"); println("This"); println("Prints"); println("With New Line"); // EXAMPLE OF SYSTEM.OUT IMPORT out.println(""); out.println(":::SYSTEM.OUT IMPORT EXAMPLE:::"); out.print("No New Line"); out.println(""); out.println("This"); out.println("Has"); out.println("NewLine"); } } Note: You can name your functions any valid name, so you could make the name even smaller, such as p() and pln(), or make it anything else you want. It's always a good rule of thumb to name things in a way that relates to its purpose, but if you don't have thumbs, do whatever you want. :D

17th Nov 2017, 11:30 PM
AgentSmith
8 Respuestas
+ 9
Yeah. I hate writing System.out.print() every time I wanna print something. I just use the static import and it makes stuff alot easier.
18th Nov 2017, 2:58 AM
qwerty
qwerty - avatar
+ 7
System.out.println("is way too long!"); i always create a static void print method if i need it makes things alot quicker.
17th Nov 2017, 10:24 PM
D_Stark
D_Stark - avatar
+ 6
Exactly. It didn't take me long using Java to hate typing that out each time. Of course, Sololearn doesn't have the live templates, so it's good here. And as you said Boris, some editors help out with shortening it, which is awesome. This same concept applies to other languages, so even with something like C# where you type out Console.WriteLine, you can make it simple. Another neat thing about it that I didn't mention, but just assumed people would realize, you can name it anything you want to name it. So you could even make it as simple as p("w00t w00t"); for your printing, though I believe one should use something that at least describes its function to some degree.
17th Nov 2017, 11:37 PM
AgentSmith
+ 6
@Rahul That's in netbeans not in Code Playground.
20th Nov 2017, 4:52 AM
qwerty
qwerty - avatar
+ 4
It is useful, especially in editors or development environments without live templates (like 'sout' in InteliJ IDEA).
17th Nov 2017, 11:15 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
ou yeah negger
18th Nov 2017, 3:00 AM
Ayrton Mauricio Vega Vera
Ayrton Mauricio Vega Vera - avatar
+ 1
i used to type sout and tab key in netbeans editor and it automatically convert System.out.println for me.
18th Nov 2017, 5:43 PM
Rahul Meshram
Rahul Meshram - avatar