Java整数是否等于字符?(Java integer is equal to character?)

编程入门 行业动态 更新时间:2024-10-28 02:31:35
Java整数是否等于字符?(Java integer is equal to character?)

如果这个问题有点简单,我很抱歉,但我有点疑惑为什么我的教授做了以下声明:

请注意,read()返回一个整数值。 使用int作为返回类型允许read()使用-1来表示它已到达流的末尾。 您将从对Java的介绍中回忆一下,int等于使用-1方便的char。

教授正在引用以下示例代码:

public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("Independence.txt"); out = new FileOutputStream("Independence.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }

这是一门高级Java课程,所以很明显我在这之前已经学过一些入门课程。 也许我只是在进行各种各样的“金发时刻”,但是我不理解在进行比较时整数可以等于一个字符。 实例方法read()在EOF时返回一个整数值。 我完全理解。

任何人都可以用粗体来阐明声明吗?

I apologize if this question is a bit simplistic, but I'm somewhat puzzled as to why my professor has made the following the statement:

Notice that read() returns an integer value. Using an int as a return type allows read() to use -1 to indicate that it has reached the end of the stream. You will recall from your introduction to Java that an int is equal to a char which makes the use of the -1 convenient.

The professor was referencing the following sample code:

public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("Independence.txt"); out = new FileOutputStream("Independence.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }

This is an advanced Java course, so obviously I've taken a few introductory courses prior to this one. Maybe I'm just having a "blonde moment" of sorts, but I'm not understanding in what context an integer could be equal to a character when making comparisons. The instance method read() returns an integer value when it comes to EOF. That I understand perfectly.

Can anyone shed light on the statement in bold?

最满意答案

在Java中,chars是一种更具体的int类型。 我可以写。

char c = 65;

此代码打印出“A”。 我需要在那里进行转换,因此Java知道我想要字符表示而不是整数。

public static void main(String... str) { System.out.println((char) 65); }

您可以在ASCII表中查找int到字符映射。

根据你的老师,int允许更多的价值观。 由于-1不是字符值,因此它可以用作标志值。

In Java, chars is a more specific type of int. I can write.

char c = 65;

This code prints out "A". I need the cast there so Java knows I want the character representation and not the integer one.

public static void main(String... str) { System.out.println((char) 65); }

You can look up the int to character mapping in an ASCII table.

And per your teacher, int allows for more values. Since -1 isn't a character value, it can serve as a flag value.

更多推荐

本文发布于:2023-08-03 12:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   字符   Java   character   equal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!