简体   繁体   中英

Are escape characters in Java platform-dependent?

I just read this question about comparing "%n" and "\\n"

What's up with Java's "%n" in printf?

The answer confirms that %n can be used across platform, while \\n is not. So I wonder what about other escape characters such as \\t , \\b, \\', \\", \\\\ .... Are they all platform-dependent just like \\n?

\\t \\' \\" and \\\\ will most likely act in the same way across all platforms as they represent real ASCII characters and there are not many platforms left that do not implement the full ASCII character set.

\\b - well that's a different matter. That will almost certainly not do the same thing across any platforms as it is supposed to implement the BEL control code which, in itself, is not platform generic.

What were you hoping to get from your ... in the question?

Added: It seems \\b is backspace - still unlikely to be cross-platform though.

Added: And as for \\f - just don't use it as it will probably only ever do something that stops working when you replace your printer - if it ever actually does something at all.

The String escape codes mean the same thing on all platforms. They map to specified Unicode codepoints that in turn correspond to standard 7-bit ASCII control characters.

The only (theoretical) concern might be some native character set which didn't have a way of representing the equivalent of those codepoints / characters. I'm pretty sure you'd be OK on ancient 6-bit and 5-bit character sets from 50+ years ago.


However, if you are trying to output text in the platform preferred form, you do need to consider two things:

  • Different platforms use different character sequences as the preferred way to designate an "end of line". (Or line separator ...)

  • The default TAB stop positions vary between platforms. On Windows they are every 4 character positions, and Unix / Linux every 8 characters.

So when you format data for fixed-width character display (eg on a "console"), you need to consider these platform dependencies.

There is also some uncertainty / variability about what will "happen" when you send those characters to a display, or include them in a file. But that's not really Java's fault, or anything that Java could address.


By contrast, "%n" ... in the context of a format string ... means the platform preferred line separator. So, on a Linux/UNIX it means "\\n" , on Windows it means "\\r" and on Macs it means "\\r\\n" . Note that this ONLY applies to format Strings; ie the first argument to String.format(...) , or something else that does that style of formatting.

Some platforms use \\r\\n as a new line, some other \\n . Using %n will ensure the right new line emitted in the output.

That has nothing to do with the backslash character preceding characters to designate special characters like the ones you mentioned. Feel free to use it in your source code.

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