Java Programming/Introduction

From Wikiversity
Jump to navigation Jump to search

This page was copied from Introduction to Java.

What is Java?[edit | edit source]

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.

History of Java[edit | edit source]

See History of Java article section at Wikipedia

The java Platform[edit | edit source]

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.

Installing the Java Development Kit[edit | edit source]

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.

Downloading and Installing[edit | edit source]

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.

Note on Editions[edit | edit source]

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.

Configuring Variables[edit | edit source]

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

For Windows Users[edit | edit source]

  • 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". And For NT or Windows 10/11, 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

For Linux and UNIX[edit | edit source]

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.

For Macintosh[edit | edit source]

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.

Java programming notes for Macintosh - feedback from new Java programmers

"Hello World"[edit | edit source]

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!

Syntax and Comments[edit | edit source]

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.




Congratulations - you finished reading Intro to Java. Now, you can move on to Integer Variables

Inside the Magic[edit | edit source]

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++.)


Project: Introduction to Programming in Java
Previous: Introduction to Programming in Java — Java Programming/Introduction — Next: Introduction to Programming in Java/Integer Variables