Introduction to Coding in Java

Introduction to Coding in Java

Welcome to the first article in a ten-part series on the introduction to coding in Java. If this is your first time coding, you’ve come to the right place. Let’s get started!

Table of Contents

Java Background

Java is a programming language that popped on the scene in 1995 from a company called Sun Microsystems now known as Oracle Corporation. In terms of capabilities, Java is a high level programming language which is both object-oriented and strongly typed—we’ll get to both of these concepts later.

If Java had a claim to fame, it would probably be its portability. All code is compiled to byte code and executed on the Java Virtual Machine or JVM. This allows a developer to write code on almost any platform and transfer it between platforms.

But, let’s back up! You’re probably here because you want to know the very basics of Java. Rather than getting bogged down with a lot of theory, you probably prefer to get your hands on some examples. In these tutorials, I’ll start every section by visiting some of the concepts that I feel you will need to know.

With the theory out of the way, we’ll tackle a couple examples. If this series isn’t enough for you, here are some additional resources:

Of course, we can’t get to any of that if we never get started, so let’s get to work.

The Basics of Logic

Before we can get into programming, we need to get an idea of how a computer works at a low level. To do so, we’ll need to cover the following concepts: computer processors and logic gates.

Computer Processors

At the center of every computer is a processor which handles all of the thinking for the system.

Think of the processor as a series of wires and switches which direct the flow of current through a circuit. At each junction, a switch can be flipped open which allows current to pass from the input of the junction to the output. Likewise, a switch can block the flow of current when flipped closed.

In logic, we use a special kind of switch called a transistor. However unlike switches, transistors have a special property where they can produce current on the output without any current on the input.

Logic Gates

Now to make these transistors useful, we usually combine them to form a logic gate. A logic gate is just a configuration of transistors characterized by a truth table. To obtain the truth table for a particular logic gate, we usually need to determine the output of a logic gate for all possible combinations of inputs.

For simplicity, we typically refer to an input as ON or 1 when there is current on it. Otherwise, we refer to that input as OFF or 0.

We can then use this knowledge to give some meaning to a few of the basic logic gates: AND, OR, NAND, and NOR. These gates control the flow of electricity in a circuit by only producing current on the output under special conditions. For instance, an AND gate only opens if all of its inputs have current on them. In other words, all of the inputs are ON or 1.

Logic Gate Diagrams

The opposite of the AND gate is the NAND gate which only produces current on the output if none of the inputs have current on them. In other words, all the inputs must be OFF or 0. In the next section, we’ll take a look at what this means for developers.

An Introduction to Binary

With some basic logic under our belt, we can now go up one level of abstraction. In particular, we’ll cover number systems and bits.

Number Systems

The zeroes and ones that are used to describe interactions with logic gates are the same units a computer uses in programming. These units are described using a number system called binary. Binary is a base 2 number system where the two possible values are 0 and 1.

In contrast, humans have decided to use a base 10 number system (possible values are 0-9). The beauty of binary is that we can start representing numbers based on the flow of electrons in a circuit.

As an example, let’s say we have a circuit that has four outputs. If each output can have a value of zero or one, how many possible output combinations can there be?

Well, each output can be two values, so we multiply the number of combinations for each output (2 x 2 x 2 x 2). In total, we can have 16 combinations which gives us a decimal range of 0-15. So regardless of what this circuit does, we can start recording results as decimal numbers.

Bits and Bytes

In computer science, we call a binary value (a 0 or a 1) a bit. In the example above, the circuit output was 4 bit. The number of bits we use is important because it defines the range of numbers that we can access.

With bits, computing the total range of values is actually pretty easy. We just raise 2 to the number of bits. If we had a 16 bit system, our total range of values would be 216 or 65,536 possible values. Keep this in the back of your mind when we you start playing around with Java numbers.

Introduction to Coding in Java

Perhaps the best way to start learning is to begin tinkering with numbers in Java. Unfortunately, the majority of Java tools don’t provide facilities for this because Java is compiled, not interpreted. In other words, Java has a specific layout that needs to be followed before we can begin testing. This can be quite cumbersome to new learners, so we plan to go over each piece in steps.

For now, I recommend downloading DrJava because it provides a convenient workaround. This workaround is called the interactions pane and it lets us start playing with code snippets.

Once DrJava is downloaded, let’s use the interactions pane to start doing basic math. The following are some examples of combinations that we could try:

5 + 7
5 + 7.0
3 - 4
3 - 4.0
1 / 2
1 / 2.0
6 * 6
6 * 6.0
4 % 5
4 % 5.0

Congratulations! We just finished our first Java tutorial. Stay tuned and we’ll chat about what exactly is happening with the results. In the meantime, keep experimenting. For an extra challenge, try playing around with truefalse=, and ==.

Series NavigationPrimitive Types and Variables in Java →
Advertisements

2 thoughts on “Introduction to Coding in Java

  1. Rupert Reply

    These are some of the most cohesive tutorials I’ve seen thus far. can’t wait to get started

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.