Quantcast
Viewing latest article 4
Browse Latest Browse All 10

Why The Programmers Use Java?

The Java syntax resembles with the C and C + + languages known by the programmers and not only by them. This article aims to show the parallels between Java and C / C + +. I will describe a sum of differences and some similarities between them and I will present elements that are in Java and are not in C+ + and  elements that stayed in C + +, but there were removed from Java.

Java supports eight basic data types. Note that Java adds two data types: byte, and boolean. (Some C + + compilers have added these newer type called boolean). One important difference on other types of data that are common to the two languages is that Java types have a fixed and known size. This is very important for Java due to its purpose of being portable. For example, if a data type occupies 16 bits on a platform and 32 bits on another platform, it will have problems to run on both platforms. C + + guarantees a certain relationship between the primary data types, for example, guarantees that a long data type is at least as large as a regular data type. But it does not guarantee the size of each type.

Image may be NSFW.
Clik here to view.
Java achieves this, each type having a fixed size. Since most machines run on 32-bit systems, the data types sizes were designed to be optimized for 32-bit. Thus, once a Java data type occupies 32 bits (compared to 16 or 32 bits in C / C + +), the data type long will occupy 64 bits (compared to 32 or 64 bits in C / C + + ). Another primary difference is that Java uses a sign. Thus, the unsigned statements from C are not allowed in Java.

And in Java and C + + you can convert between a data type and another. But in Java there is no implicit conversion. Consider the following sequence of a program written in C:
long LongNb = 54309;
int IntNb;
IntNb = LongNb;.

Image may be NSFW.
Clik here to view.
The compiler C / C + + will make an implicit conversion (cast) from long to int. On a 16 bit platform (where long has 32 bits and int 16 bits in length), after the conversion, IntNb variable will have the value 0. So there will be a loss of precision, without the programmer to be aware. Java eliminates the risk of potential errors in programming related to conversions by not converting the data. Thus the programmer is forced to make an explicit conversion (for example: IntNb = (int) LongNb;).

Now we have to talk about operators. The set of operators in Java is almost identical to the one from C / C + +. They are:! (Negation), & & (conditional and), | | (conditional or)?: (condition). A difference is that in Java they operate with Boolean values. This sequence made in C:
int x = 6;
int y = 7;
if (x & & y) {
/ / code sequence
}
will be ackward in Java, because as I said above, there is no automatic conversion. The condition will therefore be written explicitly: if (X! = 0 & & Y! = 0). Another difference of the operators which has a great importance, is that Java operators cannot be overloaded, like in C + +. Using this feature in C + + has led to many errors. Java developers have therefore decided not to retain this feature.

Pointers in C + + represent an element that gives the programmer more flexibility. However, the use of pointers is an important source of errors. Java does not allow the programmer to use pointers of any kind. How is it then sending variables? Read further to find out.

In C + + programmer is free to pass variables as he considers best using the operators &, * and ->. In Java these operators do not exist, but there is the following rule: the primary data types are passed by value (the actual copying), and massive objects and are passed by reference (by copying the address).

Consider the following example: we want to create a function to return the mark of a student:
void mark of the student (int grades [], double mark)
{
int sum = 0;
for (int i = 0; i <10; i + +)
sum + = grades [i];
mark = sum/10;
}

We cannot return the result as a parameter, because it is passed by value and its change inside the function has no effect on the variable. In C, this parameter should be transmitted by the address (int *mark or int &mark). Therefore a solution must be searched of transmitting the parameter through an address. This is done by creating a class that contains the “mark” variable. An object of this new class will be passed as a parameter by the following address:
public class defineStudent
{
double mark;
}
and the function above will take the form:
void markStudent(int grades [], defineStudent stud)
{
int sum = 0;
for (int i = 0; i <10; i + +)
sum + = marks [i];
stud.mark = sum/10;
}
Of course, the return of the mark could be made like in the above case through the return of the function:
markStudent double (int grades []) {…} but if you wish to return more basic values, creating a class that contains them remains the only valid solution.11


Viewing latest article 4
Browse Latest Browse All 10

Trending Articles