A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.
It's fast and free.
processing...Retrieve Sign In
Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.
processing...Welcome to Webmonkey
- edit articles
- add to the code library
- design and write a tutorial
- comment on any Webmonkey article
Sign In Information Sent
Program In Java - Part 1
/skill level/
/viewed/
Contents |
Introduction
Java is a powerful programming language. In the enterprise, it remains the defacto standard. Though .NET is a competitor, the price of getting started in Java--$0.00--is nice for the newbie. Beginning in this tutorial, I will teach you everything you need to know about Java. Although there are many great languages in use on the web, Java is the most powerful one. Any Java skills you acquire can be turned into real power when it comes to building your career. Java has been embraced by corporate America and the bedroom hobbyist alike. At the end of this series, you will have the skills to take your code from the bedroom to the boardroom.
What you'll need
Though the Java world is awash with fancy tools that make the life of the experienced developer easy, you really need nothing but Notepad or Textpad to get started. In fact, it's better for the newbie to start on those types of editors. You understand what is going on better that way. You will need to go to Sun's website (Sun is the owner of Java, which is still free) and download something called the Java Development Kit or JDK. Click [here for the download page and then pick Java SE 6. That will download the Java Software Development Kit. (Also known as the JDK). Install the software and remember where you put it. If you are running on a Windows machine, go to My Computer and right mouse click on it, select properties, click on the "Advanced" tab and then find a button near the bottom called "Environment variables". Create a new System Variable called JAVA_HOME and make the path be the place where you installed Java to. For example, if you put Java in C:\dev\jdk6_rc10, then that is the entire path you should put as the path for the JAVA_HOME environment variable.
Steps
The Genesis of Java
It all started with an angry programmer.
1990 Sun Microsystems Software Engineer Patrick Naughton, age 25, was disgusted with his job at Sun. He had the impossible job of making different software APIs--from dozens of languages, platform OS’s and vendors--all work as one. It was impossible.
Patrick Naughton
Naughton announced to Sun CEO Scott McNealy that he was quitting Sun. Pat was going to join NeXT, because Sun was a mess. McNealy asked Pat to write a memo outlining his gripes. The CEO asked Pat to suggest a solution, "As if you were God."
Scott McNealy, Sun CEO at the time of Java's founding.
Formation of the "Green Project"
Jan 1991
The array of standards spurred the formation of the "Green Project." Its goal was making Consumer Electronics devices talk to each other.
Since VCRs, Laser Disc Players and Stereos were all made with different CPUs, they all needed special programming.
James Gosling, then age 36, was asked to find a programming language for the project. Gosling, who had left IBM in 1984 to join Sun, first chose C++. But he soon gave up on C++, which was incapable of doing what he wanted. So, he started to modify C++, (which is a direct descendant of the C programming language).
James Gosling, father of Java.
Soon, Gosling was writing a new language, which he named "Oak" after the tree outside his window.
Oak to had to be:
• Small to work on Consumer electronics,
• Platform independent, to avoid hassles like the ones Naughton encountered,
• an Interpreted language,
• Object Oriented,
• Reliable--which made him remove aspects of C++:
i.) No Multiple Inheritance--he used interfaces instead
ii.) No Operator Overloading
iii.) No Manual Memory allocation and dealloc
iv.) No Pointers--no pointer arithmetic
v.) No assignment in conditionals (== vs =)
and add things C++ lacked:
i.) Implicit Garbage Collection--no memory leaks
ii.) Data Structures only in Objects
iii.) Built in Security.
Star7, the First Device using Java.
Demo of *7, Programmed in Oak
3 Sept 1992
This was the prototype of the first device to use the Oak programming language. The "Star7" also featured the debut of "Duke," the Java mascot. An early applet showed Duke doing cartwheels across the screen.
Duke, the offical Java mascot.
Java Catches Fire 23 Mar 1995 Although Java had not yet been officially released, it was spreading like wildfire among developers. Then, something very lucky happened...
Front page of the "San Jose Mercury News", carrying the announcement that Netscape Navigator 2.0 would carry Java.
•Netscape Navigator 2.0 23 May 1995 Two months later, at the SunWorld conference, Marc Andreessen stepped on stage and announced that "Java is real, and it will be incorporated into Netscape Navigator 2.0." At this moment, Sun’s entire Java team only numbered 30 people.
Marc Andreason, Founder of Netscape.
Java's Major Advantage over C & C++
• Because pointers were a major source of bugs in C and C++, Gosling omitted pointers entirely from Java.
• Actually, pointers are still an important part of the language--all objects are referenced by pointers--but the language handles them, not the programmer.
(Thus, it has been said that...) "Java is C without the Guns and Knives"
Java Architecture
• By now, Java itself has matured into its 3rd version, named Java 2. This course is based on Java 2. The most current is Java 2 (1.5.1) • Java is Object-Oriented--that means everything in the language behaves like an object. • What exactly that means will be explained in the coming tutorials.
---
Java’s Architecture comes from four separate but intertwined technologies: • the Java Programming Language • the Java class file format • the Java API, or Application Programming Interface • the Java Virtual Machine
Source programs are written in the Java Programming Language.
All procedural code falls within methods.
Programs are compiled into Java class files.
Classes run in the Java Virtual Machine.
• When a Java program runs, it is assisted by other classes in the Java the Application Programming Interface, or API.
This diagram gives you an idea of how the Java Virtual Machine is built.
The Java Virtual Machine
• The Java Platform is unique, because it can work without modification on any platform, on any operating system, if that platform has a "Java Virtual Machine."
Comparison of a typical Procedural Program with a Java Program:
• In a typical C program, the source code is compiled into a native machine language module that consists of 1's and 0's.
How "C" language is related to its host platform.
• The machine language is specifically tailored to one OS, be it Wintel, Mac, UNIX or MVS. • Therefore, it is impossible for one object module to be portable between platforms.
Java Bytecode
In contrast to conventional programming languages, a Java program is not compiled into native machine language. • Instead, Java makes bytecode. • Bytecode is the result of a "compile", but the compile results in an intermediate form that stops short of native machine-specific code.
• Instead of making a machine language native code for each particular OS, Java makes a single, universal bytecode module that feeds into any Java Virtual Machine (JVM).
• Each OS has its own different implementation of the Java Virtual Machine.
• The JVM sets up its own world within your RAM. • The JVM creates an internal software-only sub-computer within the OS. • The bytecode talks to the JVM, and the JVM talks to the Operating System.
• Thus, you get the Holy Grail of software reuse:
"Write Once, Run Anywhere"
How Java bytecode gets executed. (And you can see why Bill Gates is not in love with Java!)
• The Virtual Machine interprets the bytecode one instruction at a time, and translates it into native machine code.
• You compile your program once into bytecode, but it is interpreted anew every time it runs.
Security and the "Sandbox"
C and C++ are famous for speed.
• One reason they are fast is because C and C++ don’t do things like checking the bounds of arrays.
• In C or C++, a program can walk off the edge of an array and invade the memory space beyond.
• Hackers love that about C and C++.
• Another weakness of C/C++, that is a favorite among Hackers, is the Buffer Overflow.
• In this attack, the Hacker floods too much data into a buffer and whatever overflows it is turned loose on the system.
• Java solves these problems
• How Java Combats malicious code:
Java checks array boundaries
Java halts Buffer Overflows
Java has Garbage collection to get rid of objects that are no longer used.
Java’s compiler checks to make sure the code is safe before it runs.
• Gosling built security into Java, using a concept known as the "Sandbox."
Java Sandbox, circa Java version 1.2.
Steps To Writing A Java Program:
1.) Write it in a Text Editor
2.) Compiler creates bytecode
3.) The "Class loader" places the .class file in memory.
4.) The "Bytecode Verifier" makes sure the code adheres to Java's security rules.
5.) The JVM Interpreter reads bytecode and makes platform native code.
• You see, preventing problems is a major design consideration in Java.
• This idea led to the most import aspect of Java: Object Orientation.
• Object Orientation protects data and lets a program do only what is explicitly permitted.
• You could say Java is pessimistic.
• In Java, Object Orientation is so pervasive that it’s nearly impossible to write a strictly procedural program in the language.
Objects In Java
• Objects are reusable components. • In Java, everything must be run from a "class" file. This "class" contains bytecode. • Java source code has the extension Xxx.java
• If I write a Java program called:
Hello.java
then, when compiled, this program will be called:
Hello.class
• A class object is compiled Java code that contains its own data variables, called members, and sections of procedural code called methods. If you have programmed in COBOL, a method is like a paragraph you perform.
If you have programmed in C or C++, a method is like a function your program calls.
• The combination of the data variables and the methods that are used to read, write or modify those variables is called a class.
• Java has a rich collection of Class Libraries. • These are also known as the Java API or Application Programming Interface. • To program in Java, you must i.) Learn the Language, and ii.) Learn the Class Libraries.
• These class libraries greatly simplify your job as a Java programmer. • They help you to write complex programs quickly. • To master Java, you must master these class libraries.
• You have created a Java program called: Hello.java • To compile it, you run the JDK supplied utility called: javac
C:\javac Hello.java
If this was successful, a file called: Hello.class will be produced.
First Java Program
• The two largest varieties of Java programs:
Applications
Applets
• A Java Application is a free-standing program that is capable of running directly in the Java Virtual Machine.
• A Java Applet is a mini-program that is much more limited in its abilities. An Applet can only run within the context of an HTML browser.
This is the class name. Every single bit of code in Java must sit in curly brackets. Class names are capitalized. Words within the name are also capitalized. This Java program must be saved in a file with the exact same name--matching the upper case--as you see in blue above:
HelloWorld.java
The double slashes denote a "C++"-style comment. Everything on the line after the double slashes is ignored by the compiler.
Now our Application is complete. We have added the method "main". All methods are lower case. main is a special method--it actually runs the program. In any application, you are always guaranteed that method main will run.
What a successful compilation looks like.
• To run your compiled Application, you enter lowercase java HelloWorld on the command line. • Notice, the ".class" extension is omitted.
Hello World!
Look for Java Programming Part 2 (of 32)
- This page was last modified 17:15, 14 October 2008.
Special Offer For Webmonkey Users
WIRED magazine:
The first word on how technology is changing our world.







