1

A java source file (.java) holds human-readable text, as opposed to machine-readable binary (.class files).

If these source files hold plain text, what is the difference between .java files and .txt files? Why does java use its own file type for source code rather than .txt files?

6 Answers6

6

There can be many text file formats, just as there are many binary file formats. Using separate extensions makes them easy to differentiate, especially for the operating system (display icon, open with, etc.).

shmosel
  • 49,289
  • 6
  • 73
  • 138
2

your compiler will look for the .java extension to process the source code not .txt

Lux Me
  • 25
  • 4
2

At a low-level the files are the same. The difference is simply the naming (the dot extension) which is useful for humans and machines to easily determine what format the contents might be and what to do with the file.

Note that just naming a file .java or .class or whatever does not actually change the file type in a fundamental way but you can trick the operating system into thinking so.

Although using naming with dot extensions is popular, some file systems can also use other mechanisms for associating metadata and type information with a file.

ivo
  • 1,103
  • 10
  • 13
0

Well you could say there is no difference. But that would be in compete ignorance of semantics. The .java file will color text differently than the .cpp file in a code editor. It also tells YOU the programmer what it is.

HumbleWebDev
  • 555
  • 4
  • 20
0

If all the human-readable document have the same extension .txt, then all the file, including .java, .html, .js, .css, .jsp, etc, will turn to .txt file, then how Java know which file should be complied to bytecode, and how Tomcat know which file should be complied to bytecode, and how apache tell which file is html page.

Javdroider
  • 136
  • 8
0

Java compiler can identify the java program easily. And it only check for .java extention.

Even .CPP and .TXT is nothing but text, why do you use .CPP because compiler like turbocpp ,codeblock and many other will compile if it has .CPP as extention. Same logic for java too.