Skip to main content

How Java Differs from C and C++

If you are a familiar with C or C++ programming language, you should have found much of the syntax of Java – particularly at the level of the statements and operators —to be familiar.

Java and C

Because Java and C are so similar in some ways, it is important for C programmers to understand where the similarities end. There are a number of important differences that makes Java and C different and they are listed below:

1.      No Preprocessor
2.      No Global variables
3.      No Pointers
4.      No gotostatement
5.      Variable declaration anywhere
6.      No more struct and union
7.      Well-defined primitive type sizes


No Preprocessor

Java does not include a preprocessor and does not define any analogs of the #define, #include directives. There is no need of #include because Java has no header files. The reason for not including preprocessor is that, if any programmer is analyzing the code of any other programmer then he has to check every header file of the program. This task is too complex for programmer who is analyzing when program is bigger in size.

In Java, you can obtain the effects of #define by using constants.  You don’t need header files because the Java compiler compiles class definitions into a binary form that saves all type of information through the link time.

By removing all this burden of header files, Java becomes remarkably context-free. Programmers can read and understand code and, more importantly, modify and reuse code much faster and easier.

No Global variables

Java defines a very clean namespace. Packages contain classes, classes contain fields and methods, and methods contain local variables. But there are no global variables in Java, and, thus, there is no possibility of namespaces crashes among those variables. Global variables are harder to read, test and maintain.

No Pointers

Most developers agree that the misuse of pointers causes the majority of bugs in C programming. Put simply, when you have pointers, you have the ability to spoil memory. C programmers regularly use complex pointer arithmetic to create and maintain dynamic data structures. In return, C programmers spend a lot of time hunting down complex bugs caused by their complex pointer arithmetic.

The Java language does not support pointers. Java provides similar functionality by making heavy use of references. Java passes all arrays and objects by reference. This approach prevents common errors due to pointer mismanagement. It also makes programming easier in a lot ways because the correct usage of pointers is easily misunderstood by all but the most seasoned programmers.

You might be thinking that the lack of pointers in Java will keep you from being able to implement many data structures such as dynamic arrays. The reality is that any pointer task can be carried out easily, and more reliably, with objects and arrays of objects. You can then benefit from the security provided by the Java runtime system; it performs boundary checking on all array indexing operations.

No goto statement

Java doesn’t support gotostatement. Use of gotoexcept in certain circumatances is regarded as poor programming practice. Java adds exception handling and labeled break and continuestatements to the flow-control statements offered by         C. These are good alternatives for goto.

In C language the control was transferred via the dangerous gotostatement. As a result, there was too much jumping around the program, often without any clear indication of how, where and why the control is flowing. gotoshould not be used in programs at all. They have a tendency to make code unstructured and they make programs difficult to manage when they become significantly large. Removing gotoled to a simplification of the language.

Variable declarations anywhere

C requires local variable declarations to be made at the beginning of a method or block, while Java allows them anywhere in a method or block. Many programmers prefer to keep all their variable declarations grouped together at the top of a method, however.

No more Struct and Union

Java has no structures or unions as complex data types. Java forces programmers to use classes when the functionality of the structure and unions is desired. Although this sounds like more work for the programmer, it actually ends up being more consistent, because classes can imitate structures and unions with ease. The Java designers really wanted to keep the language simple, so it only made sense to remove aspects of the language that overlapped.

Well-defined primitive type sizes

All primitive types in Java have well-defined sizes. In C, the size of short, intand long types is platform independent, which hampers portability.


Java and C++

It is no secret that Java language is highly derived from the C++ language. Because C++ is currently one of the most programming languages, it is important to understand the aspects of C++ that Java inherits. Of possibly even more importance are the aspects of C++ that Java doesn’t support. Because Java is entirely new language, it was possible for the languages architects at Sun to pick and choose which feature from C++ to implement in Java and how to implement them.

Some points which makes difference between Java and C++ are listed below:

1.      Functions
2.      Multiple Inheritance
3.      Strings
4.      Operator Overloading
5.      Automatic Coercions
6.      Variable Arguments
7.      Command-line Arguments


Functions

In C, code is organized into functions, which are defaulted as global subroutines accessible to a program. C++ added classes and, in doing so, provided class methods, which are functions that are connected to classes. C++ class methods are very similar to Java class methods. However, because C++ still supports C, nothing is discouraging C++ programmers from using functions. This results in a mixture of function and method use that makes for confusing programs.
Java has no functions. Being a more pure object-oriented language than C++, Java forces programmers to bundle all routines into class methods. No limitation is imposed by forcing programmers to use methods instead of functions. As a matter of fact, implementing routines as methods encourage programmers to better organize code. Keep in mind that, strictly speaking, nothing is wrong with the procedural approach of using functions; it just doesn’t mix well with the object-oriented paradigm that defines the core Java.

Multiple Inheritance

Multiple inheritance is a feature of C++ that allows you to derive a class from multiple parent classes. Although multiple inheritance is indeed powerful, it is complicated to use correctly and causes lots of problems otherwise. It is also very complicated to implement from the compiler perspective.
Java takes the high road and provides no direct support for multiple inheritance. You can implement functionality similar to multiple inheritance by using interfaces in Java. Java interfaces provide object method descriptions, but contain no implementations.

Strings

C++ has no built-in support for text strings. The standard technique adopted among C++ programmers is that of using null-terminated arrays of characters to represent strings. In Java, strings are implemented as first class objects (string and stringbuffer), meaning that they are at the core of the Java language. Java’s implementation of strings as objects provides several advantages:
·         The manner in which you create strings and access the elements of strings is consistent across all strings on all systems.
·         Because the Java string classes are defined as part of the Java language, and not part of some extraneous extension, Java strings function predictably every time.
·         The Java string classes perform extensive runtime checking, which helps removing troublesome runtime errors.

Operator Overloading

Operator overloading, which is considered a prominent feature in C++, is not supported in Java. Although roughly the same functionality can be implemented by classes in Java, the convenience of operator overloading is still missing. However, in defense of Java, operator overloading can sometimes become very tricky. Undoubtedly the Java developers decided not to support operator overloading in order to keep the Java language as simple as possible. However, the (+) and (+=) operators are automatically overloaded to concatenate strings, and to convert other types to string in the process.

Automatic Coercions

Automatic coercionrefers to implicit casting of data types that sometimes occurs in C and C++. For example, in C++ you are allowed to assign a float value to an intvariable, which can result in a loss of information. Java doesn’t support C++ style automatic coercions. In Java, if coercion will result in a loss of data, you must always explicitly cast the data element to the new type.

Variable Arguments

C and C++ allow you to declare functions that take a variable number of arguments, such as printf. Although this is a convenient feature, it is impossible for the compiler to thoroughly type check the arguments, which means problems can arise at runtime without you knowing it. Again Java takes the high road and doesn’t support variable arguments at all.

Command-line Arguments

The command-line arguments passed from the system into a Java program differ in a couple of ways from the command-line arguments passed into a C++ program. First, the number of parameters passed differs between the two languages. In C++, the system passes two arguments to a program: argcand argv. argcspecifies the number of arguments stored in argv. argvis a pointer to an array of characters containing the actual arguments. In Java, the system passes a single value to a program: args. argsis an array of strings that contains the command line arguments.
In C++, the command-line arguments passed into a program include the name used to invoke the program. This name always appears as the first argument, and it is rarely ever used. In Java, you already know the name of the program because it is the same name as the class, so there is no need to pass this information as a command-line argument. Therefore, the Java runtime system only passes the arguments following the name that invoked the program.



Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...