Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA An overview for C++ programmers Wagner Truppel March 1st, 2004.

Similar presentations


Presentation on theme: "JAVA An overview for C++ programmers Wagner Truppel March 1st, 2004."— Presentation transcript:

1 JAVA An overview for C++ programmers Wagner Truppel wagner@cs.ucr.edu March 1st, 2004

2 Mar 1st, 2004Java for C++ programmers2 The early history James Gosling, Sun Microsystems James Gosling, Sun Microsystems Not the usual start for a prog. language Not the usual start for a prog. language Consumer electronics, 1991 Consumer electronics, 1991 Mosaic and the Internet, 1994 Mosaic and the Internet, 1994 The revolution: applets, 1995 The revolution: applets, 1995 Since then, many improvements and additions have been made to the language Since then, many improvements and additions have been made to the language http://java.sun.com/features/1998/05/birthday.html http://java.sun.com/features/1998/05/birthday.html

3 Mar 1st, 2004Java for C++ programmers3 Why is Java so appealing ? Platform independent Platform independent Safe Safe Easy to learn Easy to learn Powerful, well-documented, and easy-to-use libraries to perform many complicated tasks Powerful, well-documented, and easy-to-use libraries to perform many complicated tasks During this presentation, we’ll look into each of these qualities, and more During this presentation, we’ll look into each of these qualities, and more Comparison to C++ Comparison to C++ Hands-on activities Hands-on activities

4 Mar 1st, 2004Java for C++ programmers4 Platform independence Sun’s motto for Java: Sun’s motto for Java: write once, run anywhere It’s a great idea, but… It’s a great idea, but… how’s it done ? how’s it done ? what are the drawbacks ? what are the drawbacks ?

5 Mar 1st, 2004Java for C++ programmers5 Platform independence

6 Mar 1st, 2004Java for C++ programmers6 Platform independence Wait… so does it mean that Java is an interpreted language ? Yes, source is compiled into bytecodes. Wait… so does it mean that Java is an interpreted language ? Yes, source is compiled into bytecodes. Aren’t interpreted languages inherently slower than compiled ones ? Yes. Aren’t interpreted languages inherently slower than compiled ones ? Yes. Why you should not care so much, though: Why you should not care so much, though: Java trades speed for Java trades speed for platform independence platform independence safety (more on this later) safety (more on this later) Java compilers are pretty darn good anyway Java compilers are pretty darn good anyway Still, if you’re really worried about speed, you may always use the so-called just-in-time (JIT) compilers. Still, if you’re really worried about speed, you may always use the so-called just-in-time (JIT) compilers.

7 Mar 1st, 2004Java for C++ programmers7 Safe and easy to learn The first thing to note here is that these are relative terms The first thing to note here is that these are relative terms In this talk, we’ll compare Java and C++ In this talk, we’ll compare Java and C++ The general consensus is that Java is easier to learn and use than C++, but I’ll let you be the judge of that. The general consensus is that Java is easier to learn and use than C++, but I’ll let you be the judge of that. Is Java safer than C++ ? Is Java safer than C++ ?

8 Mar 1st, 2004Java for C++ programmers8 Safer than C++ ? What do we mean by “safe” anyway ? What do we mean by “safe” anyway ? Less prone to programmer mistakes Java achieves better safety than C++ by Java achieves better safety than C++ by providing sandboxes (won’t talk much about them here) providing sandboxes (won’t talk much about them here) checking every array access for out-of-bounds errors checking every array access for out-of-bounds errors eliminating direct access to pointer operations eliminating direct access to pointer operations automatically reclaiming any (heap) memory space not in use (automatic garbage collection) automatically reclaiming any (heap) memory space not in use (automatic garbage collection) having a less fragile approach to multiple inheritance having a less fragile approach to multiple inheritance making every function virtual making every function virtual providing, generally speaking, a simpler syntax than C++ providing, generally speaking, a simpler syntax than C++

