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

Post on 28-Dec-2015

216 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

Transcript

Java Compared to C++Neil Aggarwal

President & CEO

JAMM Consulting, Inc.

(972) 612-6056

neil@JAMMConsulting.comCopyright © 2000 JAMM Consulting, Inc.

Introduction

• Not a comprehensive discussion

• Not an intense code review

• Focus on concepts

• Interactive - Ask Questions!

Background

1. Object Concepts

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

2. C++ Syntax

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

General Similarities

1. Java “looks like C++”

2. Class structure is very similar

3. Syntax and keywords are same or similar

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

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

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

}

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)

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)

Inner Classes

• Similar to C++ nested classes

• Defined only within context of enclosing class

• May extend another class or implement an interface

No Pointers

• Java does not have explicit pointers

• All objects are passed by reference

• Therefore, everything is a pointer

Misc. Similarities

• this

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

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

Features Omitted from C++

• No preprocessor

• No asserts

• No destructors

• No operator overloading

• No templates

• No multiple inheritance

• No explicit references

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

Intermission

• Take a break

• Stretch your legs

• Come back in 15 minutes

Java Features not in C++

• Packages

• CLASSPATH

• Interfaces

• Dynamic Classes

• Multithreading

• Garbage Collection

• Object References

Java Features not in C++

• GUI development

• Platform Independence

• Language Security

• Applets

• Servlets

• Language Growth

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

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;

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

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

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

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

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

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

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

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

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)

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

GUI Development

• GUI is part of Java standard library

• No third-party libraries needed

• JFC (Swing) classes -- professional quality widgets

• Runs on any platform

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

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

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

Language Growth

• New extensions coming out every day

• Large amount of excitement and development

• PDAs and embedded devices

• Possibilities are endless

Homework

• Review the differences between Java and C++

• Begin coding simple examples of the new features of Java

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

top related