Pages

Wednesday, October 23, 2013

Difference Between Java & C++

 The Important and Main Difference Between The Java And C++ Programming Language.

main function

C++

// free-floating function
int main( int argc, char* argv[])
{
    printf( "Hello, world" );
}

Java

// every function must be part of a class; the main function for a particular
// class file is invoked when java <class> is run (so you can have one
// main function per class--useful for writing unit tests for a class)
class HelloWorld
{
    public static void main(String args[])
    {
        System.out.println( "Hello, World" );
    }
}

Compiling

C++

    // compile as
    g++ foo.cc -o outfile
    // run with
    ./outfile
    

Java

    // compile classes in foo.java to <classname>.class
    javac foo.java 

    // run by invoking static main method in <classname>
    java <classname>
    

Comments

Same in both languages (// and /* */ both work)

Class declarations

Almost the same, but Java does not require a semicolon

C++

    class Bar {};
    

Java

    class Bar {}
    

Method declarations

Same, except that in Java, must always be part of a class, and may prefix with public/private/protected

Constructors and destructors

Constructor has same syntax in both (name of the class), Java has no exact equivalent of the destructor

Static member functions and variables

Same as method declarations, but Java provides static initialization blocks to initialize static variables (instead of putting a definition in a source code file):
class Foo 
{
    static private int x;
    // static initialization block
    { x = 5; }
}

Scoping static methods and namespaces

C++

If you have a class and wish to refer to a static method, you use the form Class::method.
class MyClass
{
    public:
    static doStuff();
};

// now it's used like this
MyClass::doStuff();

Java

All scoping in Java uses the . again, just like accessing fields of a class, so it's a bit more regular:
class MyClass
{
    public static doStuff()
    {
        // do stuff
    }
}

// now it's used like this
MyClass.doStuff();

Object declarations

C++

    // on the stack
    myClass x;

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

Java

    // always allocated on the heap (also, always need parens for constructor)
    myClass x = new myClass();
    

Accessing fields of objects

C++

If you're using a stack-based object, you access its fields with a dot:
myClass x;
x.my_field; // ok
But you use the arrow operator (->) to access fields of a class when working with a pointer:
myClass x = new MyClass();
x->my_field; // ok

Java

You always work with references (which are similar to pointers--see the next section), so you always use a dot:
myClass x = new MyClass();
x.my_field; // ok

References vs. pointers

C++

    // references are immutable, use pointers for more flexibility
    int bar = 7, qux = 6;
    int& foo = bar;
    

Java

    // references are mutable and store addresses only to objects; there are
    // no raw pointers
    myClass x;
    x.foo(); // error, x is a null ``pointer''

    // note that you always use . to access a field
    

Inheritance

C++

    class Foo : public Bar
    { ... };
    

Java

    class Foo extends Bar
    { ... }
    

Protection levels (abstraction barriers)

C++

    public:
        void foo();
        void bar();
    

Java

    public void foo();
    public void bar();
    

Virtual functions

C++

    virtual int foo(); // or, non-virtually as simply int foo();
    

Java

    // functions are virtual by default; use final to prevent overriding
    int foo(); // or, final int foo();
    

Abstract classes

C++

    // just need to include a pure virtual function
    class Bar { public: virtual void foo() = 0; };
    

Java

    // syntax allows you to be explicit!
    abstract class Bar { public abstract void foo(); }

    // or you might even want to specify an interface
    interface Bar { public void foo(); }

    // and later, have a class implement the interface:
    class Chocolate implements Bar
    {
        public void foo() { /* do something */ }
    }
    

Memory management

Roughly the same--new allocates, but no delete in Java since it has garbage collection.

NULL vs. null

C++

    // initialize pointer to NULL
    int *x = NULL;
    

Java

    // the compiler will catch the use of uninitialized references, but if you
    // need to initialize a reference so it's known to be invalid, assign null
    myClass x = null;
    

Booleans

Java is a bit more verbose: you must write boolean instead of merely bool.

C++

bool foo;

Java

boolean foo;

Const-ness

C++

    const int x = 7;
    

Java

    final int x = 7;
    

Throw Spec

First, Java enforce throw specs at compile time--you must document if your method can throw an exception

C++

int foo() throw (IOException)

Java

int foo() throws IOException

Arrays

C++

    int x[10];
    // or 
    int *x = new x[10];
    // use x, then reclaim memory
    delete[] x;
    

Java

    int[] x = new int[10];
    // use x, memory reclaimed by the garbage collector or returned to the
    // system at the end of the program's lifetime
    

Collections and Iteration

C++

Iterators are members of classes. The start of a range is <container>.begin(), and the end is <container>.end(). Advance using ++ operator, and access using *.
    vector myVec;
    for ( vector<int>::iterator itr = myVec.begin();
          itr != myVec.end();
          ++itr )
    {
        cout << *itr;
    }
    

Java

Iterator is just an interface. The start of the range is <collection>.iterator, and you check to see if you're at the end with itr.hasNext(). You get the next element using itr.next() (a combination of using ++ and * in C++).
    ArrayList myArrayList = new ArrayList();
    Iterator itr = myArrayList.iterator();
    while ( itr.hasNext() )
    {
        System.out.println( itr.next() );
    }

    // or, in Java 5
    ArrayList myArrayList = new ArrayList();
    for( Object o : myArrayList ) {
        System.out.println( o );
    } 
 
 
 
Differences between Java and C++ are:
Java
C++
Java is a true and complete object oriented language.
C++ is an extension of C with object oriented behavior. C++ is not a complete object oriented language as that of Java.
Java does not provide template classes.
C++ offers Template classes.
Java supports multiple inheritance using interface.
C++ achieves multiple inheritance by permitting classes to inherit from multiple classes.
Java does not provide global variables.
Global variables can be declared in C++.
Java does not support pointers.
C++ supports pointers.
In Java, destruction of objects is performed in finalize method.
In C++, destruction of objects is performed by destructor function.
Java doesn’t provide header files.
C++ has header files.

 

Monday, October 21, 2013

some info about Java

Hello All ,
 This is my  post about Java. Everything has its own and very popular history. So when I think about java  the very first question arrived in my mind is how and why java ? Because we have c with us for the core programming and we have c++ for the entire object oriented concept. After going through a long discussion and from the internet I founded some facts which are sufficient for my queries.

So Let' start with my first question

How we got java?

  • Java was created by a team led by James Gosling for Sun Microsystems, James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time.

  • The language was initially called Oak after an oak tree that stood outside Gosling's office; it went by the name Green later, and was later renamed Java, from Java coffee, said to be consumed in large quantities by the language's creators. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet.java language derives much of its syntax from c and c++ but has a simpler object model and fewer low-level facilities.

  • Java is a fully functional, platform independent, programming language it has powerful set of machine independent libraries, including windowing (GUI) libraries.Java applications are typically compiled to byte code (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

  • The most interested thing about java is "write once, run anywhere" (WORA), meaning the code that runs on one platform does not need to be recompiled to run on another.

Why java ?


  • One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on any hardware/operating-system platform. This is achieved by compiling the Java language code to an intermediate representation called Java byte code, instead of directly to platform-specific machine code.

  • Java byte code instructions are analogous to machine code, but are intended to be interpreted by a virtual machine (VM) written specifically for the host hardware. End-users commonly use a Java Runtime Environment (JRE) installed on their own machine for standalone Java applications.

  • Java is very powerful language but it has its own pros and cons too. so lets discuss some strong and weak points of java.
Pros

  1.     Free.
  2.     The syntax is familiar to the programmers that know any other C based language.
  3.  Java (the platform) has a very large and standard class library, some parts of which are very well written.
  4. Java provides a platform for behavioral transfer from one address space to another. This is particularly evident in the dynamic class loading mechanisms of RMI (Remote Method Invocation).
  5.  Automatic Memory Management implemented by Garbage Collection
  6. Explicit Interfaces
  7. Improving performance
  8. Good portability (certainly better than that of nearly any compiled alternative)
  9. Simplified syntax (compared to C++
  10. Language design not committee driven
  11.  Lots of available code and third-party libraries
    If you love OOP, the only way to write functions is to make them class methods.
    Many standard interfaces defined in the standard library, which would have been vendor/OS specific otherwise, helps a lot in achieving portability and ease integration/selection of 3rd party libraries. E.g. JDBC, JMS, JCE, JAI, serial I/O, JAXP, JNDI, etc. Some have correspondence in other languages (e.g. ODBC) but not all.

Cons

    Performance: Java can be perceived as significantly slower and more memory-consuming than natively compiled languages such as C or C++.

    Look and feel: The default look and feel of GUI applications written in Java using the Swing toolkit is very different from native applications. It is possible to specify a different look and feel through the pluggable look and feel system of Swing.

    Single-paradigm language: Java is predominantly a single-paradigm language. However, with the addition of static imports in Java 5.0 the procedural paradigm is better accommodated than in earlier versions of Java.


So , this is all about java from my side. This are some very  basic things about java . I hope it will give all of u a good  start with java.

Source: wikipedia and my own work :)