An Introduction to the Java Programming Language History of Java



Similar documents
Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Interview Questions and Answers

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Crash Course in Java

Chapter 1 Java Program Design and Development

Introduction to Java

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

C# and Other Languages

java Features Version April 19, 2013 by Thorsten Kracht

An Overview of Java. overview-1

Java Programming Fundamentals

1001ICT Introduction To Programming Lecture Notes

Contents. 9-1 Copyright (c) N. Afshartous

Java Crash Course Part I

Installing Java. Table of contents

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

CS 106 Introduction to Computer Science I

Java applets. SwIG Jing He

6.1. Example: A Tip Calculator 6-1

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Java CPD (I) Frans Coenen Department of Computer Science

public static void main(string[] args) { System.out.println("hello, world"); } }

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

Java programming for C/C++ developers

Pemrograman Dasar. Basic Elements Of Java

02 B The Java Virtual Machine

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Chapter 1 Fundamentals of Java Programming

Reading and Understanding Java s API Documentation

Programming Languages

AP Computer Science Java Subset

An Incomplete C++ Primer. University of Wyoming MA 5310

Sources: On the Web: Slides will be available on:

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Chapter 1. Introduction to Computers, Programs, and Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

How to Install Java onto your system

Introduction to programming

INTRODUCTION TO JAVA PROGRAMMING LANGUAGE

CS 209 Programming in Java #1

C++ INTERVIEW QUESTIONS

Tutorial: Getting Started

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

C Compiler Targeting the Java Virtual Machine

Habanero Extreme Scale Software Research Project

Computing Concepts with Java Essentials

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Lecture 5: Java Fundamentals III

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Moving from CS 61A Scheme to CS 61B Java

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Lab Experience 17. Programming Language Translation

The C Programming Language course syllabus associate level

Java (12 Weeks) Introduction to Java Programming Language

Part I. Multiple Choice Questions (2 points each):

1. Overview of the Java Language

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS506 Web Design and Development Solved Online Quiz No. 01

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Under the Hood: The Java Virtual Machine. Lecture 24 CS 2110 Fall 2011

Glossary of Object Oriented Terms

Java from a C perspective. Plan

Simple Java Applications

Introduction to Java Applets (Deitel chapter 3)

CSC 551: Web Programming. Spring 2004

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Install Java Development Kit (JDK) 1.8

Fundamentals of Java Programming

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

Performance Improvement In Java Application

Description of Class Mutation Mutation Operators for Java

