Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

Java Tutorial
Java Introduction
Object Oriented Programming Concepts
Anatomy of a Java Application
Syntax and Semantics of Java
Java Objects, Classes, and Interfaces
The String and StringBuffer Classes in Java
Setting Program Attributes in Java
Using System Resources in Java
Threads of Control in Java
Errors and Exceptions in Java
Java Input and Output Streams
Overview of Java Applet
Creating an Applet User Interface in Java
Communicating with Other Programs in Java
Overview of the Java UI
Using GUI Building Blocks in Java
Laying Out Components within a Container
Working with Graphics in Java
How Java Differs from C and C++
Java Summary

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


How Java Differs from C and C++

Previous Next



pointers

Java language passes all the arrays and objects by reference but will not have an explicit pointer type. You cannot construct a reference to anonymous memory. In addition of making the programming easier, this prevents the common errors due to the pointer mismanagement.




structures and unions

The Java language do not support either structures or unions. Instead they use classes or interfaces to build the composite types.




The Command Line Arguments

Command line arguments passed to the Java application are different in number and in types than those passed to C or C++ programs.




Number of Parameters passed

In C and C++ when the program is invoked, the system passes two parameters to it: These arguments are shown below.

argc--the number of arguments passed on the command line
argv-- the pointer to an array of strings that contain these arguments

When we invoke a Java application, the system passes only one parameter to it:

args-- array of Strings (it is just an array and not a pointer to an array) which contain the arguments




First Command Line Argument

In C and C++ programming, the system passes the whole command line to the program as its arguments, including a name used to invoke it. Consider for example, if we invoked the C program like this: Then first argument in the argv parameter is diff.

diff file1 file2

In Java Programming, you always know the name of application because it is the name of class where the main method is been defined. So, the Java runtime system does not pass the class name we invoke to main method. Instead, the system passes only the those items on the command line which appears after the class name. Consider for example, if we invoked a Java application like this:

java diff file1 file2 The first command line argument is file1.




The Java Strings are First-Class Objects

By convention, the C and C++ strings are the null-terminated array of characters; there is no such real entity in C and C++ that is a string. The Java strings are first-class objects.




C Strings Behaves Unpredictably

To illustrate why this is one of the important feature of Java programming language, let us look at the small example. This C function will copy the contents of str1 into str2.

int myStrCopy(char *str1, char *str2)
{
for ( ; *str1 != '\0'; str1++, str2++)
*str2 = *str1;
}

In the above example, the developer uses the pointer arithmetic to step through both the strings copying one into the other. While allowing the programmers to inspect the arbitrary memory locations through pointers is the powerful tool, this power can be a source of many errors. One of the fruitful source of errors is pointers which stray off the end of the array. myStrCopy function above has such an error: the for loop in the function will not check the length of a string str2, and if the string str1 is longer compared to str2 the string copy writes right over the end of the string str2. Here is a program which tickles the bug.

main()
{
char *s = "HotJava is Cool!";
char t[] = "Java is Cool!";

printf("%s, %s\n", s, t);
myStrCopy(s, t);
printf("%s, %s\n", s, t);
}

The myStrCopy function writes over the end of string str2 thereby corrupting all that which was stored in the memory after it. NOTE: the %s, %s are the characters that happened to be stored in a memory location after the string str2 and will probably be different when we run the program on our machine.




The Java Strings are Predictable

Java strings are the first-class objects deriving either from a String class or a StringBuffer class. This makes the finding and fixing the whole class of common and frustrating programming errors such as the one illustrated the below trivial. Here is the program above (including an error) rewritten in the Java language.

class strcpy
{
public static void main(String args[])
{
String s = "HotJava is Cool!";
StringBuffer t = new StringBuffer("Java is Cool!");

System.out.println(s + ", " + t);
myStrCopy(s, t);
System.out.println(s + ", " + t);
}

static void myStrCopy(String str1, StringBuffer str2)
{
int i, len = str1.length();

for (i = 0; i < len; i++)
str2.setCharAt(i, str1.charAt(i));
}
}

Notice that this translation will do use the String class, the StringBuffer class and the methods which are appropriate to obtain a specific characters instead of the character arrays and the pointers.

Like the C version, Java programming versions of the myStrCopy method loops over the length of the str1 and never checks length of str2. Thus, when the string str1 is longer than the string str2, the method will try to obtain the characters beyond the end of the end of the str2. However, when we run the Java language version, we will see the following runtime error message.

Exception in thread "main" java.lang.StringIndexOutOfRangeException String index out of range: 13
at java.lang.Exception.< init >(Exception.java)
at java.lang.StringIndexOutOfRangeException.< init >(StringIndexOutOfRangeException.java)
at java.lang.StringBuffer.setCharAt(StringBuffer.java)
at strcpy.myStrCopy(strcpy.java:23)
at strcpy.main(strcpy.java:15)

The primary difference between the Java Programming language version of this program and the C, is that the Java program reliably and obviously will crash, where as the C program will do something obscure.




Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previous Next

Keywords: java download, java games, javascript, sun java, download microsoft java virtual machine, sun java free download, java chat rooms, java update, yahoo java messenger, java certification


HTML Quizes
HTML Quiz
XHTML Quiz
CSS Quiz
TCP/IP Quiz
XML Quizes
XML Quiz
XSL Quiz
XSLT Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XSL-FO Quiz
XML DOM Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
SOAP Quiz
WSDL Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizes
JavaScript Quiz
VBScript Quiz
AJAX Quiz
DHTML Quiz
HTML DOM Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizes
ASP Quiz
PHP Quiz
PERL Quiz
SQL Quiz
ADO Quiz
.NET (dotnet) Quizes
Microsoft.Net Quiz
XML Web Services Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
ADO.NET Quiz
VB.NET Quiz
Multimedia Quizes
SVG Quiz
Flash Quiz
Media Quiz
SMIL Quiz
Web Building  Quizes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Java Quizes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Programming Langauges Quizes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Soft Skills Quizes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz

Privacy Policy
Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.