sample java
TRANSCRIPT
-
8/8/2019 Sample Java
1/27
There are a number of C++ featuresthat Java does not support. In some
cases, a specific C++ feature simplydidn't relate to the Java environment.In other cases, the designers of Javaeliminated some of the duplication offeatures that exists in C++. In still
other instances, a feature of C++ isnot supported by Java because itwas deemed too dangerous forInternet applets.
Perhaps the single biggest differencebetween Java and C++ is that Javadoes not support pointers. As a C++programmer you know that thepointer is one of C++'s most powerfuland important language features. It isalso one of its most dangerous whenused improperly. Pointers don't existin Java for two reasons:
-
8/8/2019 Sample Java
2/27
* Pointers are inherently insecure.
For example, using a C++-stylepointer, it is possible
to gain access to memoryaddresses outside a program's codeand data. A malicious
program could make use of thisfact to damage the system, performunauthorized
accesses (such as obtainingpasswords), or otherwise violate
security restrictions.* Even if pointers could berestricted to the confines of the Javarun-time system (which
is theoretically possible, sinceJava programs are interpreted), thedesigners of Java
believed that they were inherentlytroublesome.
-
8/8/2019 Sample Java
3/27
Note Since pointers don't exist in
Java, neither does the -> operator.
Here are a few more of the mostimportant "omissions":
* Java does not include structuresor unions. These were felt to beredundant since the
class encompasses them.* Java does not support operator
overloading. Operator overloading issometimes asource of ambiguity in a C++
program, and the Java design teamfelt that it causes
more trouble than benefit.* Java does not include a
preprocessor nor does it support thepreprocessor directives.
-
8/8/2019 Sample Java
4/27
The preprocessor plays a lessimportant role in C++ than it does in
C. The designersof Java felt that it was time to
eliminate it entirely.* Java does not perform any
automatic type conversions that
result in a loss ofprecision. For example, aconversion from long integer tointeger must be explicitly
cast.
* All the code in a Java program isencapsulated within one or moreclasses. Therefore,
Java does not have what younormally think of as global variablesor global functions.
* Java does not allow defaultarguments. In C++, you may specifya value that a
-
8/8/2019 Sample Java
5/27
parameter will have when there isno argument corresponding to that
parameter whenthe function is invoked. This is
not allowed in Java.* Java does not support the
inheritance of multiple superclasses
by a subclass.* Although Java supportsconstructors, it does not havedestructors. It does, however,
add the finalize( ) function.
* Java does not support typedef.* It is not possible to declareunsigned integers in Java.
* Java does not allow the goto.* Java does not have the delete
operator.* The > in Java are not
overloaded for I/O operations.* In Java, objects are passed by
-
8/8/2019 Sample Java
6/27
-
8/8/2019 Sample Java
7/27
-
8/8/2019 Sample Java
8/27
C++ is a class that contains atleast one pure virtual function.) For
example, it isimpossible to create an instance
of a C++ abstract class or a Javainterface. Both are
used to specify a consistent
interface that subclasses willimplement. The maindifference is that an interface
more cleanly represents this concept.* Java has a streamlined approach
to memory allocation. Like C++, itsupports the newkeyword. However, it does not
have delete. Instead, when the lastreference to an
object is destroyed, the object,itself, is automatically deleted thenext time that
garbage collection occurs.
-
8/8/2019 Sample Java
9/27
* Java "removes" the C++standard library, replacing it with its
own set of API classes.While there is substantial
functional similarity, there aresignificant differences in the
names and parameters. Also,
since all of the Java API library isobject-oriented, andonly a portion of the C++ library
is, there will be differences in the waylibrary routines
are invoked.* The break and continuestatements have been enhanced inJava to accept labels as
targets.* The char type in Java declares
16-bit-wide Unicode characters. Thismakes them
similar to C++'s wchar_t type.
-
8/8/2019 Sample Java
10/27
The use of Unicode helps ensureportability.
* Java adds the >>> operator,which performs an unsigned rightshift.
* In addition to supporting single-line and multiline comments, Java
adds a thirdcomment form: thedocumentation comment.Documentation comments begin witha
/** and end with a */.* Java contains a built-in stringtype called String. String issomewhat similar to the
standard string class typeprovided by C++. Of course, in C++string is only available if
you include its class declarationsin your program. It is not a built-in
-
8/8/2019 Sample Java
11/27
type.If you are a beginner please read
(History of Java) and (What is Java?)before proceeding.
If you are familiar with C then Javashould be very familiar to you in
terms of syntax. If you are a C++programmer the Java will be easierstill since Java is also purely anObject Oriented Programmingwithout some of the features like
Pointers in C++.
To start programming your first Javaprogram, you need to have somepre-requisites installed on yourcomputer. As with any otherprogramming language, you firstneed to install the SDK (SoftwareDeveloper Kit) in this case it is JDK
-
8/8/2019 Sample Java
12/27
(Java Developers Kit). Nowchoosing which JDK to download
maybe a little tricky if you are new toJava. So it will be advisable to readthe How Java is Organised? beforeyou proceed.
You have two options to get startedwith Java.
1. Manual
* If you want to do it manually, youwill need to download a J2SE fromSun website and install on yourcomputer. (The J2SE has manyversions and it is better to check atthe Sun website to get the latestJDK.)
* The J2SE download is usually anexecutable file which will install
-
8/8/2019 Sample Java
13/27
automatically like any windowssoftware.
* Once the installation issuccessful, 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 theinstallation path to the PATH variablefrom your 'computer properties'environment variables section.)
* Open a Notepad, write your first
sample program (you will find manyjava samples here) and save it as.java file (the name of the file shouldbe same as the name of the CLASSin the program)
* from the command window,change current directory to whereyou saved your .java file and compilethe program ( Use 'javac' command
-
8/8/2019 Sample Java
14/27
followed by the name of the file tocompile)
* Once it is compiled a .class filewill be created in the same folder.Then run the program using 'Java'command (Use 'Java' commandfollowed by the name of the class)
2. Automated
* As with any other programminglanguage, Java too has many free
IDE (Integrated DevelopmentEnvironments) You can choose anyof them. Most of them comesbundled with the required JDKs.
* One of the widely used Javadevelopment tool is the Sun Studio.You can download it from the Sunwebsite.
* Or you can download and use
-
8/8/2019 Sample Java
15/27
the net beansFor the most part, it is quite easy to
convert a C++ function that usespointer parameters into its equivalentJava method. Since Java passes allobjects by reference, sometimes theconversion simply requires the
removal of C++'s pointer operators.For example, consider this C++program that reverses the signs of aCoord object, which stores a pair ofCartesian coordinates. The function
reverseSign( ) is passed a pointer tothe Coord object that will bereversed. As you can see, C++'s *, &,and -> pointer operators are used toperform the operation.
// Reverse the signs of acoordinate - C++ version.
#include
-
8/8/2019 Sample Java
16/27
using namespace std;class Coord {
public:int x;int y;};
// Reverse the sign of thecoordinates.void reverseSign(Coord *ob) {ob->x = -ob->x;ob->y = -ob->y;
}int main(){Coord ob;ob.x = 10;ob.y = 20;cout
-
8/8/2019 Sample Java
17/27
reverseSign(&ob);cout
-
8/8/2019 Sample Java
18/27
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(Stringargs[]) {Coord ob = new Coord();ob.x = 10;ob.y = 20;System.out.println("Original values
for ob: " +ob.x + ", " + ob.y);reverseCoord(ob);
-
8/8/2019 Sample Java
19/27
System.out.println("Sign reversedvalues for ob: " +
ob.x + ", " + ob.y);}}
The output from both of these
programs is the same and is shownhere:
Original values for ob: 10, 20Sign reversed values for ob: -10, -20
*, &, and -> pointer operators areused to perform the operation.
Internationalization involves manyaspects of application development.Practically speaking, the primary goal
-
8/8/2019 Sample Java
20/27
behind all of these facets ofdevelopment is to engineer a user
interface -and its supportinginfrastructure - that presents all UIinformation in a comprehensible wayto local users. At a minimum, thiseffort involves supporting the
following aspects of an application'sexecution:
* Messaging presentation of allvisible text (message text, error text,
UI component titles,prompts, and so forth) in thelanguage of the appropriate runtimelocale context.
* Formatting policy use of thecorrect locale-specific formats for alldate, time, and
numeric quantities.* Calendar and time zone policy
-
8/8/2019 Sample Java
21/27
use of the correct calendar for theapplication's runtime
locale.* String collation policy use of an
appropriate policy for string collationbased on the
locale's language.
* General UI features, locale-sensitive images, icons, and colorsusing images and colors
that represent meaningfulmnemonics to local users.
To support the foregoing features, aninternationalized application mustperform some dynamic configurationand information retrieval. Typically,an application will determine itslocale context dynamically uponstartup. Then, it will configure all thenecessary runtime components
-
8/8/2019 Sample Java
22/27
such as calendar objects, stringcollators, format objects and
messaging componentsthat itneeds to conform to the localecontext requirements.
Messaging: Messaging is the
presentation of all text data to theuser in a language appropriate forthe application's runtime localecontext. It's the most visible area ofi18n. Messaging involves the
execution of the following steps atruntime:
determination of the device localeenvironment loading of the application's localizedresources dynamic lookup and retrieval oflocalized resources for UI display
-
8/8/2019 Sample Java
23/27
display of localized resources
Messaging is the area that besthighlights the close relationshipbetween i18n and l10n. To make ani18n implementation usable, anapplication must be localized. For
each locale supported, the l10nprocess must produce a set oftranslated message strings that theapplication can access at runtime.
String Collation: String collation, alsoknown as lexicographic sorting, isdifferent from messaging.Nevertheless, the two areas arerelated in the sense that collationfunctions manipulate message texttext that the user sees.
Different languages define different
-
8/8/2019 Sample Java
24/27
-
8/8/2019 Sample Java
25/27
Date, Time, Numeric, and MonetaryValue Formatting: Different locales
use different formats for writingdates, times, and numbers. Forinstance, in Europe, people writedates, times and numbers differentlyfrom people in the United States. A
French user writes date, time, andnumeric quantities using thefollowing forms.
25 dcembre 2002
2002/12/2525/12/200208.3014.4520.000,45 (twenty thousand, andforty-five hundredths)
In the United States, however, thesesame quantities would normally be
-
8/8/2019 Sample Java
26/27
written as follows.
December 25, 200212/25/20028:30 am2:45 pm20,000.45 (twenty thousand, and
forty-five hundredths)
An internationalized program needsto format and display dates, times,and numbers appropriately for the
runtime locale. Programs don't fetchthese formatted quantities from somedatabase; they calculate themdynamically in the same way thatstrings are collated dynamically.
Calendar and Time Zone Support:Calendars, although related to datesand times, define different
-
8/8/2019 Sample Java
27/27
characteristics and functionality. Thedifference is that calendars perform
calculations that involve dates andtimes, whereas date and time objectssupport the formatting and display ofthose quantities.