Sie sind auf Seite 1von 27

There are a number of C++ features

that Java does not support. In some


cases, a specific C++ feature simply
didn't relate to the Java environment.
In other cases, the designers of Java
eliminated some of the duplication of
features that exists in C++. In still
other instances, a feature of C++ is
not supported by Java because it
was deemed too dangerous for
Internet applets.

Perhaps the single biggest difference


between Java and C++ is that Java
does not support pointers. As a C++
programmer you know that the
pointer is one of C++'s most powerful
and important language features. It is
also one of its most dangerous when
used improperly. Pointers don't exist
in Java for two reasons:
* Pointers are inherently insecure.
For example, using a C++-style
pointer, it is possible
to gain access to memory
addresses outside a program's code
and data. A malicious
program could make use of this
fact to damage the system, perform
unauthorized
accesses (such as obtaining
passwords), or otherwise violate
security restrictions.
* Even if pointers could be
restricted to the confines of the Java
run-time system (which
is theoretically possible, since
Java programs are interpreted), the
designers of Java
believed that they were inherently
troublesome.
Note Since pointers don't exist in
Java, neither does the -> operator.

Here are a few more of the most


important "omissions":

* Java does not include structures


or unions. These were felt to be
redundant since the
class encompasses them.
* Java does not support operator
overloading. Operator overloading is
sometimes a
source of ambiguity in a C++
program, and the Java design team
felt that it causes
more trouble than benefit.
* Java does not include a
preprocessor nor does it support the
preprocessor directives.
The preprocessor plays a less
important role in C++ than it does in
C. The designers
of Java felt that it was time to
eliminate it entirely.
* Java does not perform any
automatic type conversions that
result in a loss of
precision. For example, a
conversion from long integer to
integer must be explicitly
cast.
* All the code in a Java program is
encapsulated within one or more
classes. Therefore,
Java does not have what you
normally think of as global variables
or global functions.
* Java does not allow default
arguments. In C++, you may specify
a value that a
parameter will have when there is
no argument corresponding to that
parameter when
the function is invoked. This is
not allowed in Java.
* Java does not support the
inheritance of multiple superclasses
by a subclass.
* Although Java supports
constructors, it does not have
destructors. It does, however,
add the finalize( ) function.
* Java does not support typedef.
* It is not possible to declare
unsigned integers in Java.
* Java does not allow the goto.
* Java does not have the delete
operator.
* The << and >> in Java are not
overloaded for I/O operations.
* In Java, objects are passed by
reference only. In C++, objects may
be passed by
value or by reference.

There are a number of features in


Java that have no equivalent in C++.
Perhaps the three most important are
multithreading, packages, and
interfaces, but there are several
others that enrich the Java
programming environment as well.

* Multithreading allows two or


more pieces of the same program to
execute concurrently. Further,
this approach to concurrence is
supported at the
language level. There is no
parallel for this in C++. If you need to
multithread a C++
program, you will need to do so
manually, using operating system
functions. While
both methods allow for
concurrent execution of two or more
threads, Java's approach
is cleaner and easier to use.
* There is no feature in C++ that
directly corresponds to a Java
package. The closest
similarity is a set of library
functions that use a common header
file. However,
constructing and using a library in
C++ is completely different from
constructing and
using a package in Java.
* The Java interface is somewhat
similar to a C++ abstract class. (An
abstract class in
C++ is a class that contains at
least one pure virtual function.) For
example, it is
impossible to create an instance
of a C++ abstract class or a Java
interface. Both are
used to specify a consistent
interface that subclasses will
implement. The main
difference is that an interface
more cleanly represents this concept.
* Java has a streamlined approach
to memory allocation. Like C++, it
supports the new
keyword. However, it does not
have delete. Instead, when the last
reference to an
object is destroyed, the object,
itself, is automatically deleted the
next time that
garbage collection occurs.
* Java "removes" the C++
standard library, replacing it with its
own set of API classes.
While there is substantial
functional similarity, there are
significant differences in the
names and parameters. Also,
since all of the Java API library is
object-oriented, and
only a portion of the C++ library
is, there will be differences in the way
library routines
are invoked.
* The break and continue
statements have been enhanced in
Java to accept labels as
targets.
* The char type in Java declares
16-bit-wide Unicode characters. This
makes them
similar to C++'s wchar_t type.
The use of Unicode helps ensure
portability.
* Java adds the >>> operator,
which performs an unsigned right
shift.
* In addition to supporting single-
line and multiline comments, Java
adds a third
comment form: the
documentation comment.
Documentation comments begin with
a
/** and end with a */.
* Java contains a built-in string
type called String. String is
somewhat similar to the
standard string class type
provided by C++. Of course, in C++
string is only available if
you include its class declarations
in your program. It is not a built-in
type.
If you are a beginner please read
(History of Java) and (What is Java?)
before proceeding.

