⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appendixb.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:26:28
Translation Platform:Win32
Number of Output files:27
This File:AppendixB.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>B: Comparing C++ and Java</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
    [ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in Java, 1st edition</FONT></H2>
  <H3><FONT FACE="Verdana">&copy;1998 by Bruce Eckel</FONT></H3>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="AppendixA.html">Previous Chapter</a> ] 
    [ <a href="SimpleContents.html">Short TOC</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="AppendixC.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc407441463"></A><A NAME="_Toc408018847"></A><A NAME="Heading631"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
B: Comparing C++ and Java</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>As a C++ programmer, you
already have the basic idea of object-oriented programming, and the syntax of
Java no doubt looks familiar to you. This makes sense since Java was derived
from C++.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However, there are a surprising
number of differences between
<A NAME="Index3112"></A><A NAME="Index3113"></A>C++ and Java. These differences
are intended to be significant improvements, and if you understand the
differences you&#8217;ll see why Java is such a beneficial programming language.
This appendix takes you through the important features that distinguish Java
from C++.</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Georgia">	The biggest potential stumbling block is speed:
interpreted Java runs in the range of 20 times slower than C. Nothing prevents
the Java language from being compiled and there are just-in-time compilers
appearing at this writing that offer significant speed-ups. It is not
inconceivable that full native compilers will appear for the more popular
platforms, but without those there are classes of problems that will be
insoluble with Java because of the speed
issue.</FONT><LI><FONT FACE="Georgia">	Java has both kinds of comments
like C++ does.</FONT><LI><FONT FACE="Georgia">	Everything must be in a
class. There are no global functions or global data. If you want the equivalent
of globals, make <B>static</B> methods and <B>static</B> data within a class.
There are no structs or enumerations or unions, only
classes.</FONT><LI><FONT FACE="Georgia">	All method definitions are
defined in the body of the class. Thus, in C++ it would look like all the
functions are inlined, but they&#8217;re not (inlines are noted
later).</FONT><LI><FONT FACE="Georgia">	Class definitions are roughly the
same form in Java as in C++, but there&#8217;s no closing semicolon. There are
no class declarations of the form <B>class foo,</B> only class
definitions.</FONT>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> aType {
  <font color=#0000ff>void</font> aMethod(&#160;) { <font color=#009900>/* method body */</font> }
}</PRE></FONT></BLOCKQUOTE>

