在学习Java时和《编程导论(Java)》中,大量使用了重载的System.out.println()等类似的输出语句。特别是书籍中,一行语句中包括System.out.println会显得太长,超过一行代码40个字符的限制,因而请静态引入tips.Print并使用相应方法代替它们。通常以

  • p替代System.out.print。
  • pln替代System.out.println

《编程导论(Java)》代码库中有tips.Print,学习设计模式时使用的是tool.Print。

package tips;
import java.io.PrintStream;
/**
* 到处都是System.out.println().假设一个例程中使用它们较多,请使用本类。
* 1.2.1类体结构,练习要求阅读本类。 * @see java.io.PrintStream
* @author yqj2065
* @version 0.1
*/
public class Print{
public static void pln(Object x){
System.out.println(x);
}
public static void pln(){
System.out.println();
}
public static void p(Object x){
System.out.print(x);
} /**
* 使用指定格式字符串和參数,打印格式化的字符串。 */
public static PrintStream pf(String format, Object... args){
return System.out.printf(format,args);
}
public static void pfln(String format, Object... args){
System.out.printf(format,args). println();
}
}

对于大量的System.out.println。能够用 pln取代。

改动:

1.pln(char[] arr)

System.out.println有大量的重载方法,而tips.Print的pln仅仅有两个重载方法。

对于char[], pln(char[])与System.out.println(char[])就不一致了。

今天看一个贴子。上面有Java Puzzlers的第12个谜题,于是发现了这个问题。

    public static void bug() {
System.out.println(new char[]{'1', '2', '3'});
pln(new char[]{'1', '2', '3'});
pln(null);
}

输出为:

123

[C@78c45512

查看JDK源码:

    public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

tips.Print中没有重载System.out.println(char[])!因而char[]自己主动造型为Object。输出为引用的“大概模样”。

而System.out.println(char[])。其文档说明:

print(char[] s)  Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the
write(int) method.

所以输出打印:123

这个bug也有一个优点:

char[] cs = null;

        pln(cs);

        System.out.println(cs);

pln(Object)不怕null。pln(cs)打印null。而System.out.print(cs) 会抛出NullPointerException(java.io.Writer.write())

2.pln(int[] arr)

为了打印int[],加入

    public static void pln(int[] arr){
System.out.print(java.util.Arrays.toString(arr));
}

Java编程练习文件夹

Java练习:tips.Print的更多相关文章

  1. Java编程Tips

    原文: Java编程中"为了性能"尽量要做到的一些地方 作者: javatgo 最近的机器内存又爆满了,除了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于 ...

  2. Java基础 println print 实现输出换行

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  3. Java 编辑tips

    1.      windows 安装 jdk配置环境 1) 下载jdk,正常安装结束,保存安装路径. 2)我的电脑—〉右键属性—〉高级系统设置—〉环境变量—〉添加系统变量 新建两个变量 JAVAHOM ...

  4. Top 10 Java Debugging Tips with Eclipse

    In this tutorial we will see about debugging java applications using Eclipse. Debugging helps us to ...

  5. java sundry tips

    1.关于Arrays 记得binarySearch方法返回的int 类型的数值的含义.    If the array contains multiple elements with the spec ...

  6. Java小tips之命令行传参

    在命令行运行主函数时,后缀字符串,则会储存在args[]数组中,这种方法可以在程序运行时,借助Main函数传参 主类书写不规范见谅 ```java public class hello{ public ...

  7. Java -Tips

    1. /* xxxx */表示多行注释,双斜杠开始表示单行注释.多行注释的快捷键: 先选中多行代码,然后按下ctrl+/就可以把选中的多行代码给注释掉.

  8. 2018 完美搭建VS Code 的JAVA开发环境并解决print乱码问题

    出自微软的Visual Studio Code 并不是一个 IDE,它是个有理想,有前途的编辑器,通过相应语言的插件,可以将其包装成一个 轻量级的功能完善的IDE. 自从遇见了她,真的是对她一见钟情不 ...

  9. Java中I/O流之Print流

    Java 中的 print 流: print 流用于做输出将会非常的方便,并且具有以下特点: 1. printWriter.printStream 都属于输出流,分别针对字符,字节. 2. print ...

随机推荐

  1. C语言ASM汇编内嵌语法

    转载:http://www.cnblogs.com/latifrons/archive/2009/09/17/1568198.html C语言ASM汇编内嵌语法 .3 GCC Inline ASM G ...

  2. Max-heap && Min-heap && push_heap

    最大堆:make_heap(vi.begin(),vi.end()) #include <iostream> #include <vector> #include <al ...

  3. redis的持久化(RDB&AOF的区别)

    RDB 是什么? 在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. Redis会单独创建(fork)一个子进程来进行持久化,会 ...

  4. Cookie 的运用

    Cookie的原理是通过Set-Cookie响应头和Cookie请求头将会话中产生的数据保存在客户端.--- 底层(SUN公司已经给我们提供了一套API) Cookie是将需要保存的数据保存在了客户端 ...

  5. 山东省第六届省赛 BIGZHUGOD and His Friends II(赛瓦定理)

    Description BIGZHUGOD and his three friends are playing a game in a triangle ground. The number of B ...

  6. 开源的图像滤镜库----for Android

    1.GPUImage for Android(推荐使用) GPUImage基于OpenGL实现的各种各样图像滤镜(图像处理)效果,多达50多种, idea源于GPUImage for iOS,基本囊括 ...

  7. Codeforces 180C. Letter

    题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个仅包含大写字母和小写字母的字符串,你可以将让小写字母转化为大写字母,大写字母转化 ...

  8. Maven学习笔记2

    Maven安装配置 1.下载Maven 官方地址:http://maven.apache.org/download.cgi 将下载的压缩包解压到你要安装 Maven 的文件夹.假设你解压缩到文件夹 – ...

  9. The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club

    Doki Doki Literature Club Time Limit: 1 Second      Memory Limit: 65536 KB Doki Doki Literature Club ...

  10. centos 7下查找大文件、大目录和常见文件查找操作

    根据园子 潇湘隐者的文章 <Linux如何查找大文件或目录总结>结合实际运维需要整理出常用命令 目标文件和目录查找主要使用 find 命令 结合 xargs (给命令传递参数的一个过滤器, ...