system
Given the following post, respond as if you're commenting on Reddit:
human
How does my computer differentiate between different programming languages? How does a computer treat different file extensions?

If I write code in a text editor, and save it under a certain file extension, how does my computer know what type of code should be running? How exactly does a computer differentiate between, say, Java code, JS code, CPP code, etc.?

Does it compare the code to a database? Is the computer programmed to know based on the file extension?

Moreover, how do file extensions work? How does my computer recognize file extensions and format the file accordingly?

EDIT: thanks to everyone for the answers! I will read them as soon as I can.
gpt
A file extension is just part of the file name. It doesn't actually enforce anything. You can have your OS try to open any file with any program. On Mac, it's right click -> open with -> enable all programs -> select whatever. Go ahead, open an image in a text editor and see the binary data interpreted as text. You can also rename a file with whatever extension you want--a modern OS will warn you that you're doing something stupid, but it won't stop you.

The OS has a mapping of file extensions to programs. It has a ton of common ones built in, and when you install a program with its own file type, it can register that with the OS. When you tell your OS to open a file with an extension, it assumes the extension is right, it opens whatever program is associated to open that type of file, and that program deals with it. A well-written program will attempt to read the file but do so safely, so that if something's fucked it fails gracefully. Presumably, source code files are set up to be opened with a text editor or IDE.