Why shouldn't I use System.out.println() in android

In this code style they said that we shouldn’t use System.out.println() but I don’t understand their’s idea. can Anyone explain about that? What should I use to trace the log of my app?

System.out.println() (or printf() for native code) should never be used. System.out and System.err get redirected to /dev/null, so your print statements will have no visible effects. However, all the string building that happens for these calls still gets executed.

  • Android - ListView - performItemClick
  • Google Play Services v23 Proguard configuration
  • ContentProvider insert() always runs on UI thread?
  • Distinguish between Tablet and Smart phone on ICS
  • FCM push notification showing white square icon instead of app icon when app is in Background
  • Using HttpURLConnection and HttpsURLConnection to connect to an https?
  • Related posts:

    RecyclerView.getChild(index) shows null when list is scrolled (index gets messed up)
    LocationManager calling onLocationChanged too often?
    Programmatically find shaky OR out-of-focus Images
    How To: Voice Commands into an android application
    Android XML error message
    ActionBar goes out of the screen when keyboard shows up
  • Android recylerview v.23.2.0 & design library v.23.2.0 are broken
  • How to make an intent with multiple actions
  • Selendroid as a web scraper
  • Android - Unable to get the gps location on the emulator
  • Does SAF(Storage Access Framework) solve the SD card WRITE issue in Android 4.4 (KitKat)?
  • Emulator Not Running on Android Studio
  • 7 Solutions collect form web for “Why shouldn't I use System.out.println() in android”

    It was my problem too. You should use class methods of Log.

    As android developer, these are the commonly used method of Logging utility

    1. Log.d() – Debug
    2. Log.e() – Error
    3. Log.i() – Information
    4. Log.w() – Warning
    5. Log.v() – Verbose
    6. Log.wtf() – What a Terrible Failure: Report an exception that should never happen.

    Methods above have both parameters :

    1. String , String – This is a basic parameter which is a TAG and Message.
    2. String, String, Throwable – Has similarity on first one. However, If you want to log an Exception or any Throwables in logcat, other than calling printStackTrace() use this.

    EDIT: Going straight to answer your question. println() of System.out and System.err will still displayed in logcat, but with limitations.

    • You can’t log VERBOSE, ERROR, or DEBUG using System.out or System.err.
    • You can’t define your own TAG, it will display System.err or System.out with your text. As equivalence

      • System.out.println("Hello!") is equivalent to Log.i("System.out","Hello!")
      • System.err.println("Hello!") is equivalent to Log.w("System.err","Hello!")

    System.out.println("") in android will not run well because there is no Terminal that the app is corrected to.

    You would be better off using Log.(d)(v)(e)(i)(w), because there is something actively monitoring LogCat.

    System.out.println() will print to LogCat, but only after an additional set of System instuctions, making it not as efficient, however, as i said, it still works.

    if we want to trace the android project
    we can do it using Log class
    there is some methods like
    Log.e(TAG,MESSAGE)
    Log.v(TAG,MESSAGE)
    Log.w(TAG,MESSAGE)
    Log.d(TAG,MESSAGE)
    Log.i(TAG,MESSAGE)
    its a static method of Utils package. put it line by line and u can watch it in the LogCat..
    thats at enjoy with android

    From your own link:

    System.out.println() (or printf() for native code) should never be
    used. System.out and System.err get redirected to /dev/null, so your
    print statements will have no visible effects. However, all the string
    building that happens for these calls still gets executed.

    In addition, at the beginning of that page, it says:

    The rules below are not guidelines or recommendations, but strict
    rules. Contributions to Android generally will not be accepted if they
    do not adhere to these rules.

    So DON’T do it!

    You can use the built in Log utility that will print right out to the LogCat.

    You can use Log.e(String, String) for errors which will appear in red. There is also v, d, i, and w for verbose, debug, info, and warning respectively.

    The following should do the trick to print the exception

    1. Log.d(“myapp”, Log.getStackTraceString(new Exception()));

    or

    2. You can get longer stack traces by digging deeper. For example:

    Log.getStackTraceString(e.getCause().getCause());

    Log is the best way to trace our android project
    like following code…
    it will help u…
    just look in DDMS logCat that how exactly project is build…
    requirement… android.utils.Log; package is used..

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    for(int i=0;i {
    Log.e("i = ",""+i);
    Log.v("i = ",""+i);
    Log.i("i = ",""+i);
    Log.w("i = ",""+i);
    Log.d("i = ",""+i);
    }
    }

    i hope it will help u

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.