Tuesday, January 21, 2014

Syntax Differences Between C++ And Java

Are you having a tough time differentiating between C++ and Java? Do you keep using the semicolon when you're not supposed to? While the two languages are similar, there are certain syntactical differences that can be confusing. Here’s something that will come in handy.

C++, Java, java programming, C++ programming, programming tips, C++ Java difference, C++ syntax, Java syntax, Java C++ syntax difference, Java tips, C++ tips, tech news, news




The main function

C++

int main( int X, char* Y[])
{
printf( "Hello, world" );
}

Java

Every function in Java has to be made a part of a class. So, the main function also has to be a part of a class. Moreover, in Java there is one main() function for every class. This can come in handy when writing unit tests for the class.

class HelloWorld
{
public static void main(String X[])
{
System.out.println( "Hello, World" );
}
}

Compiling

C++

In C++, you will be compiling as,

g++ X.cc -o outfile

This will then be run with,

./outfile

Java

In Java, you will compile the classes in X.java.

javac X.java

You have to run this by invoking the static main method.

Class declarations

While C++ requires a semicolon at the end of class declarations, Java does not have any such requirement.

C++

class X{};

Java

class X{}

Method declarations

A method declaration in Java must be a part of a class always. Otherwise, both the languages are quite the same syntactically on this front. You can also use the public, private and protected access specifications in Java.

Constructors and destructors

In the case of constructors, the syntax in both C++ and Java is the same. Java though has no exact replacement for destructors.

Static members

Static members (variables and functions) are also declared in the same way in both the languages. But, Java allows for static initialisation blocks in order to initialise static variables.

class Foo
{
static private int x;
// static initialization block
{ x = 5; }
}

Scoop static namespaces and methods

C++

In C++, you will use the Class::method form in order to scoop static methods.

class MyClass
{
public:
static do();
};

Use this by,

MyClass::do();

Java

In Java, on the other hand, scooping comes with the use of the .again parameter. This is similar to accessing the fields of a class.

class MyClass
{
public static do()
{
// do something
}
}

To use the static method.

MyClass.doStuff();

Object declarations
C++

// on the stack
myClass x;

// or on the heap
myClass *x = new myClass;

Accessing fields from various objects

C++

In order to access fields from classes and other such objects, the programmer has to use dots.

myClass x;
x.field;

If a pointed is involved, then the narrow operator (->) has to be used.

myClass x = new MyClass();
x->field;

Java

In Java, only the dot is used. Since we always use references in Java, even pointers are accessed using the dot.

myClass x = new MyClass();
x.field;

Protection levels

They are specified in a different manner.

C++

public:
void X();
void Y();

Java

public void X();
public void Y();


Virtual functions

C++

virtual int X();

Virtual functions can also be used non virtually. You can simply say into X().

Java

int foo(); // or, final int foo();

In Java, functions are always virtual by default. You use ‘final’ in order to avoid them from being overridden.

Abstract Classes

C++

All you need to do is include public virtual functions./

class X{ public: virtual void X() = 0; };

Java

In Java the syntax allows the programmer to be explicit.

abstract class X{ public abstract void X(); }

If you want to specify an interface, then say,
interface X{ public void foo(); }

In that case though, you will have to implement it by,
class Y implements X
{
public void M() { /* do something */ }
}

Memory management

This is also pretty much the same in both the languages, except that Java has garbage collection, so no delete for Java.

No comments:

Post a Comment