0

What is the file encoding type used for Java class files?

I am not referring to the file.encoding or Charset.defaultCharset() properties.

When javac compiles a .java file to a .class file and saves the resulting file on disk, what file encoding type is used? Is it UTF-8?

Can someone provide more details on this? I do not want to make assumptions.

CodeBot
  • 33
  • 5
  • the one you specified. For .java files, check the javac doc: https://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html – jhamon Jun 16 '23 at 09:43
  • I imagine this would entirely depend on your text-editor, or IDE. For example, I'm using UTF-8, here. – Reilas Jun 16 '23 at 12:17
  • You will most probably (can't account for *all* compilers) find that the *strings* inside the class file will end up as UTF-8 irrespective of your source file encoding – g00se Jun 16 '23 at 12:24
  • @g00se - `strings inside the class file will end up as UTF-8 irrespective of your source file encoding` good point. – CodeBot Jun 16 '23 at 13:25

2 Answers2

3

Encoding refers to text, not binary data.

Class files are binary data - so there's no notion of a text encoding for them, any more than there is for an MP3 file or a JPEG.

Do not try to read a class file as if it's text - you're almost certain to lose information.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

When Java compiles a .java source file into a .class file, the resulting .class file does not use a specific file encoding type like UTF-8. Instead, it follows a binary format known as the Java class file format.

The Java class file format is designed to be platform-independent and is primarily intended to be consumed by the Java Virtual Machine (JVM). It contains a structured set of bytes that represent various elements of the compiled Java code, such as class declarations, methods, fields, and bytecode instructions.

The class file format specifies a specific layout and organization of bytes, including headers, constant pools, access flags, attribute tables, and more. It does not rely on any specific character encoding scheme like UTF-8 because the content of a class file consists of compiled Java bytecode instructions, numeric values, and symbolic references rather than human-readable text.

Therefore, when you compile a Java source file using the javac compiler, the resulting .class file is not encoded as text but as binary data conforming to the Java class file format.

azer-p
  • 304
  • 1
  • 12
  • @Vamsh It's completely inappropriate to ask others to upvote your own question. Don't do that. – skomisa Jun 16 '23 at 17:05
  • @skomisa, to upvote good answers, i need 50 reps. thats the point. If at all.. try to encourage new bees on the platform. – CodeBot Jun 16 '23 at 18:34
  • @Vamsh You should earn your reputation by posting good questions and good answers, not by pestering users to upvote you. Don't do it. – skomisa Jun 16 '23 at 20:10