首页编程java编程java until有什么意思?java中until包的应用

java until有什么意思?java中until包的应用

编程之家2023-10-1288次浏览

老铁们,大家好,相信还有很多朋友对于java until有什么意思和java中until包的应用的相关问题不太懂,没关系,今天就由我来为大家分享分享java until有什么意思以及java中until包的应用的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!

java until有什么意思?java中until包的应用

java "字段"啥意思

public final static InputStream in= nullInputStream();

nullInputStream是这样实现的:

private static InputStream nullInputStream() throws NullPointerException{

java until有什么意思?java中until包的应用

if(currentTimeMillis()> 0)

return null;

throw new NullPointerException();

java until有什么意思?java中until包的应用

}

他不是返回null,就是抛出异常,如何初始化in呢?

解答:

看了一下java.lang.System的源代码.

System类里有大量的native方法,是调用本地代码的,这些代码很可能是由虚拟机来调用的.

System类的开头有一段:

static{

registerNatives();

}

这段代码会在虚拟机启动的时候就执行,它在虚拟机里注册System需要使用的一些本地代码

比如:

private static native Properties initProperties(Properties props);

private static native void setOut0(PrintStream out);

在windows下的话,它就告诉虚拟机到哪个dll文件里去找相应的实现

>然而,我知道out是一个PrintStream的对象,但我查看了有关的原代码:public final static PrintStream out= nullPrintStream();

>public final static InputStream in= nullInputStream();

在nullInputStream()方法里有注释解释为什么会设置为空:

/**

* The following two methods exist because in, out, and err must be

* initialized to null. The compiler, however, cannot be permitted to

* inline access to them, since they are later set to more sensible values

* by initializeSystemClass().

*/

private static InputStream nullInputStream() throws NullPointerException{

if(currentTimeMillis()> 0)

return null;

throw new NullPointerException();

}

也就说in, out, and err初始化为null,然后会在后来由initializeSystemClass()方法类初始化成有意义的值

/**

* Initialize the system class. Called after thread initialization.

*/

private static void initializeSystemClass(){

props= new Properties();

initProperties(props);

sun.misc.Version.init();

FileInputStream fdIn= new FileInputStream(FileDescriptor.in);

FileOutputStream fdOut= new FileOutputStream(FileDescriptor.out);

FileOutputStream fdErr= new FileOutputStream(FileDescriptor.err);

setIn0(new BufferedInputStream(fdIn));!!!

setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));!!!

setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));!!!

// Enough of the world is now in place that we can risk

// initializing the logging configuration.

try{

java.util.logging.LogManager.getLogManager().readConfiguration();

} catch(Exception ex){

// System.err.println("Can′t read logging configuration:");

// ex.printStackTrace();

}

// Load the zip library now in order to keep java.util.zip.ZipFile

// from trying to use itself to load this library later.

loadLibrary("zip");

// Subsystems that are invoked during initialization can invoke

// sun.misc.VM.isBooted() in order to avoid doing things that should

// wait until the application class loader has been set up.

sun.misc.VM.booted();

}

in,out,err就是在以上方法以下三条语句里初始化的.

setIn0(new BufferedInputStream(fdIn));!!!

setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));!!!

setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));!!!

private static native void setIn0(InputStream in);

~~~~~~~

这是个native函数,是前面registerNatives()的时候注册了的.这个函数应该是把实际连接到输入输出设备的句柄传给虚拟机并赋值给in,out,err

至于:

>InputStream是个抽象的类,怎么能使用char=(char)System.in.read()读入一个字符

我想你还没有明白什么是面向对象.

看看下面代码,我用OutputStream(也是抽象类,跟InputStream对应的输出类)以方便演示:

import java.io.IOException;

import java.io.OutputStream;

public class HelloWorld{

public OutputStream out=null;

public void setOutputStream(OutputStream out){

this.out=out;

}

public static void main(String[] args) throws IOException{

HelloWorld h=new HelloWorld();

PrintStream myOut=System.out;//System.out是一个PrintStream

h.setOutputStream(myOut);

h.out.write("hello,world".getBytes());//一般没人这么写的

}

}

以上代码执行后会输出hello,world

h.out是OutputStream,也是个抽象类,为什么能write(o)呢?

因为PrintStream是OutputStream的子类,所以能被"当作"OutputStream传给h.setOutputStream(myOut);

h.out.write执行的时候实际上是调用这个传进来的PrintStream实例的write方法

同样System.in和out肯定也是在initializeSystemClass()的时候被赋予了一个实际的可用的子类

要能体会到面向对象的好处,就要逐渐适应"对接口编程"的思想,相同接口的对象可以根据需要方便的替换.

比如,我刚才传了一个PrintStream,因此HelloWorld输出到了屏幕上.我如果传给OutputStream的另一个子类FileOutputStream,就会输出到文件里

>还有为什么不是说字符流:writer和reader一般用于UniCode的读写吗?为什么键盘的输入用reader类呢?

不知道你在哪里看到说writer和reader一般用于UniCode的读写

java sleep()和 wait() 有什么区别

sleep和wait的区别:

1、sleep的意思是:睡,睡觉,睡眠。

2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。

拓展资料

sleep的用法

1、They were exhausted from lack of sleep

由于缺乏睡眠,他们非常疲惫。

2、During the car journey, the baby slept

坐车来的路上,宝宝睡着了。

3、I think he may be ready for a sleep soon.

我想他也许很快就要睡一觉了。

4、I can't get to sleep with all that singing.

那些歌声搅得我无法入睡。

5、I didn't lose too much sleep over that investigation.

我并不太担心那个调查。

wait

1、I walk to a street corner and wait for the school bus

我走到街角等校车。

2、There'll be a car waiting for you

会有辆汽车等你。

3、I want to talk to you, but it can wait

我想和你谈谈,但可以晚点再说。

4、If you think this all sounds very exciting, just wait until you read the book

如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。

5、'Wait a minute!' he broke in.'This is not giving her a fair hearing!'

“等一下,”他插嘴说,“这没有给她一个公平的解释机会!”

java中until包的应用

until这个包下大部分都是java中的集合类。主要定义了两种接口:包含集合框架、遗留的 collection类、事件模型、日期和时间设施、国际化和各种实用工具类(字符串标记生成器、随机数生成器和位数组)。这是API解释;JDk中所有的集合类由2个接口定义:Collection和Map;实现了Collection接口的有LIst接口和Set接口两种;实现了了LIst接口的集合类其中的包含的对象是无顺序的并且可以重复的。例如ArrayLIst,实现了set接口的类里面的对象是有顺序不可以重复的;例如:HashSet除此之外:实现了Map接口的是映射集合类;以键值的方式存储对象的;例如实现了Map接口的HashMap;

好了,文章到这里就结束啦,如果本次分享的java until有什么意思和java中until包的应用问题对您有所帮助,还望关注下本站哦!

苹果电脑编程java用什么软件?苹果电脑开发java需要哪些软件,收费也行.java用什么取代了指针?java语言与C++相比,有哪些优点