Java 的 Hello World 代码


public class HelloWorld {

    /**
*
* @param args
*/ public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
} }

编译执行

$ javac HelloWorld.java
$ java HelloWorld
Hello World

功能就是在控制台打印 Hello World 字符串, 我们这里深入每个步骤,探讨是怎样将字符串打印到控制台的。


环境

我们刚才的程序执行在 Ubuntu Linux 的控制台 bash 程序里。 javac 是编译器。 java 执行虚拟机。加载 HelloWorld.class 执行 HelloWorld 类的 main 函数,这部分是属于虚拟机的内容,临时不做考虑,我们主要从代码调用的步骤分析。

System 类

从代码逻辑上,我们非常easy看出是 System 类的 out 成员 println 函数运行了打印操作:

public final class System {

    /**
* Default input stream.
*/
public static final InputStream in; /**
* Default output stream.
*/
public static final PrintStream out; /**
* Default error output stream.
*/
public static final PrintStream err; private static final String lineSeparator;
private static Properties systemProperties; static {
err = new PrintStream(new FileOutputStream(FileDescriptor.err));
out = new PrintStream(new FileOutputStream(FileDescriptor.out));
in = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
lineSeparator = System.getProperty("line.separator");
}

out 是个 PrintStream

PrintStream 类

    /**
* Prints a string followed by a newline. The string is converted to an array of bytes using
* the encoding chosen during the construction of this stream. The bytes are
* then written to the target stream with {@code write(int)}.
*
* <p>If an I/O error occurs, this stream's error state is set to {@code true}.
*
* @param str
* the string to print to the target stream.
* @see #write(int)
*/
public synchronized void println(String str) {
print(str);
newline();
}

详细的工作交给了 print ,  我们在这个类里面能够看到 newline 仅仅是打印了两个特殊字符,换行。


    public synchronized void print(String str) {
if (out == null) {
setError();
return;
}
if (str == null) {
print("null");
return;
} try {
if (encoding == null) {
write(str.getBytes());
} else {
write(str.getBytes(encoding));
}
} catch (IOException e) {
setError();
}
}

进一步的工作由 write 函数完毕:

    public synchronized void write(int oneByte) {
if (out == null) {
setError();
return;
}
try {
out.write(oneByte);
int b = oneByte & 0xFF;
// 0x0A is ASCII newline, 0x15 is EBCDIC newline.
boolean isNewline = b == 0x0A || b == 0x15;
if (autoFlush && isNewline) {
flush();
}
} catch (IOException e) {
setError();
}
}

又来了个 out 这个。out 是 来自 OutputStream 的实现类 public class FileOutputStream extends OutputStream

public class FileOutputStream extends OutputStream {

    private FileDescriptor fd;
private final boolean shouldClose; /** The unique file channel. Lazily initialized because it's rarely needed. */
private FileChannel channel; /** File access mode */
private final int mode; private final CloseGuard guard = CloseGuard.get();

FileDescriptor

public final class FileDescriptor {

