Top Banner
Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 [email protected] yright © 2000 JAMM Consulting, Inc.
38

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

Dec 28, 2015

Download

Documents

Ethel Miller
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java Compared to C++ Neil Aggarwal President & CEO JAMM Consulting, Inc. (972) 612-6056 neil@JAMMConsulting.com Copyright © 2000 JAMM Consulting, Inc.

Java Compared to C++Neil Aggarwal

President & CEO

JAMM Consulting, Inc.

(972) 612-6056

[email protected] © 2000 JAMM Consulting, Inc.

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

Introduction

• Not a comprehensive discussion

• Not an intense code review

• Focus on concepts

• Interactive - Ask Questions!

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

Background

1. Object Concepts

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

2. C++ Syntax

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

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

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

General Similarities

1. Java “looks like C++”

2. Class structure is very similar

3. Syntax and keywords are same or similar

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

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

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

Class Structure

1. Declaration - same as C++

2. Data Members1. Can be primitive types and/or classes

2. Initialization is automatic

3. Constructors & Methods1. Declared within the class

2. No scope (::) operator for declaration

3. Can be overloaded

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

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

}

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

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)

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

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)

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

Inner Classes

• Similar to C++ nested classes

• Defined only within context of enclosing class

• May extend another class or implement an interface

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

No Pointers

• Java does not have explicit pointers

• All objects are passed by reference

• Therefore, everything is a pointer

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

Misc. Similarities

• this

• Exceptions: throw, try, catch. Java adds finally

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

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

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

Features Omitted from C++

• No preprocessor

• No asserts

• No destructors

• No operator overloading

• No templates

• No multiple inheritance

• No explicit references

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

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???

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

Intermission

• Take a break

• Stretch your legs

• Come back in 15 minutes

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

Java Features not in C++

• Packages

• CLASSPATH

• Interfaces

• Dynamic Classes

• Multithreading

• Garbage Collection

• Object References

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

Java Features not in C++

• GUI development

• Platform Independence

• Language Security

• Applets

• Servlets

• Language Growth

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

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

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

Packages (2)

• All classes within a package are “friendly”

• Package is declared at top of java source filepackage 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;

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

Accessibility Modifiers

• Accessibility Modifiers in Java:

Source: Flanagan, Pg. 73

Accessible to: public protected package (default) privateSame class Y Y Y YClass in same package Y Y Y NSubclass in different package Y Y N NNon-subclass, different package Y N N N

Member Visibility

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

GUI Development

• GUI is part of Java standard library

• No third-party libraries needed

• JFC (Swing) classes -- professional quality widgets

• Runs on any platform

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

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

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

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

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

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

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

Language Growth

• New extensions coming out every day

• Large amount of excitement and development

• PDAs and embedded devices

• Possibilities are endless

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

Homework

• Review the differences between Java and C++

• Begin coding simple examples of the new features of Java

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

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