println vs System.out.println in Scala

后端 未结 1 572
我在风中等你
我在风中等你 2020-12-29 18:26

I always thought that Predef.println was merely a shortcut for System.out.println, but apparently I am mistaken, since it doesn\'t seem to use

相关标签:
1条回答
  • 2020-12-29 18:53

    Predef.println is shortcut for Console.println and you can use Console.setOut or Console.withOut for redirecting.

    Also, Console.setOut only affects the current thread while System.setOut affects the whole JVM. Additionally Scala 2.9 repl evaluates each line in its own thread, so Console.setOut is not usable there.

    scala> val baos = new java.io.ByteArrayOutputStream
    baos: java.io.ByteArrayOutputStream = 
    
    scala> Console.withOut(baos)(print("hello"))
    
    scala> println(baos)
    hello
    
    0 讨论(0)
提交回复
热议问题