    /**
* Corresponds to {@code stdin}.
*/
public static final FileDescriptor in = new FileDescriptor(); /**
* Corresponds to {@code stdout}.
*/
public static final FileDescriptor out = new FileDescriptor(); /**
* Corresponds to {@code stderr}.
*/
public static final FileDescriptor err = new FileDescriptor(); /**
* The Unix file descriptor backing this FileDescriptor.
* A value of -1 indicates that this FileDescriptor is invalid.
*/
private int descriptor = -1; static {
in.descriptor = STDIN_FILENO;
out.descriptor = STDOUT_FILENO;
err.descriptor = STDERR_FILENO;
}

这里来到 STDOUT_FILENO。 该变量定义在: 在<stdio.h>。

STDIN_FILENO等是文件描写叙述符,是非负整数,一般定义为0, 1, 2,属于没有buffer的I/O。直接调用系统调用。在<unistd.h>。

參考i: http://blog.csdn.net/xiaoxi2xin/article/details/5524769
    @Override
public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException {
IoBridge.write(fd, buffer, byteOffset, byteCount);
} @Override
public void write(int oneByte) throws IOException {
write(new

最后是调用 os 的 write



版权声明:本文博客原创文章,博客,未经同意,不得转载。

深入了解 Java HelloWorld的更多相关文章

  1. java HelloWorld 提示“错误: 找不到或无法加载主类 HelloWorld“解决方案

    在检查环境变量等前提工作准确无误后,注意要配好CLASSPATH,仍然报“错误: 找不到或无法加载主类 HelloWorld“. 本人工程目录:mygs-maven/src/main/java/hel ...

  2. JAVA helloworld!

    idea创建java项目 https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html 本地文档运行 java helloworld ...

  3. java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法

    学习java的第一天: 当我在做Java入门的时候,根据教程写的第一个Java程序是: public class Hello{ public static void main(String args[ ...

  4. 初笔,JAVA.HelloWorld代码详解

    HelloWorld.java //文件名 public class HelloWorld{ public static void main(String[] args){ System.out.pr ...

  5. 第一个Java HelloWorld

    步骤 1.在记事本或者notePad++中编写java代码 public class Hello { public static void main(String[] args){ System.ou ...

  6. 第一个Java程序HelloWorld

    代码如下: // 一个文件中只能有一个共有的类,并且与文件名称一致,大小写注意 public class HelloWorld{// 程序的入口public static void main(Stri ...

  7. HelloWorld[Java]

    public class HelloWorld{ public static void main(String args[]){ System.out.println("HelloWorld ...

  8. Java入门记(一):折腾HelloWorld

    HelloWorld,学习每门语言的第一步.有人戏称,这些年的编程生涯就是学习各种语言的HelloWorld,不知是自谦还是自嘲.目前所在的公司使用Java作为主要开发语言,我进行语言转换也大半年了, ...

  9. Java学习-004-传世经典Helloworld

    此文主要通过一个广为人知的传世经典应用(Helloworld)讲述 Java 程序的结构,Java 程序的开发步骤,以及 Java 程序是如何运行的. 一.开发 Java 程序步骤 开发 Java 程 ...

随机推荐

  1. android 电平信号状态识别View平局

    1.前言 级信号状态View在今天的Android系统是常见.状态的图标就很的经典,有几种状态,到了快没电的时候有些还会闪烁提示用户充电:还有的就是一些地图App的GPS信号强度的提示.Wifi信号强 ...

  2. &quot;伪中国移动client&quot;--伪基站诈骗

    一.简单介绍: 近日,百度安全实验室发现一款"伪中国移动client"病毒.犯罪分子通过伪基站方式大量发送伪10086的短信,诱导用户点击钓鱼链接:并在钓鱼页面诱导用户输入网银账号 ...

  3. 应用spss可靠性分析软件

    问卷调查的可靠性分析 一.概念:     信度是指依据測验工具所得到的结果的一致性或稳定性,反映被測特征真实程度的指标. 一般而言,两次或两个測验的结果愈是一致.则误差愈小,所得的信度愈高,它具有下面 ...

  4. Timer Swing

    一个Swing的例子,按钮控件上中文出现乱码: 试了网上的设置Font,或将汉字使用new  String(str.getBytes(),"GBK")对展示的汉字进行编码.都无效. ...

  5. (大数据工程师学习路径)第一步 Linux 基础入门----Linux 下软件安装

    介绍 介绍 Ubuntu 下软件安装的几种方式,及 apt,dpkg 工具的使用. 一.Linux 上的软件安装 通常 Linux 上的软件安装主要有三种方式: 在线安装 从磁盘安装deb软件包 从二 ...

  6. BZOJ 1015 JSOI2008 星球大战 starwar 并检查集合

    标题效果:给定一个无向图.联通谋求块的数目,以及k一个点的破坏后每次:联通,块的数目 侧面和摧毁的地步全记录,我们可以做相反的. 需要注意的是该点不能算作破坏联通块 #include<cstdi ...

  7. SQL Server 备份和还原

    SQL Server 备份和还原   SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...

  8. Java流

    流是一组有顺序的,有起点和终点的字节集合,是对传输数据的总称或抽象.即数据在两设备间的传输称为流,流的本质是传输数据,依据传输数据特性将流抽象为各种类,方便更直观的进行数据操作. 流的分类: 数据类型 ...

  9. UI測试内容

    我们在实际工作其中,针对web应用程序,也就是常常所说的B/S系统,能够从例如以下方面来进行用户界面測试: 导航測试 导航描写叙述了用户在一个页面内操作的方式,在不同的用户接口控制之间,比如butto ...

  10. bigdata_hadoop集群配置_内存分配

    haoop集群  做好内存管理跟重要,不然经常会给抛出个 OutMemory   ,内存溢出 以horntonworks给出推荐配置为样本,给出一种常见的Hadoop集群上各组件的内存分配方案.配置时 ...