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

📄 tij0198.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0197.html">Prev</a> | <a href="tij0199.html">Next</a>
</td>
</tr></table>
<hr>

<H1 ALIGN=LEFT>
B:
Comparing C++ and Java
</H1>
<DIV ALIGN=LEFT><FONT FACE="Calligraph421 BT" SIZE=4 COLOR="Black">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><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">However,
there are a surprising number of differences between <A NAME="Index3124"></A><A NAME="Index3125"></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><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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="Carmina Md BT" SIZE=3 COLOR="Black">	Java
has both kinds of comments like C++ does.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Everything
must be in a class. There are no global functions or global data. If you want
the equivalent of globals, make 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data within a class. There are no structs or enumerations or unions, only
classes.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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="Carmina Md BT" SIZE=3 COLOR="Black">	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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>class
foo,
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
only class definitions.
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">class
aType {
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">
 void aMethod(&#160;) { /* method body */ }
</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">}</FONT></TT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	There&#8217;s
no scope resolution operator 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>::</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods: you say 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ClassName.methodName(&#160;);</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In addition, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>package</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
names are established using the dot, and to perform a kind of C++ 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>#include</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you use the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword. For example: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import
java.awt.*;
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>#include</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
does not directly map to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but it has a similar feel to it).
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Java,
like C++, has primitive types for efficient access. In Java, these are 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>byte</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>short</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>long</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>float</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>double</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">1.
Conditional expressions can be only
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
boolean
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
not integral.
</FONT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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="Carmina Md BT" SIZE=3 COLOR="Black">	The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
type uses the international 16-bit Unicode character set, so it can
automatically represent most national characters.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Static
quoted strings are automatically converted into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. There is no independent static character array string like there is in
C and C++.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Java
adds the triple right shift 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&gt;&gt;&gt;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to act as a &#8220;logical&#8221; right shift by inserting zeroes at the top
end; the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&gt;&gt;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inserts the sign bit as it shifts (an &#8220;arithmetic&#8221; shift).
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>length</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
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="Carmina Md BT" SIZE=3 COLOR="Black">	All
objects of non-primitive types can be created only via 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
There are wrapper classes for all primitive classes so that you can create
equivalent heap-based objects via 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
(Arrays of primitives are a special case: they can be allocated via aggregate
initialization as in C++, or by using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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="Carmina Md BT" SIZE=3 COLOR="Black">	Java
has no preprocessor. If you want to use classes in another library, you say 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the name of the library. There are no preprocessor-like macros.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a package and the compiler takes care of the rest.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	Object
handles defined as class members are automatically initialized to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and non-
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members alike. You don&#8217;t need to externally define storage for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>static</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
members like you do in C++.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	There
are no Java pointers in the sense of C and C++. When you create an object with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you get back a reference (which I&#8217;ve been calling a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>handle</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in this book). For example: 
</FONT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">String
s = new String(&#8220;howdy&#8221;);
</FONT></TT><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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><P><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>arithmetic</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	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="Carmina Md BT" SIZE=3 COLOR="Black">	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 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finalize(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that&#8217;s a member of each class, something like a C++ destructor, but 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>finalize(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

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