简体   繁体   中英

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.

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? 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

The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding.

This means, that each single character (including a new line and a line feed) occupies 16 bit - 2 bytes - in memory.

White space is always a character, from \\n and \\r to ' '. 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).

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. 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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