Discussion on Java and some tips

I have found that the BufferedReader is faster than the Scanner at taking inputs. My program was getting TLE with Scanner and as soon as I switched to BufferedReader, it ran perfectly well. Same goes with PrintWriter. My program which was getting TLE with System.out.print ran perfectly when I used PrintWriter instead. Will this be true for all programs? Can I save execution time by using BufferedReader and PrintWriter?

I have seen many programmers who import library classes one by one instead of the whole. I mean they write,“import java.io.BufferedReader; import java.io.PrintWriter;”. I always write,“import java.io.*;”. This imports the entire IO library. Does importing only the required libraries save time/memory or both?

What are some other ways I can reduce the execution and memory required in Java?
If you guys know some quick tips or techniques in Java that are helpful, please write them for every Java programmer.

I’ll start by saying Arrays.sort() is the best way to sort an array(even Strings).

Thanks a lot :slight_smile:

I’ve found that using ArrayList in Java is more efficient than using an Array most of the time because you can add() how many ever elements you want to it without having to specify the number of elements during declaration. For ArrayList, you can use Collections.sort() for sorting.

Is it true that BufferedReader and PrintWriter are faster than Scanner and System.out.print()? If so, I would like to start using them from now on…