If you are familiar with C then Java


should be very familiar to you in
terms of syntax. If you are a C++
programmer the Java will be easier
still since Java is also purely an
Object Oriented Programming
without some of the features like
Pointers in C++.

To start programming your first Java


program, you need to have some
pre-requisites installed on your
computer. As with any other
programming language, you first
need to install the SDK (Software
Developer Kit) in this case it is JDK
(Java Developers Kit). Now
choosing which JDK to download
maybe a little tricky if you are new to
Java. So it will be advisable to read
the How Java is Organised? before
you proceed.

You have two options to get started


with Java.

1. Manual

* If you want to do it manually, you


will need to download a J2SE from
Sun website and install on your
computer. (The J2SE has many
versions and it is better to check at
the Sun website to get the latest
JDK.)
* The J2SE download is usually an
executable file which will install
automatically like any windows
software.
* Once the installation is
successful, open a command line
(DOS Window) and type 'Java' and
'Enter'. (If you see 'Bad command'
then probably your PATH settings
are wrong. In which add the
installation path to the PATH variable
from your 'computer properties'
environment variables section.)
* Open a Notepad, write your first
sample program (you will find many
java samples here) and save it as
.java file (the name of the file should
be same as the name of the CLASS
in the program)
* from the command window,
change current directory to where
you saved your .java file and compile
the program ( Use 'javac' command
followed by the name of the file to
compile)
* Once it is compiled a .class file
will be created in the same folder.
Then run the program using 'Java'
command (Use 'Java' command
followed by the name of the class)

2. Automated

* As with any other programming


language, Java too has many free
IDE (Integrated Development
Environments) You can choose any
of them. Most of them comes
bundled with the required JDKs.
* One of the widely used Java
development tool is the Sun Studio.
You can download it from the Sun
website.
* Or you can download and use
the net beans
For the most part, it is quite easy to
convert a C++ function that uses
pointer parameters into its equivalent
Java method. Since Java passes all
objects by reference, sometimes the
conversion simply requires the
removal of C++'s pointer operators.
For example, consider this C++
program that reverses the signs of a
Coord object, which stores a pair of
Cartesian coordinates. The function
reverseSign( ) is passed a pointer to
the Coord object that will be
reversed. As you can see, C++'s *, &,
and -> pointer operators are used to
perform the operation.

// Reverse the signs of a


coordinate - C++ version.
#include <iostream>
using namespace std;
class Coord {
public:
int x;
int y;
};

// Reverse the sign of the


coordinates.
void reverseSign(Coord *ob) {
ob->x = -ob->x;
ob->y = -ob->y;
}
int main()
{
Coord ob;
ob.x = 10;
ob.y = 20;
cout << "Original values for ob: ";
cout << ob.x << ", " << ob.y <<
"\\n";
reverseSign(&ob);
cout << "Sign reversed values for
ob: ";
cout << ob.x << ", " << ob.y <<
"\\n";
return 0;
}

This program can be recoded into


the following Java version. As you
can see, most of the conversion
involves the deletion of the C++
pointer operators. Since Java passes
objects by reference, changes to the
parameter automatically affect the
argument.

// Reverse the signs of a


coordinate - Java version.
class Coord {
int x;
int y;
};

