Introduction to Programming in Java

Site: Saylor Academy
Course: CS102: Introduction to Computer Science II
Book: Introduction to Programming in Java
Printed by: Guest user
Date: Saturday, April 20, 2024, 11:37 AM

Description

This page extends on the previous resource to delve deeper into Java itself, relating its use to its history.

1. What is Java?

Java is an object-oriented, cross platform, multi-purpose programming language produced by Sun Microsystems. First released in 1995, it was developed to be a machine independent web technology. It was based on C and C++ syntax to make it easy for programmers from those communities to learn. Since then, it has earned a prominent place in the world of computer programming.

Java has many characteristics that have contributed to its popularity:

    • Platform independence - Many languages are compatible with only one platform. Java was specifically designed so that it would run on any computer, regardless if it was running Windows, Linux, Mac, Unix or any of the other operating systems.
    • Simple and easy to use - Java's creators tried to design it so code could be written efficiently and easily.
    • Multi-functional - Java can produce many applications from command-line programs to applets to Swing windows (basically, sophisticated graphical user interfaces).

Java does have some drawbacks. Since it has automated garbage collection, it can tend to use more memory than other similar languages. There are often implementation differences on different platforms, which have led to Java being described as a "write once, test everywhere" system. Lastly, since it uses an abstract "virtual machine", a generic Java program doesn't have access to the Native API's on a system directly. None of these issues are fatal, but it can mean that Java isn't an appropriate choice for a particular piece of software.


Source: WikiUniversity, https://en.wikiversity.org/wiki/Java_Programming/Introduction
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License.

2. History of Java

James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time. The language was initially called Oak after an oak tree that stood outside Gosling's office. Later the project went by the name Green and was finally renamed Java, from Java coffee, a type of coffee from Indonesia. Gosling designed Java with a C/C++-style syntax that system and application programmers would find familiar.

Sun Microsystems released the first public implementation as Java 1.0 in 1996. It promised Write Once, Run Anywhere (WORA) functionality, providing no-cost run-times on popular platforms. Fairly secure and featuring configurable security, it allowed network- and file-access restrictions. Major web browsers soon incorporated the ability to run Java applets within web pages, and Java quickly became popular. The Java 1.0 compiler was re-written in Java by Arthur van Hoff to comply strictly with the Java 1.0 language specification. With the advent of Java 2 (released initially as J2SE 1.2 in December 1998 – 1999), new versions had multiple configurations built for different types of platforms. J2EE included technologies and APIs for enterprise applications typically run in server environments, while J2ME featured APIs optimized for mobile applications. The desktop version was renamed J2SE. In 2006, for marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.

In 1997, Sun Microsystems approached the ISO/IEC JTC 1 standards body and later the Ecma International to formalize Java, but it soon withdrew from the process. Java remains a de facto standard, controlled through the Java Community Process. At one time, Sun made most of its Java implementations available without charge, despite their proprietary software status. Sun generated revenue from Java through the selling of licenses for specialized products such as the Java Enterprise System.

On November 13, 2006, Sun released much of its Java virtual machine (JVM) as free and open-source software (FOSS), under the terms of the GNU General Public License (GPL). On May 8, 2007, Sun finished the process, making all of its JVM's core code available under free software/open-source distribution terms, aside from a small portion of code to which Sun did not hold the copyright.

Sun's vice-president Rich Green said that Sun's ideal role with regard to Java was as an evangelist. Following Oracle Corporation's acquisition of Sun Microsystems in 2009–10, Oracle has described itself as the steward of Java technology with a relentless commitment to fostering a community of participation and transparency. This did not prevent Oracle from filing a lawsuit against Google shortly after that for using Java inside the Android SDK.

On April 2, 2010, James Gosling resigned from Oracle.

In January 2016, Oracle announced that Java run-time environments based on JDK 9 will discontinue the browser plugin.

Java software runs on everything from laptops to data centers, game consoles to scientific supercomputers.


Principles

There were five primary goals in the creation of the Java language:

  1. It must be simple, object-oriented, and familiar.
  2. It must be robust and secure.
  3. It must be architecture-neutral and portable.
  4. It must execute with high performance.
  5. It must be interpreted, threaded, and dynamic.


Versions

As of September 2020, Java 8 and 11 are supported as Long Term Support (LTS) versions, and one later non-LTS version is supported. Major release versions of Java, along with their release dates:

Version Date
JDK Beta 1995
JDK1.0 January 23, 1996
JDK 1.1 February 19, 1997
J2SE 1.2 December 8, 1998
J2SE 1.3 May 8, 2000
J2SE 1.4 February 6, 2002
J2SE 5.0 September 30, 2004
Java SE 6 December 11, 2006
Java SE 7 July 28, 2011
Java SE 8 March 18, 2014
Java SE 9 September 21, 2017
Java SE 10 March 20, 2018
Java SE 11 September 25, 2018
Java SE 12 March 19, 2019
Java SE 13 September 17, 2019
Java SE 14 March 17, 2020
Java SE 15 September 15, 2020
Java SE 16 March 16, 2021

3. The Java Platform

One thing that distinguished Java from some other languages is its ability to run the same compiled code across multiple operating systems.

In other languages, the source code (code that is written by the programmer), is compiled by a compiler into an executable file. This file is in machine language, and is intended for a single operating system/processor combination, so the programmer would have to re-compile the program seperately for each new operating system/processor combination.

Java is different in that it does not compile the code directly into machine language code. Compilation creates bytecode out of the source code. Bytecode generally looks something like this:

 a7 f4 73 5a 1b 92 7d

When the code is run by the user, it is processed by something called the Java Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. It goes through the bytecode and runs it. There are different versions of the JVM that are compatible with each OS and can run the same code. There is virtually no difference for the end-user, but this makes it a lot easier for programmers doing software development.

