Java 的 Hello World 代码


  1. public class HelloWorld {
  2.  
  3. /**
  4. *
  5. * @param args
  6. */
  7.  
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. System.out.println("Hello World");
  11. }
  12.  
  13. }

编译执行

  1. $ javac HelloWorld.java
  2. $ java HelloWorld
  3. Hello World

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


环境

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

System 类

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

  1. public final class System {
  2.  
  3. /**
  4. * Default input stream.
  5. */
  6. public static final InputStream in;
  7.  
  8. /**
  9. * Default output stream.
  10. */
  11. public static final PrintStream out;
  12.  
  13. /**
  14. * Default error output stream.
  15. */
  16. public static final PrintStream err;
  17.  
  18. private static final String lineSeparator;
  19. private static Properties systemProperties;
  20.  
  21. static {
  22. err = new PrintStream(new FileOutputStream(FileDescriptor.err));
  23. out = new PrintStream(new FileOutputStream(FileDescriptor.out));
  24. in = new BufferedInputStream(new FileInputStream(FileDescriptor.in));
  25. lineSeparator = System.getProperty("line.separator");
  26. }

out 是个 PrintStream

PrintStream 类

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

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


  1. public synchronized void print(String str) {
  2. if (out == null) {
  3. setError();
  4. return;
  5. }
  6. if (str == null) {
  7. print("null");
  8. return;
  9. }
  10.  
  11. try {
  12. if (encoding == null) {
  13. write(str.getBytes());
  14. } else {
  15. write(str.getBytes(encoding));
  16. }
  17. } catch (IOException e) {
  18. setError();
  19. }
  20. }

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

  1. public synchronized void write(int oneByte) {
  2. if (out == null) {
  3. setError();
  4. return;
  5. }
  6. try {
  7. out.write(oneByte);
  8. int b = oneByte & 0xFF;
  9. // 0x0A is ASCII newline, 0x15 is EBCDIC newline.
  10. boolean isNewline = b == 0x0A || b == 0x15;
  11. if (autoFlush && isNewline) {
  12. flush();
  13. }
  14. } catch (IOException e) {
  15. setError();
  16. }
  17. }

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

  1. public class FileOutputStream extends OutputStream {
  2.  
  3. private FileDescriptor fd;
  4. private final boolean shouldClose;
  5.  
  6. /** The unique file channel. Lazily initialized because it's rarely needed. */
  7. private FileChannel channel;
  8.  
  9. /** File access mode */
  10. private final int mode;
  11.  
  12. private final CloseGuard guard = CloseGuard.get();

FileDescriptor

  1. public final class FileDescriptor {
  2.  
  3. /**
  4. * Corresponds to {@code stdin}.
  5. */
  6. public static final FileDescriptor in = new FileDescriptor();
  7.  
  8. /**
  9. * Corresponds to {@code stdout}.
  10. */
  11. public static final FileDescriptor out = new FileDescriptor();
  12.  
  13. /**
  14. * Corresponds to {@code stderr}.
  15. */
  16. public static final FileDescriptor err = new FileDescriptor();
  17.  
  18. /**
  19. * The Unix file descriptor backing this FileDescriptor.
  20. * A value of -1 indicates that this FileDescriptor is invalid.
  21. */
  22. private int descriptor = -1;
  23.  
  24. static {
  25. in.descriptor = STDIN_FILENO;
  26. out.descriptor = STDOUT_FILENO;
  27. err.descriptor = STDERR_FILENO;
  28. }

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

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

參考i: http://blog.csdn.net/xiaoxi2xin/article/details/5524769
  1. @Override
  2. public void write(byte[] buffer, int byteOffset, int byteCount) throws IOException {
  3. IoBridge.write(fd, buffer, byteOffset, byteCount);
  4. }
  5.  
  6. @Override
  7. public void write(int oneByte) throws IOException {
  8. 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. 金蝶K3管理软件PDA条码解决方式,盘点机与金蝶K3无缝对接

    申明:以上文字为"武汉汉码科技有限公司"原创,转载时务必注明出处. 技术分享,沟通你我,共同进步!www.hanma-scan.com 原帖:http://www.hanma-sc ...

  2. debian 该分区的部分安装移动硬盘后无法识别。

    有一个新的团购1T移动硬盘.购买格化学式ntfs经过几次简单的子区域. 4G硬盘PE.100G高速互动,盈800许多G分为两个相等的存储盘. 到您的计算机USB接口后,, 桌面弹出自己主动4一封信. ...

  3. 无插件,直接加参数,chrome它可以模拟手机浏览器

    在目标出现,加上一些参数即可:--user-agent="mozilla/5.0 (linux; u; android 2.3.3; en-us; sdk build/ gri34) app ...

  4. 3D数学学习笔记——笛卡尔坐标系

    本系列文章由birdlove1987编写.转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24601215 1.3D数学 ...

  5. 一张地图告诉你,只JavaScript不够!

    这将是JavaScript语法,你真的会一JavaScript嘛.看看这个图片!超好用JavaScript一本书的摘录游.熊儿.快去学习! 版权声明:本文博客原创文章.博客,未经同意,不得转载.

  6. Log4net 日志

    Log4net 日志使用介绍 概述 Log4net 有三个主要组件:loggers,appenders 和 layouts.这三个组件一起工作使得开发者能够根据信息类型和等级(Level)记录信息,以 ...

  7. 实现 mouse-drag 的图标拖动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. Flex自定义组件开发之日周月日期选择日历控件

    原文:Flex自定义组件开发之日周月日期选择日历控件         使用过DateField的我们都知道,DateField 控件是用于显示日期的文本字段,字段右侧带有日历图标.当用户在控件边框内的 ...

  9. 一个JavaWeb项目开发总结

    一.学会如何读一个JavaWeb项目源代码 步骤:表结构->web.xml->mvc->db->spring ioc->log->代码 先了解项目数据库的表结构,这 ...

  10. jQuery组织您钞四----jQuery操作DOM

    一.采用jQuery创建节点 节点是DOM基础设施.依据DOM产品规格,Node是一个很宽泛的概念,包含元素.属性.正文.档..实际开发过程中,要创建动态内容,主要操作的节点包括元素. 属性和文本. ...