java vs. c++

What are the most surprising things of java for a C++ programmer?

If you are a good C programmer, you may not be a good C++ programmer or a java programmer.

If you are a good java programmer, you may not be a good C++ programmer or a C programmer.

If you are a good C++ programmer, then you are already a good C programmer and a good java programmer except that you may

need to spend some time, say half hour, reading below tips.

1. You don't use header files and there is no concept of prototype or forward declaration. In java, you use "import" key

word to "include" libraries or modules. Since the dependency relation becomes simpler than C++, you don't make "make file"

for compiler.

¡¡

2. All variables in java are like pointers in C++ except for those primitive type. For example, all class variables and

array variables are simply like pointers which need to be dynamically allocated with memory by "new".

¡¡

3. The compiler takes no responsibility to check the program entry function, namely "main", because it is possible that

you are simply declaring a class without "main". Therefore don't be puzzled with the strange error message from compiler

when you carelessly miss-declare your "main" with a wrong format other than "static void main(String argv[])".

¡¡

4. You don't have a chance to declare any global variables and if you do need to declare global constants you may have to

declare a class full of public constants. Similarly you don't have chances to declare macros.

¡¡

5. It is not amazing for you to declare local class within class as you can do it in C++ too. However, considering that you

can access the member data of enclosing class like accessing a "global variable" within that local class, you will be

stunned. If you are familiar with compiler design, you may notice that there are something impossible for compiler at

compile time. At least in VC6, the only things of enclosing class you can access are type, enum or struct.

    import java.io.*;

    class Father
    {
    	private int fatherID;
    	private Son son;
    	public Father(int id)
    	{
    		fatherID=id;
    		son=new Son();
    	}

    	public void display()
    	{
    		System.out.println("father has id of " +fatherID + " son has id of "+ son.sonID);
    	}
    	class Son
    	{
    		private	int sonID;
    		public Son()
    		{
    			sonID=fatherID;
    		}
    	}

    }

    public class Main
    {
    	public static void main(String argv[])
    	{
    		Father father=new Father(5);
    		father.display();
    	}
    }

¡¡

6. Don't be fooled by the "finally" in "try-catch-finally" because you might think the "finally" only works when exception

occurs. If you think so, you would be surprised to see the code within "finally" are always executed.

¡¡

7. You would be probably annoyed by java compiler because it always asks you to give all results on all possible if's. And

you may also feel impatient about the strict requirement of "try-catch" for all possible exceptions.

¡¡

8. You may find you cannot have "out" parameter for any function because even though in java we have references for complex

data like class yet they are all "const" references. Then the only choice for you to return more than one value is to

return a class object.

¡¡

9. Don't be fooled by the so-called garbage-collection because it only deals with memory allocation. As for other resources

like files, sockets etc, still it is programmer's responsibility to de-allocate them.

¡¡

10. The synonym for "package" in java is directory. So, when you see "import package MyFolder" then search directory

"MyFolder" for source code. And class name is also the file name plus ".java". As for "classpath", there is potential

risk for compiler to compile accidentally different version of your code. For example, if you include too many path inside

classpath and you keep multiple version of same class, just pray that the first one "javac" encounters is the current one.

¡¡

11. The error messages of java compiler are sometimes really silly because you can imagine that compared with that of C++

the java compiler is like a toy.

¡¡

12. When you use "new" to create an object of a class, you have to include the parentheses of constructor of class even

there is no parameter in constructor. i.e. "Dummy* dummy=new Dummy" is ok in C++ but in java you need add parentheses after

constructor "Dummy". "Dummy dummy=new Dummy()".

¡¡

13. Don't believe those articles which exaggerates the advantage of java. For example, they may criticise C++ for allowing

multiple inheritance and java fixes this by disallows multiple inheritance. However, java interface actually allows

multiple inheritance. Then what would you a programmer to implement multiple interface? Of course the easiest way is by

multiple inheritance from all classes which already implement those interfaces. Does this make things better? I would say

it only makes compiler design easier which eliminates ambiguity of inheritance.

¡¡

¡¡

¡¡

¡¡