class DropPointers {
// Reverse the sign of the
coordinates.
static void reverseCoord(Coord
ob) {
ob.x = -ob.x;
ob.y = -ob.y;
}

public static void main(String


args[]) {
Coord ob = new Coord();
ob.x = 10;
ob.y = 20;
System.out.println("Original values
for ob: " +
ob.x + ", " + ob.y);
reverseCoord(ob);
System.out.println("Sign reversed
values for ob: " +
ob.x + ", " + ob.y);
}
}

The output from both of these


programs is the same and is shown
here:

Original values for ob: 10, 20


Sign reversed values for ob: -10, -20
*, &, and -> pointer operators are
used to perform the operation.

Internationalization involves many


aspects of application development.
Practically speaking, the primary goal
behind all of these facets of
development is to engineer a user
interface -and its supporting
infrastructure - that presents all UI
information in a comprehensible way
to local users. At a minimum, this
effort involves supporting the
following aspects of an application's
execution:

* Messaging— presentation of all


visible text (message text, error text,
UI component titles,
prompts, and so forth) in the
language of the appropriate runtime
locale context.
* Formatting policy— use of the
correct locale-specific formats for all
date, time, and
numeric quantities.
* Calendar and time zone policy—
use of the correct calendar for the
application's runtime
locale.
* String collation policy— use of an
appropriate policy for string collation
based on the
locale's language.
* General UI features, locale-
sensitive images, icons, and colors—
using images and colors
that represent meaningful
mnemonics to local users.

To support the foregoing features, an


internationalized application must
perform some dynamic configuration
and information retrieval. Typically,
an application will determine its
locale context dynamically upon
startup. Then, it will configure all the
necessary runtime components—
such as calendar objects, string
collators, format objects and
messaging components—that it
needs to conform to the locale
context requirements.

Messaging: Messaging is the


presentation of all text data to the
user in a language appropriate for
the application's runtime locale
context. It's the most visible area of
i18n. Messaging involves the
execution of the following steps at
runtime:

• determination of the device locale


environment
• loading of the application's localized
resources
• dynamic lookup and retrieval of
localized resources for UI display
• display of localized resources

Messaging is the area that best


highlights the close relationship
between i18n and l10n. To make an
i18n implementation usable, an
application must be localized. For
each locale supported, the l10n
process must produce a set of
translated message strings that the
application can access at runtime.

String Collation: String collation, also


known as lexicographic sorting, is
different from messaging.
Nevertheless, the two areas are
related in the sense that collation
functions manipulate message text—
text that the user sees.

Different languages define different


rules for collation. A string collation
facility must use a mechanism that
understands the collation rules for
the strings' language context.
Practically, this includes an
understanding of the details of the
underlying character encoding set.
Applications do string collation with
no knowledge of the source of the
string text. That is, a collation
function doesn't retrieve collated
strings from some repository of
localized text. Instead, the program
collates strings that have already
been retrieved from a localized
repository. Collation functions don't
need to know the original location of
the string. They only need a
language context and a properly
encoded string.
Date, Time, Numeric, and Monetary
Value Formatting: Different locales
use different formats for writing
dates, times, and numbers. For
instance, in Europe, people write
dates, times and numbers differently
from people in the United States. A
French user writes date, time, and
numeric quantities using the
following forms.

25 décembre 2002
2002/12/25
25/12/2002
08.30
14.45
20.000,45 (twenty thousand, and
forty-five hundredths)

In the United States, however, these


same quantities would normally be
written as follows.

December 25, 2002


12/25/2002
8:30 am
2:45 pm
20,000.45 (twenty thousand, and
forty-five hundredths)

An internationalized program needs


to format and display dates, times,
and numbers appropriately for the
runtime locale. Programs don't fetch
these formatted quantities from some
database; they calculate them
dynamically in the same way that
strings are collated dynamically.

Calendar and Time Zone Support:


Calendars, although related to dates
and times, define different
characteristics and functionality. The
difference is that calendars perform
calculations that involve dates and
times, whereas date and time objects
support the formatting and display of
those quantities.

Das könnte Ihnen auch gefallen