9 Mar 1st, 2004Java for C++ programmers9 No pointers ? Some people claim that Java has no pointers… Not true! Some people claim that Java has no pointers… Not true! All objects are accessed through references, which are automatically de-referenced pointers All objects are accessed through references, which are automatically de-referenced pointers However, the pointer nature of these references is hidden from the programmer. Why ? However, the pointer nature of these references is hidden from the programmer. Why ? Reduced number of pointer-related errors Reduced number of pointer-related errors

10 Mar 1st, 2004Java for C++ programmers10 Automatic garbage collection Objects are always allocated in the heap, using new, as in Foo f = new Foo(); Objects are always allocated in the heap, using new, as in Foo f = new Foo(); f itself is always allocated in the stack f itself is always allocated in the stack the object referenced by f is allocated in the heap the object referenced by f is allocated in the heap recall that memory allocation in C++ is not so simple recall that memory allocation in C++ is not so simple Java keeps track of how many valid references exist for each object – when an object has no more references to it, the memory space it occupies in the heap gets reclaimed Java keeps track of how many valid references exist for each object – when an object has no more references to it, the memory space it occupies in the heap gets reclaimed No, it doesn’t mean that you may be sloppy No, it doesn’t mean that you may be sloppy Automatic garbage collection has pros and cons Automatic garbage collection has pros and cons Pro: prevents many common memory allocation bugs Pro: prevents many common memory allocation bugs Con: has a negative impact on your program’s efficiency Con: has a negative impact on your program’s efficiency

11 Mar 1st, 2004Java for C++ programmers11 No multiple inheritance ? C++ inheritance forces the inheritance of both data and behavior (code) C++ inheritance forces the inheritance of both data and behavior (code) That’s a very fragile approach – in order to inherit some behavior your class may have to gain some data as well, even if it’s not really needed That’s a very fragile approach – in order to inherit some behavior your class may have to gain some data as well, even if it’s not really needed Java solves that problem and at the same time eliminates the need for multiple inheritance by defining something called an interface Java solves that problem and at the same time eliminates the need for multiple inheritance by defining something called an interface Interfaces only define the expected behavior of a set of functions, like a contract – no data and no implementation Interfaces only define the expected behavior of a set of functions, like a contract – no data and no implementation A class may implement as many interfaces as needed A class may implement as many interfaces as needed Of course, regular inheritance between classes is still allowed, but a class may inherit from only one other class - no multiple class inheritance in Java Of course, regular inheritance between classes is still allowed, but a class may inherit from only one other class - no multiple class inheritance in Java

12 Mar 1st, 2004Java for C++ programmers12 Functions are always virtual All (non-static) functions in Java follow a late- binding process All (non-static) functions in Java follow a late- binding process Which function code is actually executed depends on the actual run-time type of the object on which the function is being called, not on the object’s declared type at compile time Which function code is actually executed depends on the actual run-time type of the object on which the function is being called, not on the object’s declared type at compile time In C++, unless one declares a function to be virtual, the code to be executed is decided at compile time In C++, unless one declares a function to be virtual, the code to be executed is decided at compile time Thus, in Java, all (non-static) functions are virtual Thus, in Java, all (non-static) functions are virtual Late-binding is a little slower but prevents common hard-to-find bugs Late-binding is a little slower but prevents common hard-to-find bugs

13 Mar 1st, 2004Java for C++ programmers13 Other differences between Java & C++ (Almost) everything is an object (Almost) everything is an object Only primitive types (boolean, char, int, long, float, double) are not objects Only primitive types (boolean, char, int, long, float, double) are not objects Function arguments are always passed by value Function arguments are always passed by value Objects are not copied – only their references are Objects are not copied – only their references are Neat solution to name collisions (packages) Neat solution to name collisions (packages) No separation between header and implementation No separation between header and implementation No operator overloading No operator overloading No structs No structs No generics (templates) and no enums (constant enumerations) until Java 2, 1.5 No generics (templates) and no enums (constant enumerations) until Java 2, 1.5