Explain the relationship between a class and an object. Which is general and which is specific?

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Java History. Java History (cont'd)

2 Introduction to Java. Introduction to Programming 1 1

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Introduction to Data Structures

SE 450 Object-Oriented Software Development. Requirements. Topics. Textbooks. Prerequisite: CSC 416

More on Objects and Classes

Computer Programming I

INPUT AND OUTPUT STREAMS

TYLER JUNIOR COLLEGE School of Continuing Studies 1530 SSW Loop 323 Tyler, TX

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Chapter 7D The Java Virtual Machine

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Serialization in Java (Binary and XML)

Transcription:

An Introduction to the Java Programming Language History of Java In 1991, a group of Sun Microsystems engineers led by James Gosling decided to develop a language for consumer devices (cable boxes, etc.). They wanted the language to be small and use efficient code since these devices do not have powerful CPUs. They also wanted the language to be hardware independent since different manufacturers would use different CPUs. The project was code-named Green. These conditions led them to decide to compile the code to an intermediate machine-like code for an imaginary CPU called a virtual machine. (Actually, there is a real CPU that implements this virtual CPU now.) This intermediate code (called bytecode) is completely hardware independent. Programs are run by an interpreter that converts the bytecode to the appropriate native machine code. Thus, once the interpreter has been ported to a computer, it can run any bytecoded program. Sun uses UNIX for their computers, so the developers based their new language on C++. They picked C++ and not C because they wanted the language to be object-oriented. The original name of the language was Oak. However, they soon discovered that there was already a programming language called Oak, so they changed the name to Java. The Green project had a lot of trouble getting others interested in Java for smart devices. It was not until they decided to shift gears and market Java as a language for web applications that interest in Java took off. Many of the advantages that Java has for smart devices are even bigger advantages on the web. Currently, there are two versions of Java. The original version of Java is 1.0. At the moment (Nov. 1997), most browsers only support this version. The newer version is 1.1 (in addition 1.2 is in beta). Only MS Internet Explorer 4.0 and Sun's HotJava browsers currently support it. The biggest differences in the two versions are in the massive Java class libraries. Unfortunately, Java 1.1 applets will not run on web browsers that do not support 1.1. (However, it is still possible to create 1.0 applets with Java 1.1 development systems.) Basics of Java

Applications There are 2 basic types of Java applications: standalone These run as a normal program on the computer. They may be a simple console application or a windowed application. These programs have the same capabilities of any program on the system. For example, they may read and write files. Just as for other languages, it is easily to write a Java console program than a windowed program. So despite the leanings of the majority of Java books, the place to start Java programming is a standalone console program, not an applet! applets These run inside a web browser. They must be windowed and have limited power. They run in a restricted JVM (Java Virtual Machine) called the sandbox from which file I/O and printing are impossible. (There are ways for applets to be given more power.) Development Tools There are many development tools for Java: Sun's JDK Sun's Java Development Kit has two big advantages: 1. It is the most up to date. 2. It is free! Its main disadvantage is that it only includes command line tools, no IDE. Many Java books include this on a CD-ROM. Borland's JBuilder It contains an IDE and supports Java 1.1 MS Visual J++ It contains an IDE, but to my knowledge does not yet support Java 1.1 Symantec's Visual Cafe It contains an IDE, but no Java 1.1 support yet. Compiling and Running a Java Standalone Application Hello, Java program Here is a short Java program: /* A simple Java program

public class Hello public static void main( String args[] ) System.out.println("Hello, Java"); Creating a Java source file Java source files must end in an.java extension. The root name must be the same as the name of the one public class in the source file. In the program above, the class is named Hello and thus, the file must be named Hello.java (Yes, case is important!). Just as for other languages, any text editor can be used to save the text of the program into a text file. Compiling a Java source file Sun's JDK includes a Java compiler named javac. To compile the above Java program one would type: javac Hello.java If successful, this creates a file named Hello.class If not successful, it prints out error messages like other compilers. Running a Java program To run a standalone program, Sun's JDK provides a Java interpreter called java. To run the.class file created above, type: java Hello Note that.class is not specified! The output of running the above program is simply: Hello, Java Java Programming Classes and objects Java is an object-oriented language (like C++). An object is an abstract thing in a program. Generally, objects are not completely different from each other and can be classified into like groups. The group an object belongs to is called a class. Objects in the same class share two attributes: 1. A state space (i.e., the set of all possible states of the object)

speak). Consider the Vector class that the Java class library provides. A Vector object is basically an array that grow or shrink in size dynamically. It's state is defined by: 1. The elements stored in their given order 2. The number of elements stored It's methods include: addelement() Adds an element to end of the vector increasing its size by 1 insertelementat() Adds an element at a specified position in the vector. The elements above are shifted up one position and the size is increased by 1. setelementat() Stores an element at a specified position in the vector. The previous element value is lost. removeelementat() Removes an element at a specified position. The elements above are shifted down and the size is decreased by 1. elementat() Returns the element at a specified position. size() Returns the current size of the vector. If v is an object of type Vector, then a method is invoked on v by the following syntax: v.method-name(method-arguments); For example, to add another object x to the end of v use: v.addelement(x); In an object-oriented language, one looks at the statement above as a request for the v object to add the object x to the end. That is, invoking a method sends a message to the object being acted on. The message asks the object to perform some operation. Primitive types Java also supports some primitive types that are not classes. These types are similar to the primitive types of C: byte a single byte (8-bit) signed integer char a two-byte (16-bit) Unicode character short a two-byte (16-bit) signed integer

int long a four-byte (32-bit) signed integer an eight-byte (64-bit) signed integer float a four-byte floating point number double an eight-byte floating point number boolean a type of variable that may be either true or false Defining a class The general form of a class definition is: public class class-name /* class state definitions /* class method definitions Here's an actual example that creates a Queue class: public class Queue /* * the state of the Queue is represented by an internal vector instance * (The private indicates that this part of the Queue is only * accessible by the methods of the Queue class. This implementation * hiding is known as encapsulation.) private Vector v; /* * This is a constructor - it is used to create a new instance of * a Queue. The public indicates that this method is available for * use by any class. public Queue( ) v = new Vector(); // construct the internal vector object /* * This method adds an element to the queue public void enque( Object obj ) v.addelement(obj); // add to end of internal vector

/* * This method removes and returns an element from the queue public Object deque( ) if ( v.size() > 0 ) Object obj = v.elementat(0); // read object from front of vector v.removeelementat(0); // remove object from front of vector return obj; // return object else return null; // if queue empty, return special null va /* * This method returns the number of elements in the queue public int size() return v.size(); // return size of internal vector Using classes To create an instance of a class, the instance must be constructed by a special method called a constructor. Constructor methods always have the same name as the class. To create a queue, one would type: Queue q = new Queue(); The new keyword says to create a new object. The instance variable q can be used to refer to this object. Thus, the statement: q.enque("one"); says to enque the string "one" in the Queue referred to by q. It is often stated that Java does not have pointers and technically this is true. However, class reference variables act like pointers (or really more like reference variables in C++). Assigning class instance variables do not create new instances. For example: Queue q1 = new Queue(); Queue q2 = q1; q1.enque("one"); q2.enque("two"); // q1 refers to created Queue object // both q1 and q2 refer to *same* object!

System.out.println((String) q2.deque() ); // prints out "one", not "two" Non-class instance variables (like int variables) work just as in C, assignment does copy values of these. These rules have important consequences for parameters of methods. For example, consider the following method call and code: // method call int x = 5; Queue q = new Queue(); method(x,q);... // method code void method( int xp, Queue qp ) xp++; // changes local var xp, not x! qp.enque("word"); // adds "word" to the single object referenced by // q and qp Thus, primitive type instances are always passed by value and class instances are always passed by reference. Static methods and state Normally a method acts on a single object (or instance) of a class. However, it is possible to define a method that acts on a class itself instead of a particular instance of the class. These classes are declared static. The static keyword can also be used on the state variables of the class with a similar result. The variable becomes a property of the class itself and not any particular instance. Static methods can only access the static variables of its class, not the non-static variables. Here's a very simple example: public class StaticTest public int ns; // ns is a normal non-static variable public static int s; // s is a static variable public void ns_method() // normal non-static method ns = 3; // can access ns s = 5; // and s public static void s_method() // static method

s = 7; // can *only* access static s! Since static methods and instances are properties of a class and not an instance of a class, they are invoked on the class itself. For example, to call the static method above, one would type: StaticTest.s_method(); Now we are ready to understand our initial Java program: public class Hello public static void main( String args[] ) System.out.println("Hello, Java"); System is a name of a class in the Java class library. System.out refers to a static object in the class that represents the console screen. The println() method is used to tell System.out to display a string on the console. Note that main() is a static method. Object-Oriented Programming and Inheritance The preceding section looked at classes from the purely Abstract Data Type view. The object-oriented paradigm goes further by looking at relationships between different classes. Often different classes have an IS-A relationship. This type of relationship exists when one class is a more specialized version of another. For example, what if one needed a searchable vector class. The Vector class does not include a method to search for objects inside an instance. However, one could create a new vector class that performed just like the old Vector class, but directly supported searching. The wrong way to do this is to create a new SearchableVector class from scratch that contains a internal Vector (like how the Queue was implemented). This would require one to re-specify each of Vector's methods for SearchableVector (there are many more methods than the one's specified above!). The right way is to use Java's inheritance mechanism. The SearchableVector class is an extension of the Vector class. Or, in other words, a SearchableVector IS-A Vector. A SearchableVector can do anything that a Vector can do, plus more. Java allows one to simply define a new class that extends the features of an existing class. The new class automatically has all the state and methods of the existing class. The syntax is:

class subclass extends superclass // new features of subclass where subclass refers to the new class and superclass refers to the existing class being extended. All classes in Java are extensions either directly or indirectly from the Object class. Here is the definition of the SearchableVector class: public class SearchableVector extends Vector /* search returns the index of the object if found, * else -1 public int search( Object obj ) for( int i = 0; i < size(); i++ ) if ( elementat(i).equals(obj) ) return i; return -1; In the limit of the for loop, the size() method for a Vector is called. No object is specified since search() itself is a method and so size() acts on the same object that search() does. The object a method acts on can be referred to by the this keyword. The size() call in the for loop could be replaced with this.size() with the same effect. The if compares the element at index i with the object searched for. The elementat(i) call returns the object at index i. The equals() method of this returned object is then called to compare it with the object searched for. The equals method of a class compares the values of two instances and returns true or false based on whether the values are the same. The C-like == operator does not compare values of classes in Java. It only looks at whether the two variables compared are referring to the same object. (Remember object variables are really references!). Where to Go from Here Sample code You can download the code from my examples to try out. Hello.java Queue.java SearchableVector.java Vtest.java - Test program that uses the two classes above.

Books on Java There are lots of Java books available. Most of them are not very good! Here are some of my favorites. Core Java 1.1: Volume 1 - Fundamentals by Cay S. Horstmann and Gary Cornell, Pentice-Hall. (This will be the textbook for the Spring 1998 Java class.) Java in a Nutshell, 2nd Edition by David Flanagan. O'Reilly. (A very good reference!) 1997 Paul Carter. Send corrections/comments to pcarter@dogbert.comsc.ucok.edu