<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="en-ca">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>java vs. c++</title>
</head>

<body>

<p><font color="#FF0000" size="4"><b>What are the most surprising things of java 
for a C++ programmer?</b></font></p>
<p><font color="#0000FF">If you are a good C programmer, you may not be a good 
C++ programmer or a java programmer.</font></p>
<p><font color="#0000FF">If you are a good java programmer, you may not be a 
good C++ programmer or a C programmer.</font></p>
<p><font color="#0000FF">If you are a good C++ programmer, then you are already 
a good C programmer and a good java programmer except that you may</font></p>
<p><font color="#0000FF">need to spend some time, say half hour, reading below 
tips.</font></p>
<p>1. You don't use header files and there is no concept of prototype or forward 
declaration. In java, you use &quot;import&quot; key </p>
<p>word to &quot;include&quot; libraries or modules. Since the dependency relation becomes 
simpler than C++, you don't make &quot;make file&quot;</p>
<p>for compiler.</p>
<p>　</p>
<p>2. All variables in java are like pointers in C++ except for those primitive 
type. For example, all class variables and </p>
<p>array variables are simply like pointers which need to be dynamically 
allocated with memory by &quot;new&quot;. </p>
<p>　</p>
<p>3. The compiler takes no responsibility to check the program entry function, 
namely &quot;main&quot;, because it is possible that </p>
<p>you are simply declaring a class without &quot;main&quot;. Therefore don't be puzzled 
with the strange error message from compiler </p>
<p>when you carelessly miss-declare your &quot;main&quot; with a wrong format other than 
&quot;static void main(String argv[])&quot;. </p>
<p>　</p>
<p>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</p>
<p>declare a class full of public constants. Similarly you don't have chances to 
declare macros. </p>
<p>　</p>
<p>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</p>
<p>can access the member data of enclosing class like accessing a &quot;global 
variable&quot; within that local class, you will be </p>
<p>stunned. If you are familiar with compiler design, you may notice that there 
are something impossible for compiler at </p>
<p>compile time. At least in VC6, the only things of enclosing class you can 
access are type, enum or struct. </p>
<pre>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(&quot;father has id of &quot; +fatherID + &quot; son has id of &quot;+ 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();
	}
}</pre>
<p>　</p>
<p>6. Don't be fooled by the &quot;finally&quot; in &quot;try-catch-finally&quot; because you might 
think the &quot;finally&quot; only works when exception</p>
<p>occurs. If you think so, you would be surprised to see the code within &quot;finally&quot; 
are always executed.</p>
<p>　</p>
<p>7. You would be probably annoyed by java compiler because it always asks you 
to give all results on all possible if's. And</p>
<p>you may also feel impatient about the strict requirement of &quot;try-catch&quot; for 
all possible exceptions. </p>
<p>　</p>
<p>8. You may find you cannot have &quot;out&quot; parameter for any function because even 
though in java we have references for complex</p>
<p>data like class yet they are all &quot;const&quot; references. Then the only choice for 
you to return more than one value is to </p>
<p>return a class object. </p>
<p>　</p>
<p>9. Don't be fooled by the so-called garbage-collection because it only deals 
with memory allocation. As for other resources </p>
<p>like files, sockets etc, still it is programmer's responsibility to de-allocate 
them.</p>
<p>　</p>
<p>10. The synonym for &quot;package&quot; in java is directory. So, when you see &quot;import 
package MyFolder&quot; then search directory </p>
<p>&quot;MyFolder&quot; for source code. And class name is also the file name plus 
&quot;.java&quot;. As for &quot;classpath&quot;, there is potential </p>
<p>risk for compiler to compile accidentally different version of your code. For 
example, if you include too many path inside</p>
<p>classpath and you keep multiple version of same class, just pray that the 
first one &quot;javac&quot; encounters is the current one.</p>
<p>　</p>
<p>11. The error messages of java compiler are sometimes really silly because you 
can imagine that compared with that of C++ </p>
<p>the java compiler is like a toy. </p>
<p>　</p>
<p>12. When you use &quot;new&quot; to create an object of a class, you have to include 
the parentheses of constructor of class even </p>
<p>there is no parameter in constructor. i.e. &quot;Dummy* dummy=new Dummy&quot; is ok in 
C++ but in java you need add parentheses after</p>
<p>constructor &quot;Dummy&quot;. &quot;Dummy dummy=new Dummy()&quot;.</p>
<p>　</p>
<p>13. Don't believe those articles which exaggerates the advantage of java. For 
example, they may criticise C++ for allowing</p>
<p>multiple inheritance and java fixes this by disallows multiple inheritance. 
However, java interface actually allows </p>
<p>multiple inheritance. Then what would you a programmer to implement multiple 
interface? Of course the easiest way is by </p>
<p>multiple inheritance from all classes which already implement those 
interfaces. Does this make things better? I would say</p>
<p>it only makes compiler design easier which eliminates ambiguity of 
inheritance. </p>
<p>　</p>
<p>14. In java, there is no &quot;destructor&quot;. And don't be fooled by the so called 
&quot;finalize()&quot; method which will not be called </p>
<p>automatically when your object dies. Whoever uses your object maybe 
responsible to call your class's &quot;finalize()&quot; method, </p>
<p>but it is not mandatory which is very bad. At least this is true for all 
user-defined class. (Maybe some java built-in class can be automatically called 
with &quot;finalize&quot; if you inherit your class from them, but I haven't got any clue 
yet.)</p>
<p>15. At beginning you may think compilation in java is easier than c++ because 
you don't have to write a &quot;makefile&quot; for compiler. So, at </p>
<p>command line, you may simply type &quot;javac mypackage/*.java&quot; to compile 
everything in your project.&nbsp; As long as all your modified code is there</p>
<p>you don't have to worry about dependency. However, for large project this may 
take time and finally you still need &quot;incremental compilation&quot; by writing</p>
<p>some &quot;makefile&quot; so as not to ask compiler to compile again and again.</p>
<p>　</p>
<p>　</p>

</body>

</html>