4. Installing the Java Development Kit

Before installing the Java Development Kit (JDK), you should probably know what it is. It is distributed by Oracle. It contains the core libraries and compiler required to develop Java. The JDK should not be confused with the JRE (Java Runtime Environment). The JRE is a JVM for running, as opposed to compiling, Java programs.

4.1. Downloading and Installing

To download the JDK, go to http://www.oracle.com/technetwork/java/javase/downloads/index.html. Click on "JDK with NetBeans Bundle". Follow the instructions for downloading the JDK installation file.

Windows: If you are running Windows, simply run the executable file and follow the installation instructions.

Unix, Solaris, or Linux: For Linux and Unix, download the "jdk1 6.0" for Linux systems. Save the downloaded file in any drive. Once you have saved the file, extract it to a place that you can remember, by using Terminal or by double clicking on the file. When you have finished extracting the file, copy the JDK 1.6.0 folder and paste it in the usr/local (To paste to the usr/local directory, you have to be in root) so that every user can use the java files. You can delete the downloaded zip file so that it doesn't take up space on your drive.

Macintosh: The latest available JDK is automatically installed by the operating system. Because Java for Macintosh is developed and maintained by Apple, in coordination with Sun, the current version on the Macintosh may not be the current version that is available from Sun.

4.2. Note on Editions

The JDK comes in three editions.

  • Java Standard Edition (JSE) – This version is the basic platform for Java. The course will focus on this edition.
  • Java Enterprise Edition (JEE) – This edition is mainly for developing and running distributed multitier architecture Java applications, based largely on modular software components running on an application server. We will not be covering this version in the course.
  • Java Micro Edition (JME) – This edition is primarily for developing programs to run on consumer applicances, such as PDAs and cell phones.

4.3. Configuring Variables

Before writing code, it is recommended that you set the Path variable on your system so you can compile your code more easily.


4.4. For Windows Users

  • From the Control Panel, double click "System" (System and Maintenance in Vista)
  • For Windows 7 or Vista, click on "System," "Advanced System Settings" on the left, and then on "Environment Variables".
  • For XP and 2000, click on the "Advanced" tab and click on "Environment Variables" For NT, click on the "Environment" tab.
  • Select the Path variable and click "Edit"
  • Add the path to the bin directory of where Java is installed on your hard drive. It should probably be: C:\Program Files\Java\jdk1.6.0_20\bin unless you changed it during installation.
  • Click OK

4.5. For Linux and UNIX

One way to set your path in Linux/Unix is to add a path export to your bash profile.

  • In order to do this, first open your bash profile in a text editor. For example,

pico ~/.bash_profile

  • Then add this line:

export PATH=$PATH:/usr/local/jdk/bin

Note that the path to the java directory "/usr/local/jdk/bin" may be different on your machine.

  • Restart your shell.

4.6. For Macintosh

Apple sets everything up for you. Sit back and relax.

The only drawback is that because Apple handles development and maintenance of Java on the Mac, there is usually a delay from the time that a new version is released by Sun and the time that the new version is released on the Mac. Also, getting the latest version sometimes requires an operating system upgrade.

Oh well, you can't have everything.


5. "Hello World"

Anytime you learn a computer programming language, it is tradition that the first program you write should be to make your computer say, "Hello World". This is a small feat, but is a good opportunity to make sure that you installed the JDK properly.

The Java compiler reads basic text files. Open up a text editor like Notepad (Don't use a complex program like Word for this). Type this code, remembering that Java is case-sensitive:

public class HelloWorld 
{
   public static void main (String[] args) 
   {
      System.out.println("Hello World!");
   }
}

Save this file as HelloWorld.java. Start your system's command line and navigate to the folder that you saved HelloWorld.java to. Type javac HelloWorld.java. This runs the java compiler, javac, and creates a class file, which contains the bytecode for the application. Next type java -cp . HelloWorld. This runs the class file that was just created by the compiler. Your console should print:

Hello World!

5.1. Syntax and Comments

As mentioned above, Java is case-sensitive, meaning that if a variable name is "day" it cannot be referred to as "Day" later in the program. In addition, semicolons must end each statement in the code.

Programmers also use comments to insert statements into their code that the computer ignores. Comments can be used to help explain code so that other programmers can understand it or the original writer of the code can remember what their code does. Java has several types of comments.

System.out.println("Hello World!");//This is an inline comment


/*
 *This is a block comment
 */
System.out.println("Hello World!");


/**
 *This is a Javadoc comment
 */
public static void main (String[] args) 
{
  System.out.println("Hello World!");
}


Go to the Wikipedia article for Javadoc for more information on that type of comment.

5.2. Inside the Magic

The "Hello World" program has become a de-facto standard with computer programmers for a first program in any language. The main advantage is that it is as simple as a program can get - when considering what it does. Depending upon the language and/or environment though, it may be necessary for some set-up code and/or syntactic glue.

In the program shown above, there are two pieces of syntactic glue.

The first part, public class HelloWorld, and the accompanying matched pair of curly braces, declare a Class construct. At this point, we could try and explain what a class is, but for someone who is genuinely new to programming, the explanation would probably be more of a distraction than a help. For now, we will settle for a simple universal truth - all Java code must be contained inside a Class construct.

The second part, public static void main(String[] args) and the accompanying matched pair of curly braces declare a Method construct. In particular, they declare a special type of method that Java can use as the entry point for a program. A method is a somewhat simpler construct than a class, but for now we will just describe it as a group of runnable code. A main method is required for each and every Java program. (Methods are also known as 'functions' in some other programming languages such as C++).