<LI><FONT FACE="Georgia">	There&#8217;s no scope resolution operator
<B>::</B> in Java. Java uses the dot for everything, but can get away with it
since you can define elements only within a class. Even the method definitions
must always occur within a class, so there is no need for scope resolution there
either. One place where you&#8217;ll notice the difference is in the calling of
<B>static</B> methods: you say <B>ClassName.methodName(&#160;);</B>. In
addition, <B>package</B> names are established using the dot, and to perform a
kind of C++ <B>#include</B> you use the <B>import</B> keyword. For example:
<B>import java.awt.*;</B>. (<B>#include</B> does not directly map to
<B>import</B>, but it has a similar feel to
it).</FONT><LI><FONT FACE="Georgia">	Java, like C++, has primitive types
for efficient access. In Java, these are <B>boolean</B>, <B>char</B>,
<B>byte</B>, <B>short</B>, <B>int</B>, <B>long</B>, <B>float</B>, and
<B>double</B>. All the primitive types have specified sizes that are machine
independent for portability. (This must have some impact on performance, varying
with the machine.) Type-checking and type requirements are much tighter in Java.
For example:</FONT><BR><FONT FACE="Georgia">1. Conditional expressions can
be only<B> boolean</B>, not integral.</FONT><BR><FONT FACE="Georgia">2.
The result of an expression like X + Y must be used; you can&#8217;t just say
&#8220;X + Y&#8221; for the side
effect.</FONT><LI><FONT FACE="Georgia">	The <B>char</B> type uses the
international 16-bit Unicode character set, so it can automatically represent
most national characters.</FONT><LI><FONT FACE="Georgia">	Static quoted
strings are automatically converted into <B>String</B> objects. There is no
independent static character array string like there is in C and
C++.</FONT><LI><FONT FACE="Georgia">	Java adds the triple right shift
<B>&gt;&gt;&gt;</B> to act as a &#8220;logical&#8221; right shift by inserting
zeroes at the top end; the <B>&gt;&gt;</B> inserts the sign bit as it shifts (an
&#8220;arithmetic&#8221; shift).</FONT><LI><FONT FACE="Georgia">	Although
they look similar, arrays have a very different structure and behavior in Java
than they do in C++. There&#8217;s a read-only <B>length</B> member that tells
you how big the array is, and run-time checking throws an exception if you go
out of bounds. All arrays are created on the heap, and you can assign one array
to another (the array handle is simply copied). The array identifier is a
first-class object, with all of the methods commonly available to all other
objects.</FONT><LI><FONT FACE="Georgia">	All objects of non-primitive
types can be created only via <B>new</B>. There&#8217;s no equivalent to
creating non-primitive objects &#8220;on the stack&#8221; as in C++. All
primitive types can be created only on the stack, without <B>new</B>. There are
wrapper classes for all primitive classes so that you can create equivalent
heap-based objects via <B>new</B>. (Arrays of primitives are a special case:
they can be allocated via aggregate initialization as in C++, or by using
<B>new.</B>)</FONT><LI><FONT FACE="Georgia">	No forward declarations are
necessary in Java. If you want to use a class or a method before it is defined,
you simply use it &#8211; the compiler ensures that the appropriate definition
exists. Thus you don&#8217;t have any of the forward referencing issues that you
do in C++.</FONT><LI><FONT FACE="Georgia">	Java has no preprocessor. If
you want to use classes in another library, you say <B>import</B> and the name
of the library. There are no preprocessor-like
macros.</FONT><LI><FONT FACE="Georgia">	Java uses packages in place of
namespaces. The name issue is taken care of by putting everything into a class
and by using a facility called &#8220;packages&#8221; that performs the
equivalent namespace breakup for class names. Packages also collect library
components under a single library name. You simply <B>import</B> a package and
the compiler takes care of the
rest.</FONT><LI><FONT FACE="Georgia">	Object handles defined as class
members are automatically initialized to <B>null</B>. Initialization of
primitive class data members is guaranteed in Java; if you don&#8217;t
explicitly initialize them they get a default value (a zero or equivalent). You
can initialize them explicitly, either when you define them in the class or in
the constructor. The syntax makes more sense than that for C++, and is
consistent for <B>static</B> and non-<B>static</B> members alike. You
don&#8217;t need to externally define storage for <B>static</B> members like you
do in C++.</FONT><LI><FONT FACE="Georgia">	There are no Java pointers in
the sense of C and C++. When you create an object with <B>new</B>, you get back
a reference (which I&#8217;ve been calling a <I>handle</I> in this book). For
example: </FONT><BR><TT><FONT FACE="Courier New">String s = new
String(&#8220;howdy&#8221;);</FONT></TT><BR><FONT FACE="Georgia">However,
unlike C++ references that must be initialized when created and cannot be
rebound to a different location, Java references don&#8217;t have to be bound at
the point of creation. They can also be rebound at will, which eliminates part
of the need for pointers. The other reason for pointers in C and C++ is to be
able to point at any place in memory whatsoever (which makes them unsafe, which
is why Java doesn&#8217;t support them). Pointers are often seen as an efficient
way to move through an array of primitive variables; Java arrays allow you to do
that in a safer fashion. The ultimate solution for pointer problems is native
methods (discussed in Appendix A). Passing pointers to methods isn&#8217;t a
problem since there are no global functions, only classes, and you can pass
references to objects.</FONT><BR><FONT FACE="Georgia">The Java language
promoters initially said &#8220;No pointers!&#8221;, but when many programmers
questioned how you can work without pointers, the promoters began saying
&#8220;Restricted pointers.&#8221; You can make up your mind whether it&#8217;s
&#8220;really&#8221; a pointer or not. In any event, there&#8217;s no pointer
<I>arithmetic</I>.</FONT><LI><FONT FACE="Georgia">	Java has constructors
that are similar to constructors in C++. You get a default constructor if you
don&#8217;t define one, and if you define a non-default constructor,
there&#8217;s no automatic default constructor defined for you, just like in
C++. There are no copy constructors, since all arguments are passed by
reference.</FONT><LI><FONT FACE="Georgia">	There are no destructors in
Java. There is no &#8220;scope&#8221; of a variable per se, to indicate when the
object&#8217;s lifetime is ended &#8211; the lifetime of an object is determined
instead by the garbage collector. There is a <B>finalize(&#160;)</B> method
that&#8217;s a member of each class, something like a C++ destructor, but
<B>finalize(&#160;)</B> is called by the garbage collector and is supposed to be
responsible only for releasing "resources" (such as open files, sockets, ports,
URLs, etc). If you need something done at a specific point, you must create a
special method and call it, not rely upon <B>finalize(&#160;)</B>. Put another
way, all objects in C++ will be (or rather, should be) destroyed, but not all
objects in Java are garbage collected. Because Java doesn&#8217;t support
destructors, you must be careful to create a cleanup method if it&#8217;s
necessary and to explicitly call all the cleanup methods for the base class and
member objects in your class.</FONT><LI><FONT FACE="Georgia">	Java has
method overloading that works virtually identically to C++ function
overloading.</FONT><LI><FONT FACE="Georgia">	Java does not support default
arguments.</FONT><LI><FONT FACE="Georgia">	There&#8217;s no <B>goto</B> in
Java. The one unconditional jump mechanism is the <B>break </B><I>label</I> or
<B>continue </B><I>label</I>, which is used to jump out of the middle of
multiply-nested loops.</FONT><LI><FONT FACE="Georgia">	Java uses a
singly-rooted hierarchy, so all objects are ultimately inherited from the root
class <B>Object</B>. In C++, you can start a new inheritance tree anywhere, so
you end up with a forest of trees. In Java you get a single ultimate hierarchy.
This can seem restrictive, but it gives a great deal of power since you know
that every object is guaranteed to have at least the <B>Object</B> interface.
C++ appears to be the only OO language that does not impose a singly rooted
hierarchy.</FONT><LI><FONT FACE="Georgia">	Java has no templates or other
implementation of parameterized types. There is a set of collections:
<B>Vector</B>, <B>Stack</B>, and <B>Hashtable</B> that hold <B>Object</B>
references, and through which you can satisfy your collection needs, but these
collections are not designed for efficiency like the C++ Standard Template
Library (STL). The new collections in Java 1.2 are more complete, but still
don&#8217;t have the same kind of efficiency as template implementations would
allow.</FONT><LI><FONT FACE="Georgia">	Garbage collection means memory
leaks are much harder to cause in Java, but not impossible. (If you make native
method calls that allocate storage, these are typically not tracked by the
garbage collector.) However, many memory leaks and resouce leaks can be tracked
to a badly written <B>finalize(&#160;) </B>or to not releasing a resource at the
end of the block where it is allocated (a place where a destructor would
certainly come in handy). The garbage collector is a huge improvement over C++,
and makes a lot of programming problems simply vanish. It might make Java
unsuitable for solving a small subset of problems that cannot tolerate a garbage
collector, but the advantage of a garbage collector seems to greatly outweigh
this potential drawback.</FONT><LI><FONT FACE="Georgia">	Java has built-in
multithreading support. There&#8217;s a <B>Thread</B> class that you inherit to
create a new thread (you override the <B>run(&#160;)</B> method). Mutual
exclusion occurs at the level of objects using the <B>synchronized</B> keyword
as a type qualifier for methods. Only one thread may use a <B>synchronized</B>
method of a particular object at any one time. Put another way, when a
<B>synchronized</B> method is entered, it first &#8220;locks&#8221; the object

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -