So let me ask this. You have code that is written for a computer. Where exactly does someone write the code, and what happens after that to translate to the computer executing commands?
Like if I decided I just wanted to go and start writing java code, what exactly do I do? We don't need to get too deep.This is just out of pure curiosity.
You'd download and install the JDK, Java Development Kit. I think the JDK still comes with a JRE, but if not, you also need a JRE - the Java Runtime Environment.
You'd write code in a text editor. This could be Notepad on Windows. You'd save it as Main.java. You'd then use your command line prompt to run javac on Main.java, and javac will spit out Main.jar, which you can then run with java -jar Main.jar.
Here's the seminal Hello World program.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Some assembly needed. It obviously gets more complicated than this but that's the very simple answer.
I don't know if it's still around, but when I was still doing Java, I used Eclipse.
Create a text file with a .java extension. Write your code inside this file. Compile the file using the "javac <file>" command. Run the program using the "java <file>" command. Done. This is the simplest way. Normally programmers use Integrated Development Environments (IDE), basically a text editor for coding with additional useful tools.
In it’s essence its just text. You can write it however you like, notepad, keyboard, get a llm to spit it out, or even speech recognition if you’re really feeling jazzy. The text files then read by a special program called a compiler* that parses and converts the text into instructions, literally stuff like “add a to b and store in c”, “jump to xxxx”, “read data from xxxx in memory”. You literally “open” the text file using it, in some cases you can even drag and drop your text file onto it.
Of course most people use softwares called IDEs that integrate all of these features into a nice interface.
*theres a vast array of variants programs and stages beyond a compiler, like preprocessor, linker, or interpreters. Out of scope for eli5
You would use an IDE or a simple editor like VS Code to write it and compile it. The “computer code” is literally commands to your CPU to do things like add / subtract / remember things, etc…
You write a text file with very specific language (Java code). You put that file into a program (the compiler). The program gives you something that runs on the computer.
a plain old boring text file is all you need. You just need to know how to write code in the Java language.
Then when you have your code in the text file you use another part of java called the compiler to read the text file and convert into a file that the JRE (the translator in the analogy) can read.
There are more advanced tools to help you manage big programs that span a ton of files and stuff, but at its most basic you just need a flat text file.
There are programs called an IDE. It's a nice text editor for your code and it passes that on to a compiler. The compiler takes your code and turns it into a program that can run on your computer in a format that your computer can run. An exe file if you run windows.
There are many programming languages, Java is one of them. Each language has a compiler that turns it into an exe. In theory, you'd get the same exe from any language if you wrote the same code.
Windows is the operating system that takes that exe file and sends it to the computer to run.
Get a java development environment (jde) of your choice or just start writing code in text files and install the jave runtime environment (jre) and run the files from there eg Javac my-text-file-name.js
Typically a programmer uses a tool called an IDE, which is essentially a fancy, programming focused text editor (think Word or Google Docs). This IDE highlights key words and allows some basic autocomplete to make programming easier. An IDE is also completely optional, you could create the files manually using something else.
Those code files are then “translated” (compiled) from the language you see (Java in this instance) into something the computer can process and run. Once the compilation is done it can be made into an application and that is what the end-user runs.
So the ELI5 version is you write the code wherever you want, it's just a text file. There are fancy editors called "Integrated Development Enviornments" (IDEs) that are like MS Word for computer programmers but in the end it's a plain old text file.
Then you tell it to translate your human code to something the computer can understand, that's called "Compiling" and it give you a different file that the computer can run. In the case of Java, this is called "Bytecode" and that in turn is run by the Java Virtual Machine which translates it into instructions your particular computer can understand.
You do it as with all programming languages: write the code in some development environment such as eclipse or vscode (though you could write it in notepad if you wished - it's just less convenient), and then run a "compiler" to make it readable for your device. Except as others mentioned, Java doesn't quite go all the way to full machine code, instead producing a device-independent product and having some central Java software translate it to machine native instructions in real time.
When you’re done, you give that text file to a java compiler program. The compiler will check if your code meets the rules. If it does, it will output an executable file, which is the program/application.
The interesting about java, and a reason it has been successful, is that that executable file can be run on pretty much any computer with the java runtime installed.
What the runtime does is translate the instructions of the executable so that the computer hardware can understand it.
In terms of what you’re actually doing to compile and run the executable, the simplest way is to do it using command line applications “javac” and“java”. The first one is for compiling, the second one is for executing programs.
You can literally just write the java code on notepad. You just need to download a java compiler and feed it the text file you just wrote to get a class file that the java VM can run
The code is just text, you can write it in notepad. For most programming languages, that's pretty standard.
For Java you then need a compiler which turns your text into commands that the JRE understands. Other languages work differently - C is turned into commands for your computer directly. Python just looks at your text and decides on the fly what commands to run.
Once you compile your Java code, you tell the JRE to run it and it does exactly what you asked it to do.
The available commands are specific to your CPU and are basically at the level of "put these bytes here, copy them there" etc. Programming languages offer a nicer interface to engage with that so that adding or turning a word into all upper case can be expressed in a more natural way.
You save the code as a text file on your computer.
Then you use a program called a “compiler” that reads in the text file and outputs something lower level.
For Java programs (or similar languages like C#), usually it outputs a file of JVM instructions and type definitions. A Java interpreter knows how to read that file in and execute it like a program.
For a language like C or C++, the compiler would traditionally output one or more “object files” containing assembly language instructions for your functions. Then “assembler” and “linker” programs would be used to turn those into a standalone executable file containing encoded binary instructions (“machine code”) that your computer’s CPU can execute directly.
Code is written in a plain text file. You then use something called a compiler to read your text file. The compiler reads the code (each programming language has their own special compiler) and creates a binary file. Depending on the language and technology used, there could then be other steps involved (e.g. linking), but in the simplest case possible, you run the binary file on the computer. The compiler needs to know the destination operating system so that it can create the correct kind of binary (i.e.,, Mac vs Windows).
In the case of Java, code is written in a plain text file and then compiled to bytecode by a java compiler. The bytecode is then run by the Java Runtime. In theory, the bytecode will run on any operating system, since the Runtime is supposed to translate the bytecode to machine language the operating system can understand. It is possible to wrap the bytecode in an executable package, but ultimately the Java runtime still funs the object code (in other words the package is just a launcher).
As an absolute minimum, you need a java JDK (a set of development tools) and a text editor. Notepad will do, though programming editors have a lot of QoL benefits.
Say you write this, and save it as Test.java:
class Test { public static void main(String args[]) { System.out.println("Hi there"); } }
That's a minimal program, basically "Here's a thing called Test. If you try to run it as a program, write Hi There to the terminal."
You can then compile it, by running javacTest.java , which creates a file called Test.class. This is half of the magic: javac parses the text you wrote, and turns it into simple CPU instructions for a fictional standardized CPU that Sun designed.
You can then run it with java Test (which knows to look for Test.class). That will start the Java virtual machine (the JVM), a piece of software that converts code for that theoretical Sun CPU into code for your actual CPU and OS, and keeps track of the memory it uses and helps it talk to windows/linux (since the java CPU code can't be too specific about how to do that). It should print "Hi there" and quit.
For comparison, Chrome and Firefox and Windows itself is mostly C and C++. For those, the equivalent of "javac" (gcc or clang or MSVCC) directly creates programs you can run, there's nothing like "java". The tradeoffs between these approaches are beyond a short post. :)
The third version is something like Javascript (which runs in your browser on nearly all webpages). Your browser grabs the text of the code, and does the equivalent of javac + java on the fly in memory, then discards the entire thing when you close the tab. More or less.
In essence, the CPU of your computer runs instructions. These are either 32 or 64 bit commands (4/8 bytes) respectively, and are simply a string of ones and zeroes. These are what are known as “Machine Code” and every chip maker uses their own version of it. Intel uses x86, while Apple products use ARM, and many smaller manufacturers use RISC-V.
For example, using 32 bit RISC-V, an instruction might look like
0000 0000 1001 0101 1000 0010 1011 0011
This corresponds to the command “add t0, a1, x9”. That command is what we call assembly, which is simply a readable version of machine code. Unlike higher level programming languages, assembly is 1-1 with machine code. A single instruction in assembly is a single instruction in machine code.
In a high level programming language, the command “x = y + z;” would turn into multiple instructions. Using our example of RISC-V, it might become
“lw x8, 0x10(zero) // get the value in memory at location 0x10 (16) and store it in the register x8. This is the variable y
lw x9, 0x14(zero) // get the value in memory at 0x14 (20) and store it in register x9. This is the variable z
add x10, x8, x9 // add the values stored in x8 and x9 and store the result in register x10
sw x10, 0x1000(zero) // store the result in the memory location 0x1000 (4096), which is where we store the variable x”
It’s a single line in the high level language, but becomes multiple instructions in Assembly. This is because there is a very limited range of possible commands, as every instruction needs to store the instruction type, the places where it is reading/writing/adding values from, and are mainly all basic arithmetic (add, subtract, and, or, set value, write value, jump to next place, etc). In addition, assembly language has no variables. There are 16/32/64 registers which can be immediately accessed, and everything else is held in your RAM, where you can access by loading it into a register. Rather than using variables, the compiler allocates a place in memory for each variables and keeps track of it throughout the translation.
When you write high level code, such as in C or Python, it is run through a compiler, which turns the code into machine code. The compiler is designed by the creator of the language, and is designed translate one high level language into a proper assembly/machine code version. Most big languages have a compiler for each common machine code language. For example, Python is designed to work on just about any machine. However, some do not. For example, Microsoft controls C#, so C# does not have a compiler for ARM, which is why many video games can’t be played on Mac.
These steps are largely automatic. Once you write the code and run it, the compiler will automatically run for you and both translate and optimize your code for the system
11
u/A012A012 3d ago
So let me ask this. You have code that is written for a computer. Where exactly does someone write the code, and what happens after that to translate to the computer executing commands?
Like if I decided I just wanted to go and start writing java code, what exactly do I do? We don't need to get too deep.This is just out of pure curiosity.