简体   繁体   English

在Java中,空格和换行符是否视为字节?

[英]Are spaces and newlines considered a byte in Java?

I want to code a method that extracts the number of bytes of a Java file. 我想编写一种方法来提取Java文件的字节数。

So, the compiler stores each character of a file in a byte of memory, but does it also store spaces, \\n and \\r in one byte of memory? 那么,编译器将文件的每个字符存储在一个字节的内存中,但是它是否还在一个字节的内存中存储空格\\n\\r Should I include them in my calculations? 我应该在计算中包括它们吗?

I couldn't find a specific explanation anywhere. 我在任何地方都找不到具体的解释。

I couldn't find a specific explanation anywhere. 我在任何地方都找不到具体的解释。

See the Java specification 请参阅Java规范

The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding. Java编程语言使用UTF-16编码以16位代码单元的顺序表示文本。

This means, that each single character (including a new line and a line feed) occupies 16 bit - 2 bytes - in memory. 这意味着,每个单个字符(包括换行和换行符)在内存中占用16位-2个字节。

White space is always a character, from \\n and \\r to ' '. 空格始终是一个字符,从\\ n和\\ r到''。 Therefore they are stored the same way as characters. 因此,它们的存储方式与字符相同。 Since they are all under 255 in ASCII, I'd assume they're merely a byte (it is my understanding that standard ASCII based characters are represented as a single byte, even in Unicode, because their representation is low enough in value to reserve a mere byte; however, anything greater than 255 would end up being two bytes -- or larger, for UTF-32). 由于它们的ASCII码都在255以下,因此我假设它们只是一个字节(据我的理解,即使是Unicode,标准的基于ASCII的字符也被表示为单个字节,因为它们的表示值很低,无法保留一个字节;但是,大于255的结果最终将是两个字节-或对于UTF-32而言更大)。

All that said, if the new line or carriage return you're talking about is from a call to System.out.println() then they would not be stored at all (or once?) as you're calling a predefined method by Java. 话虽如此,如果您要谈论的新行或回车是从对System.out.println()的调用中获得的,则它们根本不会被存储(或一次?),因为您正在通过以下方式调用预定义方法Java。 The new line character only has to be stored once (in the method) to be called again and again. 新行字符只需存储一次(在方法中)即可一次又一次地调用。 Java does not need to store multiple copies of that newline to call System.out.println() multiple times. Java不需要存储该换行符的多个副本即可多次调用System.out.println()。

Yes, they should be included in your calculations -- special characters are characters as well. 是的,它们应该包含在您的计算中-特殊字符也是字符。 Also, Java stores a char using 2 bytes :) hope this helped! 此外,Java使用2个字节存储一个char :)希望这有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM