introduction to java tavaris j. thomas ph.d. bnai zion scientists division job orientation &...
TRANSCRIPT
![Page 1: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/1.jpg)
Introduction to Java
Tavaris J. Thomas Ph.D.BNAI ZION SCIENTISTS DIVISION
JOB ORIENTATION & TERMINOLOGY CLASSES
Fall 2012
![Page 2: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/2.jpg)
Contact Info
• Tavaris J. Thomas– [email protected]– www.ee.cooper.edu/~tthomas/java
• Google Group– http://groups.google.com/group/bz-java
![Page 3: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/3.jpg)
Any Programming Experience?
![Page 4: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/4.jpg)
Class Logistics
• Lectures will run every Tues 6:00pm – 9:00pm• The Cooper Union Microlab 602• Approx 4+ programming assignments.• Class Lectures will be available on the class group
page or on the class webpage • Textbook:– Core Java ,Volume 1 – Fundamentals (8th edition)– Cay S. Horstmann and Gary Cornel– ISBN: 0132354764
![Page 5: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/5.jpg)
Course Topics
• Introduction to Java• Fundamentals• Objects and Classes • Inheritance• Interfaces and Inner Classes• Deploying Applications• Debugging and Exceptions• Multithreading
![Page 6: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/6.jpg)
Week 1
• Introduction• What is Java?• Installing the Java SDK and Eclipse IDE• Language Fundamentals
![Page 7: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/7.jpg)
History of Java• Began as a Sun Microsystems project called
“Green”• James Gosling
• Intended to be used on a variety of architectures• All code is translated to the same “Virtual
Machine” code, and specific interpreters are written for the VM
• Chose to make it object-oriented like C++ instead of like Pascal
• First commercial application: applets (1995)
![Page 8: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/8.jpg)
Java’s Evolution• Java 1.0 First release• Java 1.1 Inner classes• Java 1.2-1.3 (no additions)• Java 1.4 Assertions• Java 5.0 [“1.5”] Generic classes, for each loops, variable
arguments, autoboxing, metadata, enums, static import• Java 6 Performance improvements, library enhancements
update 37• Java 7 More security and library enhancements – update 9• Java 8 TDA September 2013
![Page 9: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/9.jpg)
Versions of Java
• Java SE – Standard Edition• Java ME – Micro Edition – embedded devices
or resource constrained devices – set top boxes, blu-ray players, mobile devices
• Java EE – Enterprise Edition – For server side processing
![Page 10: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/10.jpg)
Uses of Java
• “Write Once, Run Anywhere”– Stand alone applications– Applets (java code embedded into webpages run
via we browser)– Servlets (server side Java code that interact with
clients typically using HTTP)
• Android development
![Page 11: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/11.jpg)
Programming Languages
• Interpreted languages– Perl– Python– PHP
• Compiled languages– BASIC– C/C++– Fortran– Java (to bytecode)
![Page 12: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/12.jpg)
![Page 13: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/13.jpg)
JVM Approach• Architecture neutral– Only need an implementation of JVM for the native
machine– Same Java code will run on all platforms
• Portable– The results on x86 = results on ARM = results on PPC– Caveat: don’t always fully utilize architecture capabilities
• Object oriented– Everything is a class
• Doesn’t this all mean Java is slow?– On average: slower than compiled languages– But, using just-in-time (JIT) compiler Java is fast!
![Page 14: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/14.jpg)
Grabbing Java and Eclipse
1. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Download Java SE 6 Update 37 JDK (includes the JRE)3. Install the JDK4. Install the JRE5. Download Eclipse from:
http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/heliossr2
6. Continue with installing Eclipse IDE
![Page 15: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/15.jpg)
Hello World Example
• Simplest possible program: prints one line• Will re-visit this program laterpublic class HiWorld {
public static void main(String[] args) {
System.out.println(“Hello World“);
}
}
![Page 16: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/16.jpg)
Another Example
• Uses an array of three strings and a looppublic class Greetings { public static void main(String[] args) {
String[] greeting = new String[3];greeting[0] = "Welcome to BNAI ZION";greeting[1] = “Introduction to Java";greeting[2] = “Spring 2012 ";for(String thisline : greeting)
System.out.println(thisline); }}
![Page 17: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/17.jpg)
“Hello World” In-depth
public class HiWorld {public static void main(String[] args) {// this is a comment./* so is this, but the following isa statement: */
System.out.println(“Hello World“);}
}•Java is case sensitive•public is an access modifier•Controls level of access other part of program have to this code•Everything in Java is a class – used to create building blocks•System.out is an object, calling its println method with parameter “Hello World”
![Page 18: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/18.jpg)
Class• Class is a container for the program logic that
defines the behavior of an application• Building blocks with which all Java applications
and applets are built.• Everything in a Java program must be inside a
class.• Following the keyword class is the name of the
class.• Names must begin with a letter, and after that,
they can have any combination of letters and digits.
![Page 19: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/19.jpg)
Simple Template with Javadoc/**
* This is a simple template, documented.
* @version 0.01
* @author Your Name
*/
public class ClassName{
public static void main(String[] args){
program statements;
}
}
![Page 20: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/20.jpg)
The 8 Primitive Data Types in Java
![Page 21: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/21.jpg)
Variables• For any meaningful program you need to modify data• Variables are used to store values• Operators operate on one or two variables
– Forming expressions• Declare a variable called Name of type type:
– type Name;– Example:
• String name; int a, b;
• Assigning Name a value val:– type Name=val;– Example:
• String name=“Don Knuth”; int a=3; float k=3.3;
![Page 22: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/22.jpg)
Integer Types
• Range depends on size of each type:– long (8 bytes) -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807– int (4 bytes) -2,147,483,648 to 2,147,483,647– short (2 bytes) -32,768 to 32,767– byte (1 byte) -128 to 127
• All integer types are signed (the unsigned keyword does not exist in Java)
• All integer types are the same size regardless of the device’s architecture
![Page 23: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/23.jpg)
Representing Floating Point
• Used for positive and negative numbers with fractional parts– Range and precision both depend on type– float (4 bytes) ±3.40282347*1038– double (8 bytes) ±1.79769313486231570*10308
• Float stores up to 7 fractional digits• Double stores 15 decimal digits– In general, doubles should be used instead of Float– If speed or memory are constrained, floats may be
necessary
![Page 24: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/24.jpg)
Representing Characters
• Unlike C, where char is almost always a single byte, a Java char can hold a multi-byte Unicode character
• Every char is 16 bits (2 bytes), and stores either a complete character of Unicode U+0000 to U+FFFF or half of a U+10000 to U+10FFFF character
• In most cases, String variable should be used to avoid having to worry about character types and lengths.– String pi = "\u03C0”; //π
![Page 25: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/25.jpg)
Boolean Types• Can only indicate two values , true or false• Unlike C/C++, integer 0 and 1 are not equivalent to
false and true• Avoid easy-to-create bugs. For example:if((x=1)) { statement;
} //this would not compile in Java• Must use true and false when assigning boolean
variables• No implicit conversion is possible between boolean
and other data types
![Page 26: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/26.jpg)
Strings
• Java does NOT have a built-in string type• Standard library contains class called String• Every quoted string is an instance of this class• Java strings are sequence of Unicode
characters– Example: “Java\u2122” consists of: J,a,v,a,™– Example: String e = “”; //an empty string– String planet = “Earth”;
• More later with String API
![Page 27: Introduction to Java Tavaris J. Thomas Ph.D. BNAI ZION SCIENTISTS DIVISION JOB ORIENTATION & TERMINOLOGY CLASSES Fall 2012](https://reader030.vdocuments.us/reader030/viewer/2022032803/56649e305503460f94b20f3d/html5/thumbnails/27.jpg)
Enumerated Types• Sometimes variable should only hold a value• from specific (restricted) set• Example:• Shirt size allowed to be small, medium, large
– You can, of course do:– int SMALL=1;– int MEDIUM=2;– int LARGE=3;– int shirtSize=one-of-the-above;
• But nothing prevents one from setting shirtSize=-1;– Solution: enum’s:– Enum Size {SMALL, MEDIUM, LARGE};
• Size shirtSize=Size.one-of-the-above;