Thursday, March 10, 2016

Intro to Java: Section 1.1 - Overview & Setup



What is Java?

Watch Lecture Video 1.1

Java is an extremely popular object oriented programming language which runs on billions of different devices worldwide. The language specifications were originally developed by Sun Microsystems in the early 1990s under the name Oak rather than Java. In 1995 the name of the language was changed from Oak to Java and Version 1 was released to the public. Oracle Corporation acquired Sun in 2010 and has since continued development of the language, which is now up to version 8.

Java's success can be largely attributed to the fact that it was originally developed with the intention of allowing programmers to write their code once and then run the code on any device. Sun even created a slogan for this - "write once, run anywhere." For example, a Java programmer can write Java code on a Windows PC and then run the application on Mac or Linux with little or no modification required.

Like most programming languages, Java is free to use. Anyone is allowed to download the tools necessary to write Java applications from Oracle and develop applications as they see fit. Oracle is able to allow people to use Java for free because they offer a variety of paid support services to companies whose systems were developed using Java.


Advantages of Learning Java

Learning Java makes it easier to learn other programming languages. Although coding syntax and keywords varies between different programming languages, the logic behind it remains the same.

Java is considered to be an object oriented programming (OOP) language. Object oriented programming languages allow programmers to create virtual objects to hold data in memory with their own unique values and functions. It's also possible to create derivatives or subclasses of object types. For example, a programmer could define a "shape" type object with a certain set of properties, and then develop a separate "rectangle" object that takes on the same properties of the "shape" object and adds its own unique properties and operations. This enables programmers to develop systems that are very modular and extensible. It's also easier to think about things in terms of objects rather than in more abstract terms. OOP will be discussed in greater detail later in this series.

Learning Tip

In order to gain a proper understanding of Java programming you should carefully follow along with every exercise in this series. The only way to learn how to program properly is to do it yourself. You must write and analyze the code to understand it. Reading this material and copy/pasting the code into Eclipse will not be sufficient if you want to learn anything. Even if you think you have a solid understanding of what is going on based on reading the code you should still try the exercises. Follow along with every exercise presented to you.
A Note on Ethics: It is illegal to steal code. If you use any other code in your projects, you must add a note saying where it came from. You must also ensure that the code is licensed in a way which grants you permission to use it.

Computers & Programming Languages

Before we can begin discussing how Java works, it's important to have a basic understanding of how computers and programming languages work. All computers contain the following components.

  • Input Devices
    • Input devices are pieces of hardware which allow users to interact with their computers by providing input. Examples include keyboard, mouse, microphone, webcam, drawing tablet, touch screen, etc.
  • Main Memory
    • Main Memory allows data to be read and written very quickly. This makes main memory useful for storing program and file data temporarily for the CPU to use. Main memory may also be referred to as RAM (random access memory) or primary memory. Modern computers typically use main memory in sizes between 2GB and 16GB+
  • CPU
    • The central processing unit or CPU is the hardware that performs computations according to programmer instructions given in the form of machine code. The two leading CPU PC manufacturers at the moment are Intel and AMD.
  • Operating System
    • The Operating System (OS) is the most important piece of software that runs on a computer. They are responsible for managing computer hardware and providing services to software which is installed on the computer. Linux, Windows, and Mac are examples of operating systems.
  • Aux Memory
    • Secondary memory, or auxiliary memory, stores data that is not being actively processed for later use. This may be a hard drive, DVD rom drive, usb drive, etc. It is where all the files and programs are stored. Data stored in secondary memory is considered to be persistent data because it does not need to be accessed constantly. If it does need to be accessed it's loaded into main memory.
  • Output Devices
    • Output devices are pieces of hardware which allow the user to see the results of their input. Hardware such as monitors, printers, or speakers can be considered output devices.
Sadly computer processors do not understand how to interpret English. They can only understand things in terms of machine code. Machine code is the binary code in 1s and 0s that processors use as instructions to perform basic computer operations such as arithmetic, logical comparisons, and data/memory handling. Additionally, machine code is platform specific - machine code written for one processor architecture will not work with another.  Programming in machine code is extremely tedious and time consuming, so developers created assembly languages to make programming easier. Assembly languages are low level programming languages that allow programmers to write machine code using English abbreviations rather than having to write the actual machine code.

Code written in an assembly language is translated into machine code by a program known as a compiler so that the computer can understand it. A compiler is any program that translates source code from one programming language into another.

Although assembly languages reduced the overall complexity of programming, they are still very difficult to use because programmers still have to manually write instructions tailored for the CPU. The need for simpler programming methods led to the development of high level programming languages. High level programming languages use natural language elements to make it easier to program. They also include large libraries of functions which can be accessed by programmers to speed up programming. This greatly reduces the amount of time it takes to program because you can rely on functions written by others. For example, if you want to take the square root of a number using Java you can simply use a function that was designed for taking the square root of a number. You wouldn't have to write your own code to find the square root - essentially you just tell Java you want a square root and it gives it to you. Like low level programming languages, high level programming languages must also use a compiler to convert their code into machine code which can be understood by the processor.

The basic process of creating and running a program is as follows:

  • The programmer writes an application in a high level language
  • The programmer uses a compiler which translates the source code into a lower language. Multiple compilers may be used until it is ultimately reduced to machine code.
  • The program is loaded into the computer's memory
  • The CPU processes the code from memory and begins to create some type of output
  • The output it sent to an output device or it may be stored as data on an auxiliary memory device

How Java Works

Java was intended from the beginning to work across multiple platforms (write once run anywhere) as was discussed at the beginning of this section. Sun Microsystems achieved this through the use of the Java Virtual Machine (JVM). The Java compiler does not translate Java source code directly into machine code. Instead, Java is compiled from its Java source code into bytecode. The JVM contains a bytecode verifier which acts as a safety mechanism to ensure that the code won't do anything to harm the host computer. It won't prevent the program itself from crashing if there's an error but it will prevent the program from crashing the entire computer. Once the bytecode is verified, another part of the JVM known as the just in time compiler (JIT) translates the bytecode into machine code for execution. JVMs are available for all sorts of different devices (Linux, Mac, Windows, Mobile, etc.). Sometimes the JVM comes preinstalled on an operating system and sometimes it needs to be installed manually by the user. The diagram below illustrates this process.

Review Exercise 1.1: Install Tools

In order to program using Java you will need a few tools. You need the Java Development Kit (JDK) which is a collection of tools that enable you to program in Java. It contains things such as the Java compiler, documentation, debugger, etc. It also contains the Java Runtime Environment which includes the JVM that allows you to run Java programs on your computer. The JDK is available directly from Oracle and can be downloaded here. Note that you should download the JDK by itself, without Netbeans.

The second tool you will need to program is an editor. Although it is possible to program in Java using only a basic notepad, programming is greatly simplified by an Integrated Development Environment (IDE), which provides useful tools such as source code editor with syntax highlighting, autocompletion of code, file structure/organization tools, debugger, and much more. Eclipse is the free cross platform IDE for Java that we will be using in this series. It is incredibly powerful, popular, and extensible. Eclispe is available here. You should download the standard version.

Instructions: Please download and install the Java Development Kit before continuing. You should also download Eclipse. Eclipse does not need to be installed - you simply unzip the downloaded file and launch Eclipse from the extracted folder. Watch the video lecture if you need a demonstration. 

0 comments:

Post a Comment