Your SlideShare is downloading. ×
0
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
Java i lecture_1_upd1
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_upd1

624

Published on

NIIT Java I

NIIT Java I

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

No Downloads
Views
Total Views
624
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
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. Chapter 1Introduction to Computers, the Internet and the Web Java I--Copyright © 2000-2004 Tom Hunter
  • 4. The Genesis of Java• 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 5. The Genesis of Java Naughton 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.” Java I--Copyright © 2000-2004 Tom Hunter
  • 6. The Genesis of Java• Formation of the “Green Project”Jan 1991 The 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 7. The Genesis of Java• James Gosling, then age 36, was asked to find aprogramming 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 adirect descendant of the C programminglanguage). Java I--Copyright © 2000-2004 Tom Hunter
  • 8. The Genesis of Java• 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 the ones Naughton encountered, • an Interpreted language, • Object Oriented, Java I--Copyright © 2000-2004 Tom Hunter
  • 9. The Genesis of Java • 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 10. The Genesis of Java• Demo of *7, Programmed in Oak3 Sept 1992This was the prototype of thefirst device to use the Oakprogramming language. The “Star7” also featured the debut of “Duke,” the Java mascot. An early applet showed Duke doing cartwheelsacross the screen. Java I--Copyright © 2000-2004 Tom Hunter
  • 11. The Genesis of Java• Oak becomes Java.Jan 1995 By 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 12. The Genesis of Java• Java Catches Fire23 Mar 1995 Although Java had not yet beenofficially released, it was spreading likewildfire among developers. Then, something very lucky happened... Java I--Copyright © 2000-2004 Tom Hunter
  • 13. Java I--Copyright © 2000-2004 Tom Hunter
  • 14. The 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 15. Java’sMajor Advantage over C & C++ Java I--Copyright © 2000-2004 Tom Hunter
  • 16. Java’s Major Advantage over C & C++• 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 I--Copyright © 2000-2004 Tom Hunter
  • 17. Java’s Origins in C & C++Thus, it has been said that...“Java is C without the Guns and Knives.” Java I--Copyright © 2000-2004 Tom Hunter
  • 18. Java Architecture• 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 I--Copyright © 2000-2004 Tom Hunter
  • 19. Java Architecture Java’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 Java I--Copyright © 2000-2004 Tom Hunter
  • 20. Java 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 21. Java Architecture • When a Java program runs, it is assisted by other classes in the Java the Application Programming Interface, or API. Java I--Copyright © 2000-2004 Tom Hunter
  • 22. Java Architecture Combined, the Java Example Java API class files Virtual Machine and Object.class String.class the Java API form a “Platform.” Compile-Time Environment Hello.class Java Virtual Java Compiler Machine Run-Time Hello.java Environment The Java PlatformJava I--Copyright © 2000-2004 Tom Hunter
  • 23. Java Architecture• The Java Platform is unique, because it canwork without modification onany platform, on anyoperating system, if thatplatform has a “Java VirtualMachine.” Java I--Copyright © 2000-2004 Tom Hunter
  • 24. Java Architecture Java What is the Virtual ? MachineComparison 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 25. C Source Code C object module compiled into machine language• 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 26. Java Architecture Java “bytecode”In 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 I--Copyright © 2000-2004 Tom Hunter
  • 27. Java Architecture• 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 the Java Virtual Machine. Java I--Copyright © 2000-2004 Tom Hunter
  • 28. Java Architecture• 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 I--Copyright © 2000-2004 Tom Hunter
  • 29. Java Architecture• Thus, you get the Holy Grail of software reuse: “Write Once, Run Anywhere”. Java I--Copyright © 2000-2004 Tom Hunter
  • 30. Java Source You can easily see why BillThe Gates isn’t in love withbytecode Java Bytecode Java!is methalf-wayby theJVM.JVM-Win JVM-Mac JVM-Unix JVM-IBM Wintel Mac UNIX MVS Java I--Copyright © 2000-2004 Tom Hunter
  • 31. Java Architecture • 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 32. Security and the “Sandbox” Java I--Copyright © 2000-2004 Tom Hunter
  • 33. Security 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++. Java I--Copyright © 2000-2004 Tom Hunter
  • 34. Security 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 Java I--Copyright © 2000-2004 Tom Hunter
  • 35. Security and the “Sandbox”• 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 aconcept known as the “Sandbox.” Java I--Copyright © 2000-2004 Tom Hunter
  • 36. Security and the “Sandbox” Remote Code Local Code All Code, both Local and Remote, Must Pass Security Policy JDK 1.2 Security Model SANDBOX Vulnerable System Resources (files, etc) Even Local Code is Not TrustedHas Fine-Grain Access Control Java I--Copyright © 2000-2004 Tom Hunter
  • 37. Security and the “Sandbox”• 5 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 38. Security 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 39. Objects in Java• In Java, Object Orientation is sopervasive that it’s nearly impossibleto write a strictly procedural programin the language. Java I--Copyright © 2000-2004 Tom Hunter
  • 40. 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 extensionXxx.java Java I--Copyright © 2000-2004 Tom Hunter
  • 41. Objects in Java• If I write a Java program called: Hello.java then, when compiled, this program willbe called: Hello.class Java I--Copyright © 2000-2004 Tom Hunter
  • 42. Objects 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 43. Objects in Java• The combination of the data variables and the methods that are used to read, write or modify those variablesis called a class. Java I--Copyright © 2000-2004 Tom Hunter
  • 44. Objects 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 must i.) Learn the Language, and ii.) Learn the Class Libraries. Java I--Copyright © 2000-2004 Tom Hunter
  • 45. Objects 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 46. Compiling A Java Program• You have created a Java program called Hello.java• To compile it, you run the JDK suppliedutility called: javacC:javac Hello.java If this was successful, a file called: Hello.class will be produced. Java I--Copyright © 2000-2004 Tom Hunter
  • 47. First Java Program• The two largest varieties of Javaprograms: Applications Applets Java I--Copyright © 2000-2004 Tom Hunter
  • 48. First 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. Java I--Copyright © 2000-2004 Tom Hunter
  • 49. A Java Application// HelloWorld.java Our first Java Applicationpublic class HelloWorld{ public static void main( String args[]) { System.out.println( “Hello World!” ); }}Nowdouble slashesname. a “C++”-style comment.The our Application is complete. We have added the This is the class denotemethod “main”. All methodsJava lowerslashesmain is Every single bit of code in are must case. isEverything on the line after the double sit in curly brackets.a specialby the compiler.ignored names are capitalized. the program. Class method--it actually runs Words within the name are also capitalized. In any application, you are always guaranteedthat method main will run.be saved in a file with the exact This Java program must same name--matching the upper case--as you see in blue above: HelloWorld.java Java I--Copyright © 2000-2004 Tom Hunter
  • 50. A Java Application C:>javac HelloWorld.java C:>• A successful compile of your javaprogram will return to a bare cursor, asyou see here. Java I--Copyright © 2000-2004 Tom Hunter
  • 51. A Java Application C:>javac HelloWorld.java C:>java HelloWorld Hello World! • To run your compiled Application,you enter lowercase java HelloWorldon the command line. • Notice, the “.class” extension isomitted. Java I--Copyright © 2000-2004 Tom Hunter
  • 52. In Class ActivityNow load the JDK1.4.1,the documentation,change the class path andwrite your first Java program. Java I--Copyright © 2000-2004 Tom Hunter
  • 53. Java I--Copyright © 2000-2004 Tom Hunter

×