Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 Copyright © 2000 JAMM Consulting, Inc.

Similar presentations


Presentation on theme: "Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 Copyright © 2000 JAMM Consulting, Inc."— Presentation transcript:

1 Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 neil@JAMMConsulting.com Copyright © 2000 JAMM Consulting, Inc.

2 Introduction Not a comprehensive discussion Not an intense code review Focus on concepts Interactive - Ask Questions!

3 Background 1.Object Concepts 1.Abstraction 2.Encapsulation 3.Inheritance 4.Polymorphism 2.C++ Syntax

4 Resources 1.“Java 2 and JavaScript for C and C++ Programmers, Revised Edition” M. Daconta, A. Saganich, E. Monk, John Wiley & Sons, Inc., 1999. 2.“A Programmer’s Guide to Java Certification” K. Mughal, R. Rasmussen, Addison-Wesley, 2000. 3.“Just Java 1.1 and Beyond, Third Edition” P. van der Linden, Sunsoft Press, 1998 4.“Java in a Nutshell, 2nd Edition” D. Flanagan, O’Reilly& Associates, 1997

5 General Similarities 1.Java “looks like C++” 2.Class structure is very similar 3.Syntax and keywords are same or similar

6 Class Example C++: class Entity { protected: char *name; int x,y; public: Entity(); ~Entity(); void setName(char *inName); void setXY(int inX, int inY); }; Entity::Entity() { x=y=0; name=NULL; } Entity::~Entity() { … } Entity::setName(char *iName) { … } Entity::setXY(int inX, int inY) { … } Java: class Entity { protected String name; protected int x; protected int y; public Entity() { x=y=0; name=null; } public void finalize() { … } public void setName(String name) { … } public void setXY(int inX, int iny) { … } } Source: Daconta, Pg. 104

7 Class Structure 1.Declaration - same as C++ 2.Data Members 1.Can be primitive types and/or classes 2.Initialization is automatic 3.Constructors & Methods 1.Declared within the class 2.No scope (::) operator for declaration 3.Can be overloaded

8 Class Initialization Can assign initial values to instance variables at declaration: public String name = “Neil”; Classes may have static initializers: public static String name = “Neil”; static { // Static initialization code }

9 Inheritance Concept is same, syntax is different –C++: class Sub: public Base Constructor():SuperConstructor() { … } –Java: class Sub extends Base super() No multiple inheritance (Can use interfaces)

10 Abstract and Virtual Pure Virtual Functions –In C++: Declared as virtual –In Java: Declared as abstract Classes –In C++: No explicit declaration –In Java: Declared as abstract Java: All methods are virtual (late binding)

11 Inner Classes Similar to C++ nested classes Defined only within context of enclosing class May extend another class or implement an interface

12 No Pointers Java does not have explicit pointers All objects are passed by reference Therefore, everything is a pointer

13 Misc. Similarities this Exceptions: throw, try, catch. Java adds finally

14 Misc. Differences Statics are declared within a class Java: instanceof, C++: typeid main must be a static method of a class Scoping operator: Java uses dot (.) while C++ uses double colon(::) String is a fundamental Java type Different system libraries

15 Features Omitted from C++ No preprocessor No asserts No destructors No operator overloading No templates No multiple inheritance No explicit references

16 Features Omitted from C++ (2) No friend classes No typedef No variable-length argument lists No enumerated types No forward references No explicit memory management Why is this language useful???

17 Intermission Take a break Stretch your legs Come back in 15 minutes

18 Java Features not in C++ Packages CLASSPATH Interfaces Dynamic Classes Multithreading Garbage Collection Object References

19 Java Features not in C++ GUI development Platform Independence Language Security Applets Servlets Language Growth

20 Packages Container for related classes and interfaces Relate to directory structure via CLASSPATH All classes belong to a package, if not stated, they are in the unnamed package A class is uniquely referenced by: packageName.className Convention is to use internet domain name: com.jammconsulting.util.UtilityClass

21 Packages (2) All classes within a package are “friendly” Package is declared at top of java source file package com.jammconsulting.util; Can use import statement to allow local reference to one or more classes within a package: import com.jammconsulting.util.*; import com.jammconsulting.util.UtilityClass;

22 Accessibility Modifiers Accessibility Modifiers in Java: Source: Flanagan, Pg. 73

23 CLASSPATH Environment variable May be overridden by parameter to Java VM Lists starting points to search for packages Packages must be in directories underneath the classpath point May contain ZIP or JAR files containing classes

24 Interfaces High-level description of a class behavior Listing of method signatures Abstract class that may point to any class that implements the interface class MyClass implements MyInterface Can reference MyClass by MyInterface A class may implement multiple interfaces

25 Dynamic Classes & Interfaces An interface may be used without knowing the classes that implement it. Any class may be loaded and instantiated during runtime using Class.forName Using the Reflection API, classes may: – Inspect themselves or other classes – Follow relationships between classes –Perform actions on classes

26 Multithreading Any Java process can spawn additional execution paths -- called threads Threads execute independently and simultaneously Threads share the same memory space Threads may operate at different priority levels Threads may be grouped to manage them collectively

27 Multithreading (2) Safety, deadlock, and starvation Java allows for thread interactions –start, stop, destroy, suspend, resume, sleep, wait, yield, notify Java has implicit locks on each object -- use synchronized keyword to utilize these locks

28 Garbage Collection My favorite feature!! The new operator explicitly creates objects No way to explicitly free objects Java VM automatically identifies unused objects and frees them during periods of low activity Can use finalize method to give it a hint Can manually call the garbage collector

29 Reference Objects Used to track the state of an object Type of reference determines when the object may be garbage collected. Object States: –Active: Currently in use –Finalized: Garbage Collector has marked the object for reclamation, but it is still in RAM –Reclaimed: The RAM previously allocated to the object has been used for something else

30 Reference Objects (2) Strong reference: An object referenced directly from a class variable Three types of Reference Objects: –SoftReference (Cannot be garbage collected. Used for implementing object caches) –WeakReference (Can be garbage collected, but not yet finalized) –PhantomReference (Has been finalized, but not yet reclaimed)

31 Platform Independence Java source code is compiled into bytecode Bytecode is platform neutral Platform-dependent VM interprets bytecode and runs it. Many use JIT compilers. Internet applications must be viable on any platform “Write Once Run Anywhere” NOTE: Bytecode can be easily decompiled

32 GUI Development GUI is part of Java standard library No third-party libraries needed JFC (Swing) classes -- professional quality widgets Runs on any platform

33 Language Security Protection against malicious code Allows untrusted code to be run safely Programs cannot directly access memory Programs are restricted to own memory space Byte-code verification Can run in sandbox -- restricted rights Digital signing

34 Applet A mini-application Runs within a web browser Runs in sandbox Allows highly-interactive locally running programs Eliminates software distribution Slow to download code

35 Servlet Program running on a web server Like CGI, can invoke a servlet to process information and generate a response Unlike CGI, remain resident across invocations -- much higher performance Allows web site development in one comprehensive language “Easy” database interactions

36 Language Growth New extensions coming out every day Large amount of excitement and development PDAs and embedded devices Possibilities are endless

37 Homework Review the differences between Java and C++ Begin coding simple examples of the new features of Java

38 About JAMM Consulting Full-service consulting firm dedicated to Internet development projects E-commerce, online databases, interactive web site design Skills: Java, C++, C, HTML, perl, CGI, etc. Finder’s fees paid for job referrals http://www.JAMMConsulting.com


Download ppt "Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 Copyright © 2000 JAMM Consulting, Inc."

Similar presentations


Ads by Google