1 / 69

Prof Darshana Mistry HOD of Computer Department Gandhinagar Institute Of Technology

Prof Darshana Mistry HOD of Computer Department Gandhinagar Institute Of Technology. Core Java. James Gosling and Sun Microsystems Oak Java, May 20, 1995, Sun World HotJava The first Java-enabled Web browser JDK Evolutions

clovis
Télécharger la présentation

Prof Darshana Mistry HOD of Computer Department Gandhinagar Institute Of Technology

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prof DarshanaMistryHOD of Computer DepartmentGandhinagar Institute Of Technology Core Java

  2. James Gosling and Sun Microsystems • Oak • Java, May 20, 1995, Sun World • HotJava • The first Java-enabled Web browser • JDK Evolutions • J2SE, J2ME, and J2EE (not mentioned in the book, but could discuss here optionally) History

  3. Java is simple • Java is object-oriented • Java is distributed • Java is interpreted • Java is robust • Java is secure • Java is architecture-neutral • Java is portable • Java’s performance • Java is multithreaded • Java is dynamic Characteristics of Java

  4. Java Language vs Java Platform • Current version of the language is 1.4.1 • Core language plus additional APIs is called the Java 2 platform • Three versions of the Java 2 Platform, targetted at different uses • Java 2 Micro Edition (J2ME) • Very small Java environment for smart cards, pages, phones, and set-top boxes • Subset of the standard Java libraries aimed at limited size and processing power • Java 2 Standard Edition (J2SE) • The basic platform, which this course will cover • Java 2 Enterprise Edition (J2EE) • For business applications, web services, mission-critical systems • Transaction processing, databases, distribution, replication Versions of Java

  5. Sun are constantly adding new features and APIs • The Core Java API is now very large • Often difficult to keep up with every change • Separate set of extension APIs for specific purposes • E.g. Telephony, Web applications, Game programming • All new developments reviewed through Java Community Process (http://www.jcp.org) • Chance for developers to provide feedback on emerging standards and APIs • Useful to keep an eye on what's coming through • Also a wide range of “open source“ APIs available • E.g. through the Jakarta project (http://jakarta.apache.org) The Java APIs

  6. Useful resources on the web • Java home (http://java.sun.com) • Articles, Software and document downloads, Tutorials • Java Developer Services http://developer.java.sun.com • Early access downloads, forums, newsletters, bug database • Javaworld (http://www.javaworld.com) • Java magazine site, good set of articles and tutorials • IBM developerWorks (http://www.ibm.com/developerWorks) • Technology articles and tutorials Useful Resources

  7. Java programs are compiled to Bytecode • Bytecodeis then interpreted by a JVM, or Java Virtual machine.Thevirtual machine is what runs your program. • It’s the JVM that cares about your Operating system, NOT THE PROGRAM! • WORA - Write Once, Run Anywhere! Is Java Interpreted or Compiled? BOTH!!

  8. Compiling and Executing of Java Program

  9. Java is both compiled and interpreted • Source code is compiled into Java bytecode • Which is then interpreted by the Java Virtual Machine (JVM) • Therefore bytecode is machine code for the JVM • Java bytecode can run on any JVM, on any platform • …including mobile phones and other hand-held devices • Networking and distribution are core features • In other languages these are additional APIs • Makes Java very good for building networked applications, server side components, etc. The Virtual Machine

  10. The Garbage Collector • Java manages memory for you, the developer has no control over the allocation of memory (unlike in C/C++). • This is much simpler and more robust (no chance of memory leaks or corruption) • Runs in the background and cleans up memory while application is running • The Just In Time compiler (JIT) • Also known as “Hot Spot” • Continually optimises running code to improve performance • Can approach the speed of C++ even though its interpreted Features of the JVM

  11. Security • Java offers very fine control over what an application is allowed to do • E.g. Read/write files, open sockets to remote machines, discover information about the users environment, etc • Used in Java Applets to create a “sandbox”. Stops a rogue applet attacking your machine. • Makes Java very safe, an important feature in distributed systems • Class Loading • Loading of bytecode into the virtual machine for execution • Code can be read from a local disk, over a network, or the Internet • Allows downloading of applications and applets on the fly • …and even ‘mobile code’ Features of the JVM

  12. Is Java Object Oriented? YES! Every bit of code in a Java program is in a “Class” Code Reuse, Encapsulation, Polymorphism, Inheritance

  13. Understanding OOP is fundamental to writing good Java applications • Improves design of your code • Improves understanding of the Java APIs • There are several concepts underlying OOP: • Abstract Types (Classes) • Encapsulation (or Information Hiding) • Aggregation • Inheritance • Polymorphism Object-Oriented Programming

  14. Modelling real-world objects in software • Why design applications in this way? • We naturally classify objects into different types. • By attempting to do this with software aim to make it more maintainable, understandable and easier to reuse • In a conventional application we typically: • decompose it into a series of functions, • define data structures that those functions act upon • there is no relationship between the two other than the functions act on the data What is OOP?

  15. How is OOP different to conventional programming? • Decompose the application into abstract data types by identifying some useful entities/abstractions • An abstract type is made up of a series of behaviours and the data that those behaviours use. • Similar to database modelling, only the types have both behaviour and state (data) What is OOP?

  16. Identifying abstract types is part of the modelling/design process • The types that are useful to model may vary according to the individual application • For example a payroll system might need to know about Departments, Employees, Managers, Salaries, etc • An E-Commerce application may need to know about Users, Shopping Carts, Products, etc • Object-oriented languages provide a way to define abstract data types, and then create objects from them • It’s a template (or ‘cookie cutter’) from which we can create new objects • For example, a Car class might have attributes of speed, colour, and behaviours of accelerate, brake, etc • An individual Car object will have the same behaviours but its own values assigned to the attributes (e.g. 30mph, Red, etc) Abstract Data Types

  17. The data (state) of an object is private – it cannot be accessed directly. • The state can only be changed through its behaviour, otherwise known as its public interface or contract • This is called encapsulation Encapsulation

  18. Main benefit of encapsulation • Internal state and processes can be changed independently of the public interface • Limits the amount of large-scale changes required to a system Encapsulation

  19. What does an OO program consist of? • A series of objects that use each others behaviours in order to carry out some desired functionality • When one object invokes some behaviour of another it sends it a message • In Java terms it invokes a method of the other object • A method is the implementation of a given behaviour. • OO programs are intrinsically modular • Objects are only related by their public behaviour (methods) • Therefore objects can be swapped in and out as required (e.g. for a more efficient version) • This is another advantage of OO systems What is an OO program?

  20. Aggregation is the ability to create new classes out of existing classes • Treating them as building blocks or components • Aggregation allows reuse of existing code • “Holy Grail” of software engineering • Two forms of aggregation • Whole-Part relationships • Car is made of Engine, Chassis, Wheels • Containment relationships • A Shopping Cart contains several Products • A List contains several Items Aggregation

  21. Inheritance is the ability to define a new class in terms of an existing class • The existing class is the parent, base or superclass • The new class is the child, derived or subclass • The child class inherits all of the attributes and behaviour of its parent class • It can then add new attributes or behaviour • Or even alter the implementation of existing behaviour • Inheritance is therefore another form of code reuse Inheritance

  22. Means ‘many forms’ • Difficult to describe, easier to show, so we’ll look at this one in a later lesson • In brief though, polymorphism allows two different classes to respond to the same message in different ways • E.g. both a Plane and a Car could respond to a ‘turnLeft’ message, • however the means of responding to that message (turning wheels, or banking wings) is very different for each. • Allows objects to be treated as if they’re identical Polymorphism

  23. Example //This application program prints Welcome //to Java! Import java.io.*; package chapter1; public class Hello { public static void main(String[] args) { System.out.println(“Hello World!"); } } • The import statement tells the compiler to make available classes and methods of another package • A main method indicates where to begin executing a class (if it is designed to be run as a program) • public = can be seen from any package • static = not “part of” an object A Simple Application

  24. On command line • javacfile.java (it produce file.class file) Creating and Compiling Programs

  25. On command line • java classname (Starts the JVM and runs the main method) Executing Applications

  26. javacHello.java Java Hello output:...Hello World Example

  27. Downloading a Java JDK You want a JDK, not just a JRE Create a “Temp” directory on your PC or laptop Go to http://java.sun.com Go to the “Popular Downloads” section and select “Java SE” Select a JDK without Netbeans (We’ll talk about this later) Agree to the accept the use policy Right Click and Save the Offline Windows JDK to your temp dir

  28. Installing a Java JDK Go to your “Temp” dir using Windows Explorer Make sure no other apps are running and double click the install program you just downloaded. Follow the steps. I install in C:\Java Watch fom multiple JREs and JDKs! In Windows the Registry runs the show now, not the JAVA_HOME env variable Test with a Command Prompt Window and “java –version” Add C:\Java\”Java ver”\bin to your PATH var Be sure to add “current directory” to the CLASSPATH (if you had one)

  29. Now, Lets test it DOS - Command Prompt Make a directory structure Type “Edit” and Voila PSVM Test JAVAC and JAVA Create Bytecode ( the *.class file) with “Javac” Run Program with “Java”

  30. If we were lucky we got something that looks like this…

  31. Help on the Web http://java.sun.com/javase/6/docs/api/ http://www.javaranch.com http://java.sun.com/docs/books/tutorial/ http://www.sorcon.com/java2/ http://remus.rutgers.edu/freestuff Got an Error? Just type it verbatim into Google and you usually can find the answer to your question or solve your problem. Wikipedia is a good place to get background and history On anything, and Java is no exception

  32. Java distinguishes two kinds of entities • Primitive types • Objects • Primitive-type data is stored in primitive-type variables • Reference variables store the address of an object • No notion of “object (physically) in the stack” • No notion of “object (physically) within an object” References and Primitive Data Types

  33. Represent numbers, characters, boolean values • Integers: byte, short, int, and long • Real numbers: float and double • Characters: char Primitive Data Types

  34. Primitive Data Types

  35. Primitive Data Types (continued)

  36. subscript [ ], call ( ), member access . • pre/post-increment ++ --, boolean complement !, bitwise complement ~, unary + -, type cast (type), object creation new • * / % • binary + - (+ also concatenates strings) • signed shift << >>, unsigned shift >>> • comparison < <= > >=, class test instanceof • equality comparison == != • bitwise and & • bitwise or| Operators

  37. logical (sequential) and && • logical (sequential) or || • conditional cond ? true-expr : false-expr • assignment=, compound assignment += -= *= /= <<= >>= >>>= &= |= Operators

  38. Widening conversion: • In operations on mixed-type operands, the numeric type of the smaller range is converted to the numeric type of the larger range • In an assignment, a numeric type of smaller range can be assigned to a numeric type of larger range • bytetoshorttointtolong • intkind tofloattodouble Type Compatibility and Conversion

  39. int square; square = n * n; • double cube = n * (double)square; • Can generally declare local variables where they are initialized • All variables get a safe initial value anyway (zero/null) Declaring and Setting Variables

  40. You can declare reference variables • They reference objects of specified types • Two reference variables can reference the same object • The new operator creates an instance of a class • A constructor executes when a new object is created • Example: String greeting = ″hello″; Referencing and Creating Objects

  41. A group of statements executed in order is written • { stmt1; stmt2; ...; stmtN; } • The statements execute in the order 1, 2, ..., N • Control statements alter this sequential flow of execution Java Control Statements

  42. Java Control Statements (continued)

  43. Java Control Statements (continued)

  44. A Java method defines a group of statements as performing a particular operation • static indicates a static or class method • A method that is not static is an instance method • All method arguments are call-by-value • Primitive type: value is passed to the method • Method may modify local copy but will not affect caller’s value • Object reference: address of objectis passed • Change to reference variable does not affect caller • But operations can affect the object, visible to caller Methods

  45. The Class Math Appendix A: Introduction to Java

  46. An escape sequence is a sequence of two characters beginning with the character \ • A way to represents special characters/symbols Escape Sequences Appendix A: Introduction to Java

  47. OO Programming Concepts

  48. Class and Objects

  49. class Circle { double radius = 1.0; double findArea(){ return radius * radius * 3.14159; } } Class Declaration

More Related