Java printf() 和 println() 的区别是什么?

1.在学习Java7 concurrency cookbook 的第一章节时,发现《Interrupting a thread》这个部分的代码没有达到预期的效果:控制台并没有像书上所描述的那样输出内容。再把其使用的printf()函数换成println()之后;程序得到预期的效果。

2.代码如下:


package lee.twowater.java7.chapterThree;


public class PrimeGenerator extends Thread {


    @Override

    public void run() {

        long number = 1L;

        while (true) {

            if (isPrime(number)) {

                System.out.printf("Number "+ number +" is Prime");

            }

            

            if (isInterrupted()) {

                System.out.printf("The Prime Generator has been Interrupted");

                return;

            }

            number++;

        }

    }

    

    private boolean isPrime(long number) {

        if (number <= 2) {

            return true;

        }

        for (long i = 2; i < number; i++) {

            if ((number % i) == 0) {

                return false;

            }

        }

        return true;

    }

}


    package lee.twowater.java7.chapterThree;


public class Main {

    public static void main(String[] args) {

        Thread task = new PrimeGenerator();

        task.start();

        

        try {

            Thread.sleep(5000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        

        task.interrupt();

    }

}

3.我猜想println()和printf()除了换行和格式化的差异之外,是不是在缓存方面还存在差异?


开满天机
浏览 629回答 3
3回答

慕桂英3389331

貌似跟缓存没啥关系,println的源码是这样的:public void println(String x) {&nbsp; &nbsp; &nbsp; &nbsp; synchronized (this) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(x);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }然后是printf的源码:&nbsp; &nbsp; public PrintStream printf(String format, Object ... args) {&nbsp; &nbsp; &nbsp; &nbsp; return format(format, args);&nbsp; &nbsp; }在format里面就是单纯的格式化操作没啥了&nbsp; &nbsp; public PrintStream format(String format, Object ... args) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; synchronized (this) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ensureOpen();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((formatter == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; || (formatter.locale() != Locale.getDefault()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formatter = new Formatter((Appendable) this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; formatter.format(Locale.getDefault(), format, args);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedIOException x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.currentThread().interrupt();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trouble = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }至于你的那个问题你可以把代码里打印输出的那一句改为:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Number is Prime: %d", number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\r");就可以了

一只名叫tom的猫

Integer number = 55;System.out.printf("Number " + number + " is Prime\n");System.out.printf("Number %s is Prime", number);换行是一个区别,但是最主要的区别是printf可以格式化表达式,具体格式语法可参考java.util.Formatter
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java