Product SiteDocumentation Site

2.3. What is source code?

Let's start with an explanation of source code. One cannot understand open source without first understanding source.
Source code is a set of instructions for computers that is meant to be read and written by humans.
Here's an example of source code, in the Java programming language, for a simple, but complete, program.
public class Hello {
  public static void main(final String[] args) {
    System.out.println("Hello World!");
  }
}
In order to run this program, it must be compiled into machine code. First, we save the program into a file called Hello.java. Then, we compile it:
javac Hello.java

The command is javac, which stands for "Java Compiler." The argument is the name of the source file that we want to compile (Hello.java).

To run the program, type the command:

java Hello
This says "run the Java program called Hello that is in the current directory." When run, this program will print Hello World! and exits.

After compiling the program, you should find the bytecode file produced by the compiler (Hello.class). This is the "machine code" that the Java Virtual Machine can use to run the program.

At this point, you have two files in your directory: hello.java, the source code, and Hello.class, program bytecode produced by the compiler. That bytecode is a piece of that machine code that the Java Virtual Machine (JVM) can use to run the program. You can open it with a program called hexdump that will let you see the binary in a hexidecimal form. You can do this yourself on the command line:
hexdump hello
We've reproduced some of what it looks like when Hello.class is viewed in hexdump after hello.java has been compiled by javac:
0000000 ca fe ba be 00 00 00 37 00 22 0a 00 06 00 14 09
0000010 00 15 00 16 08 00 17 0a 00 18 00 19 07 00 1a 07
0000020 00 1b 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29
0000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e
0000040 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63
0000050 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01
0000060 00 04 74 68 69 73 01 00 07 4c 48 65 6c 6c 6f 3b
0000070 01 00 04 6d 61 69 6e 01 00 16 28 5b 4c 6a 61 76
0000080 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56
0000090 01 00 04 61 72 67 73 01 00 13 5b 4c 6a 61 76 61
00000a0 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 01 00 0a
00000b0 53 6f 75 72 63 65 46 69 6c 65 01 00 0a 48 65 6c
00000c0 6c 6f 2e 6a 61 76 61 0c 00 07 00 08 07 00 1c 0c
00000d0 00 1d 00 1e 01 00 0b 48 65 6c 6c 6f 20 57 6f 72
00000e0 6c 64 07 00 1f 0c 00 20 00 21 01 00 05 48 65 6c
00000f0 6c 6f 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f
0000100 62 6a 65 63 74 01 00 10 6a 61 76 61 2f 6c 61 6e
0000110 67 2f 53 79 73 74 65 6d 01 00 03 6f 75 74 01 00
0000120 15 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53
0000130 74 72 65 61 6d 3b 01 00 13 6a 61 76 61 2f 69 6f
0000140 2f 50 72 69 6e 74 53 74 72 65 61 6d 01 00 07 70
0000150 72 69 6e 74 6c 6e 01 00 15 28 4c 6a 61 76 61 2f
0000160 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56 00 21
0000170 00 05 00 06 00 00 00 00 00 02 00 01 00 07 00 08
0000180 00 01 00 09 00 00 00 2f 00 01 00 01 00 00 00 05
0000190 2a b7 00 01 b1 00 00 00 02 00 0a 00 00 00 06 00
00001a0 01 00 00 00 07 00 0b 00 00 00 0c 00 01 00 00 00
00001b0 05 00 0c 00 0d 00 00 00 09 00 0e 00 0f 00 01 00
00001c0 09 00 00 00 37 00 02 00 01 00 00 00 09 b2 00 02
00001d0 12 03 b6 00 04 b1 00 00 00 02 00 0a 00 00 00 0a
00001e0 00 02 00 00 00 0d 00 08 00 0e 00 0b 00 00 00 0c
00001f0 00 01 00 00 00 09 00 10 00 11 00 00 00 01 00 12
0000200 00 00 00 02 00 13                              
0000206
Even though there is only one executable statement in the program, this bytecode representation is somewhat larger and not easily read by humans. (The JVM that runs this bytecode is about 300MB.)
As you can see, there's a huge difference between source code, which is intended to be read and written by humans, and binary code, which is intended to be read and written by computer processors.
This difference is a crucial one for programmers who need to modify a computer program. Let's say you wanted to change the program to say "Open source is awesome!!!". With access to the source code, making this change is trivial, even for a novice programmer. Without access to the source code, making this change would be incredibly difficult. And this for just a few lines of code.

2.3.1. Exercise - Change the source code

Change the source code to print out "Open source is awesome!!!" instead of "Hello World!". Spend no more than half an hour on this exercise.