深入了解 Java HelloWorld
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 字符串, 我们这里深入每个步骤,探讨是怎样将字符串打印到控制台的。
环境
System 类
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");
}
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>。
@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的更多相关文章
- java HelloWorld 提示“错误: 找不到或无法加载主类 HelloWorld“解决方案
在检查环境变量等前提工作准确无误后,注意要配好CLASSPATH,仍然报“错误: 找不到或无法加载主类 HelloWorld“. 本人工程目录:mygs-maven/src/main/java/hel ...
- JAVA helloworld!
idea创建java项目 https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html 本地文档运行 java helloworld ...
- java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法
学习java的第一天: 当我在做Java入门的时候,根据教程写的第一个Java程序是: public class Hello{ public static void main(String args[ ...
- 初笔,JAVA.HelloWorld代码详解
HelloWorld.java //文件名 public class HelloWorld{ public static void main(String[] args){ System.out.pr ...
- 第一个Java HelloWorld
步骤 1.在记事本或者notePad++中编写java代码 public class Hello { public static void main(String[] args){ System.ou ...
- 第一个Java程序HelloWorld
代码如下: // 一个文件中只能有一个共有的类,并且与文件名称一致,大小写注意 public class HelloWorld{// 程序的入口public static void main(Stri ...
- HelloWorld[Java]
public class HelloWorld{ public static void main(String args[]){ System.out.println("HelloWorld ...
- Java入门记(一):折腾HelloWorld
HelloWorld,学习每门语言的第一步.有人戏称,这些年的编程生涯就是学习各种语言的HelloWorld,不知是自谦还是自嘲.目前所在的公司使用Java作为主要开发语言,我进行语言转换也大半年了, ...
- Java学习-004-传世经典Helloworld
此文主要通过一个广为人知的传世经典应用(Helloworld)讲述 Java 程序的结构,Java 程序的开发步骤,以及 Java 程序是如何运行的. 一.开发 Java 程序步骤 开发 Java 程 ...
随机推荐
- JSON-C结构简介、使用
官方站点介绍http://www.json.org JSON (JavaScript Object Notation) is a lightweight data-interchange format ...
- Android特效 五种Toast具体解释
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,并且Toast显示的时间有限,过一定的时间就会自己主动消失. 1.默认效果: 代码: Toas ...
- SQL Server 服务器磁盘测试之SQLIO篇
原文:SQL Server 服务器磁盘测试之SQLIO篇 数据库调优工作中,有一部分是需要排查IO问题的,例如IO的速度或者RAID级别无法响应高并发下的快速请求.最常见的就是查看磁盘每次读写的响应速 ...
- log4j与commons-logging,slf4j的关系(转)
前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...
- Microsoft.AlphaImageLoader过滤评论
Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.尽管FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,可是就IE5-IE6而言还是 ...
- Unix / 类 Unix shell 中有哪些很酷很冷门很少用很有用的命令?(转)
著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:孙立伟 链接:http://www.zhihu.com/question/20140085/answer/14107336 ...
- nyoj 517 最小公倍数 【java睑板】
我写了一个gcd TL该.然后调用math内gcd,AC该... 思维:它是采取n前面的最小公倍数和n求 1~n的最小公倍数 代码: import java.util.Scanner; import ...
- Do a “git export” (like “svn export”)?(转)
Probably the simplest way to achieve this is with git archive. If you really need just the expanded ...
- Codeforces Round #266 (Div. 2)-C,D
C - Number of Ways 直接暴力从前往后寻找.假设找到1/3sum的位置,那么标记++.找到2/3的位置,总数加上标记数. #include<stdio.h> #includ ...
- 发布一个参考ssdb,使用go类似的实现redis高性能nosql:ledisdb
起因 ledisdb是一个參考ssdb.採用go实现,底层基于leveldb,相似redis的高性能nosql数据库,提供了kv,list,hash以及zset数据结构的支持. 我们如今的应用极大的依 ...