Your SlideShare is downloading. ×
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Java i lecture_1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java i lecture_1

70

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
70
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java I--Copyright © 2000-2004 Tom Hunter
  • 2. Java I--Copyright © 2000-2004 Tom Hunter
  • 3. Java I--Copyright © 2000-2004 Tom HunterChapter 1Introduction to Computers,the Internet and the Web
  • 4. Java I--Copyright © 2000-2004 Tom Hunter• It all started with an angry programmer.1990Sun Microsystems SoftwareEngineer Patrick Naughton,age 25, was disgusted withhis job at Sun. He had the impossible job ofmaking different software APIs--fromdozens of languages, platform OS’s andvendors--all work as one. It was impossible.The Genesis of Java
  • 5. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of JavaNaughton announced to CEOScott McNealy that he was quitting Sun.Pat was going to join NeXT,because Sun was a mess.McNealy asked Pat to write a memooutlining his gripes.The CEO asked Pat to suggest asolution, “As if you were God.”
  • 6. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of Java• Formation of the “Green Project”Jan 1991The array of standards spurred theformation of the “Green Project.” Its goalwas making Consumer Electronics devicestalk to each other.Since VCRs, Laser Disc Players andStereos were all made with different CPUs,they all needed special programming.
  • 7. Java I--Copyright © 2000-2004 Tom Hunter• James Gosling, then age 36, was asked to find aprogramming language for the project.Gosling, who had leftIBM in 1984 to join Sun,first chose C++. But hesoon gave up on C++,which was incapable of doingwhat he wanted. So, he started tomodify C++, (which is adirect descendant of the C programminglanguage).The Genesis of Java
  • 8. Java I--Copyright © 2000-2004 Tom Hunter• Soon, Gosling was writing a new language,which he named “Oak” after the tree outsidehis window.Oak to had to be:• Small to work on Consumer electronics,• Platform independent, to avoid hassles like theones Naughton encountered,• an Interpreted language,• Object Oriented,The Genesis of Java
  • 9. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of Java• Reliable--which made him remove aspects of C++:i.) No Multiple Inheritance--he used interfacesinsteadii.) No Operator Overloadingiii.) No Manual Memory allocation and deallociv.) No Pointers--no pointer arithmeticv.) No assignment in conditionals (== vs = )and add things C++ lacked:i.) Implicit Garbage Collection--no memory leaksii.) Data Structures only in Objectsiii.) Built in Security.
  • 10. Java I--Copyright © 2000-2004 Tom Hunter• Demo of *7, Programmed in Oak3 Sept 1992This was the prototype of thefirst device to use the Oakprogramming language.The “Star7” also featured thedebut of “Duke,” the Javamascot. An early appletshowed Duke doing cartwheelsacross the screen.The Genesis of Java
  • 11. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of Java• Oak becomes Java.Jan 1995By this time, the Internet had taken off.Bill Joy, one of Sun’s founders, realized thatthe needs of the Web [ reliability, platformindependence, security ] exactly matched thecharacteristics of Oak, which had just beenrenamed Java.
  • 12. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of Java• Java Catches Fire23 Mar 1995Although Java had not yet beenofficially released, it was spreading likewildfire among developers.Then, something very lucky happened...
  • 13. Java I--Copyright © 2000-2004 Tom Hunter
  • 14. Java I--Copyright © 2000-2004 Tom HunterThe Genesis of Java•Netscape Navigator 2.023 May 1995Two months later, at the SunWorldconference, Marc Andreessen stepped onstage and announced that “Java is real, and itwill be incorporated into Netscape Navigator2.0.”At this moment, Sun’s entire Java team onlynumbered 30 people.
  • 15. Java I--Copyright © 2000-2004 Tom HunterJava’sMajor Advantageover C & C++
  • 16. Java I--Copyright © 2000-2004 Tom Hunter• Because pointers were a major source ofbugs in C and C++, Gosling omitted pointersentirely from Java.• Actually, pointers are still an important partof the language--all objects are referenced bypointers--but the language handles them, notthe programmer.Java’s Major Advantage over C & C++
  • 17. Java I--Copyright © 2000-2004 Tom Hunter“Java is C withoutthe Guns andKnives.”Thus, it has been said that...Java’s Origins in C & C++
  • 18. Java I--Copyright © 2000-2004 Tom Hunter• By now, Java itself has matured into its 3rdversion, named Java 2. This course is basedon Java 2. The most current is Java 2 (1.5.1)• Java is Object-Oriented--that meanseverything in the language behaves like anobject.• What exactly that means will be explainedin the coming weeks.Java Architecture
  • 19. Java I--Copyright © 2000-2004 Tom HunterJava ArchitectureJava’s Architecture comes from fourseparate but intertwined technologies:• the Java Programming Language• the Java class file format• the Java API, or Application Programming Interface• the Java Virtual Machine
  • 20. Java I--Copyright © 2000-2004 Tom HunterJava ArchitectureSource programs are written in the JavaProgramming Language.All procedural code falls within methods.Programs are compiled into Java class files.Classes run in the Java Virtual Machine.
  • 21. Java I--Copyright © 2000-2004 Tom HunterJava Architecture• When a Java program runs,it is assisted by other classesin the Java the ApplicationProgramming Interface, orAPI.
  • 22. Java I--Copyright © 2000-2004 Tom HunterJava ArchitectureHello.javaJavaCompilerHello.classCompile-TimeEnvironmentRun-TimeEnvironmentJavaVirtualMachineObject.class String.classExample Java API class filesCombined, the JavaVirtual Machine andthe Java API form a“Platform.”The Java Platform
  • 23. Java I--Copyright © 2000-2004 Tom Hunter• The Java Platform is unique, because it canwork without modification onany platform, on anyoperating system, if thatplatform has a “Java VirtualMachine.”Java Architecture
  • 24. Java I--Copyright © 2000-2004 Tom HunterComparison of a typical ProceduralProgram with a Java Program:• In a typical C program, the source code iscompiled into a native machine languagemodule that consists of 1’s and 0’s.What is the ?Java ArchitectureJavaVirtualMachine
  • 25. Java I--Copyright © 2000-2004 Tom HunterC object modulecompiled intomachine languageC Source Code• The machine language is specifically tailoredto one OS, be it Wintel, Mac, UNIX or MVS.• Therefore, it is impossible for one objectmodule to be portable between platforms.
  • 26. Java I--Copyright © 2000-2004 Tom HunterIn contrast to conventional programminglanguages, a Java program is not compiledinto native machine language.• Instead, Java makes bytecode.• Bytecode is the result of a “compile”, butthe compile results in an intermediate formthat stops short of native machine-specificcode.Java “bytecode”Java Architecture
  • 27. Java I--Copyright © 2000-2004 Tom Hunter• Instead of making a machine languagenative code for each particular OS, Javamakes a single, universal bytecode modulethat feeds into any Java Virtual Machine(JVM).• Each OS has its own differentimplementation of theJava Virtual Machine.Java Architecture
  • 28. Java I--Copyright © 2000-2004 Tom Hunter• The JVM sets up its own world withinyour RAM.• The JVM creates an internalsoftware-only sub-computer within the OS.• The bytecode talks to the JVM, and theJVM talks to the Operating System.Java Architecture
  • 29. Java I--Copyright © 2000-2004 Tom Hunter• Thus, you get the Holy Grail of software reuse:“Write Once,Run Anywhere”.Java Architecture
  • 30. Java I--Copyright © 2000-2004 Tom HunterJava SourceJava BytecodeWintel Mac UNIX MVSJVM-Win JVM-Mac JVM-Unix JVM-IBMYou can easily see why BillGates isn’t in love withJava!Thebytecodeis methalf-wayby theJVM.
  • 31. Java I--Copyright © 2000-2004 Tom Hunter• The Virtual Machine interprets thebytecode one instruction at a time,and translates it into native machinecode.• You compile your program onceinto bytecode, but it is interpretedanew every time it runs.Java Architecture
  • 32. Java I--Copyright © 2000-2004 Tom HunterSecurity and the“Sandbox”
  • 33. Java I--Copyright © 2000-2004 Tom HunterSecurity and the “Sandbox”C and C++ are famous for speed.• One reason they are fast is because Cand C++ don’t do things like checking thebounds of arrays.• In C or C++, a program can walk offthe edge of an array and invade the memoryspace beyond.• Hackers love that about C and C++.
  • 34. Java I--Copyright © 2000-2004 Tom HunterSecurity and the “Sandbox”• Another weakness of C/C++, that is afavorite among Hackers, is the BufferOverflow.• In this attack, the Hacker floods too muchdata into a buffer and whatever overflows it isturned loose on the system.• Java solves these problems
  • 35. Java I--Copyright © 2000-2004 Tom Hunter• How Java Combats malicious code:Java checks array boundariesJava halts Buffer OverflowsJava has Garbage collection to get ridof objects that are no longer used.Java’s compiler checks to make surethe code is safe before it runs.• Gosling built security into Java, using aconcept known as the “Sandbox.”Security and the “Sandbox”
  • 36. Java I--Copyright © 2000-2004 Tom HunterJDK 1.2 Security ModelVulnerable System Resources(files, etc) Even Local Code is NotTrustedSANDBOXSecurity and the “Sandbox” Remote CodeHas Fine-Grain Access ControlAll Code, both Local and Remote, Must Pass SecurityPolicyLocal Code
  • 37. Java I--Copyright © 2000-2004 Tom HunterSecurity and the “Sandbox”• 5 Steps To Writing A Java Program:1.) Write it in a Text Editor2.) Compiler creates bytecode3.) The “Class loader” places the .classfile in memory.4.) The “Bytecode Verifier” makes surethe code adheres to Java’s securityrules.5.) The JVM Interpreter reads bytecodeand makes platform native code.
  • 38. Java I--Copyright © 2000-2004 Tom HunterSecurity and the “Sandbox”• You see, preventing problems is a majordesign consideration in Java.• This idea led to the most import aspect ofJava: Object Orientation.• Object Orientation protects data and lets aprogram do only what is explicitly permitted.• You could say Java is pessimistic.
  • 39. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• In Java, Object Orientation is sopervasive that it’s nearly impossibleto write a strictly procedural programin the language.
  • 40. Java I--Copyright © 2000-2004 Tom HunterObjects 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 extensionXxx.java
  • 41. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• If I write a Java program called:Hello.javathen, when compiled, this program willbe called:Hello.class
  • 42. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• A class object is compiled Java code thatcontains its own data variables, calledmembers, and sections of procedural codecalled methods.If you have programmed in COBOL, amethod is like a paragraph you perform.If you have programmed in C or C++, amethod is like a function your program calls.
  • 43. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• The combination of the data variablesand the methodsthat are used to read,writeor modifythose variablesis called a class.
  • 44. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• Java has a rich collection of Class Libraries.• These are also known as the Java API orApplication Programming Interface.• To program in Java, you musti.) Learn the Language, andii.) Learn the Class Libraries.
  • 45. Java I--Copyright © 2000-2004 Tom HunterObjects in Java• These class libraries greatly simplify yourjob as a Java programmer.• They help you to write complex programsquickly.• To master Java, you must master theseclass libraries.
  • 46. Java I--Copyright © 2000-2004 Tom HunterCompiling A Java Program• You have created a Java program calledHello.java• To compile it, you run the JDK suppliedutility called:javacC:javac Hello.javaIf this was successful, a file called:Hello.class will be produced.
  • 47. Java I--Copyright © 2000-2004 Tom Hunter• The two largest varieties of Javaprograms:ApplicationsAppletsFirst Java Program
  • 48. Java I--Copyright © 2000-2004 Tom HunterFirst Java Program• A Java Application is a free-standingprogram that is capable of runningdirectly in the Java Virtual Machine.• A Java Applet is a mini-program that ismuch more limited in its abilities. AnApplet can only run within the context ofan HTML browser.
  • 49. Java I--Copyright © 2000-2004 Tom HunterThe double slashes denote a “C++”-style comment.Everything on the line after the double slashes isignored by the compiler.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 exactsame name--matching the upper case--as you see in blueabove: HelloWorld.javaNow our Application is complete. We have added themethod “main”. All methods are lower case. main isa special method--it actually runs the program.In any application, you are always guaranteedthat method main will run.A Java Application// HelloWorld.java Our first Java Application// HelloWorld.java Our first Java Applicationpublic class HelloWorld{}// HelloWorld.java Our first Java Applicationpublic class HelloWorld{public static void main( String args[]){System.out.println( “Hello World!” );}}
  • 50. Java I--Copyright © 2000-2004 Tom HunterA Java ApplicationC:>javac HelloWorld.javaC:>• A successful compile of your javaprogram will return to a bare cursor, asyou see here.
  • 51. Java I--Copyright © 2000-2004 Tom HunterC:>javac HelloWorld.javaC:>java HelloWorldHello World!A Java Application• To run your compiled Application,you enter lowercase java HelloWorldon the command line.• Notice, the “.class” extension isomitted.
  • 52. Java I--Copyright © 2000-2004 Tom HunterNow load the JDK1.4.1,the documentation,change the class path andwrite your first Java program.In Class Activity
  • 53. Java I--Copyright © 2000-2004 Tom Hunter

×