14 Mar 1st, 2004Java for C++ programmers14 A few other nice things about Java Inherently multi-threaded Inherently multi-threaded Threads are supported at the language level and are also objects Threads are supported at the language level and are also objects Much nicer compiler and run-time error messages than C++ Much nicer compiler and run-time error messages than C++ Exception handling is idiomatic – every Sun- written library uses it and does so consistently Exception handling is idiomatic – every Sun- written library uses it and does so consistently Powerful and easy-to-use libraries for data structures, multi-threading, networking, I/O, graphics, GUI Powerful and easy-to-use libraries for data structures, multi-threading, networking, I/O, graphics, GUI

15 Mar 1st, 2004Java for C++ programmers15 Other cool stuff Javadoc Javadoc Auto-documenting your code Auto-documenting your code Your comments are nicely formatted into a set of HTML pages Your comments are nicely formatted into a set of HTML pages C++ has something similar: Doxygen C++ has something similar: Doxygen Swing Swing Dynamically pluggable look-and-feel (plaf) Dynamically pluggable look-and-feel (plaf) Powerful, easy-to-use GUI toolkit Powerful, easy-to-use GUI toolkit

16 Mar 1st, 2004Java for C++ programmers16 Examples, please !

17 Mar 1st, 2004Java for C++ programmers17 Hot from the Sun… Current release version: Java 2, 1.4.2 Current release version: Java 2, 1.4.2 Java 2, version 1.5 beta is out Java 2, version 1.5 beta is out Support for generics (templates) Support for generics (templates) Autoboxing Autoboxing Enhanced for loop Enhanced for loop Enumerated types Enumerated types Static import Static import C-style formatted input/output C-style formatted input/output Variable arguments Variable arguments and more… and more… http://java.sun.com/developer/technicalArticles/releases/j2se15/ http://java.sun.com/developer/technicalArticles/releases/j2se15/

18 Mar 1st, 2004Java for C++ programmers18 References – online The creators of Java The creators of Java http://www.java.sun.com/ http://www.java.sun.com/ The Java Developer Connection is a must. In particular, their Tech Tips free mailing list is awesome The Java Developer Connection is a must. In particular, their Tech Tips free mailing list is awesome http://developer.java.sun.com/ http://developer.java.sun.com/ A cool site to keep in touch with Java news A cool site to keep in touch with Java news http://www.ibiblio.org/javafaq/index.shtml http://www.ibiblio.org/javafaq/index.shtml This site's free subscription mailing lists are excellent This site's free subscription mailing lists are excellent http://www.javaworld.com/ http://www.javaworld.com/

19 Mar 1st, 2004Java for C++ programmers19 References – books I (basics) The Java Tutorial: A Short Course on the Basics The Java Tutorial: A Short Course on the Basics Thinking in Java (http://www.mindview.net/Books/TIJ/) Thinking in Java (http://www.mindview.net/Books/TIJ/) The JFC Swing Tutorial The JFC Swing Tutorial Java in a Nutshell Java in a Nutshell Java Cookbook Java Cookbook The Elements of Java Style The Elements of Java Style

20 Mar 1st, 2004Java for C++ programmers20 References – books II (intermediate) Practical Java Programming Language Guide Practical Java Programming Language Guide Effective Java Programming Language Guide Effective Java Programming Language Guide Java Pitfalls: Time-Saving Solutions and Workarounds to Improve Programs Java Pitfalls: Time-Saving Solutions and Workarounds to Improve Programs Design Patterns Java Workbook Design Patterns Java Workbook GoF’s Design Patterns (C++) GoF’s Design Patterns (C++)

21 Mar 1st, 2004Java for C++ programmers21 Questions ? I’d like to thank all of you for coming all of you for coming (and staying !) Titus Winters and Dan Berger, for organizing these talks and for inviting me to give this one Titus Winters and Dan Berger, for organizing these talks and for inviting me to give this one


Download ppt "JAVA An overview for C++ programmers Wagner Truppel March 1st, 2004."

Similar presentations


Ads by Google