   [java] C++ to Java
  Hi,I am a proficient, self-taught c++ programmer and I would like to port my code to Java as painlessly as possible.To give me a head start may I ask some questions.1. I have found there are no Global Functions. Can I just creat some class, eg UTIL and put these global functions in there? Then call UTIL.MyOldGlobalFunction(). Bit of a cheat but I think it sounds legit.2. In C++ I have two classes that have refences to each other in their functions. eg---MyClass1.hclass MyClass2; // A Declaration to declare EXISTENCEMyClass1{void func1(MyClass2 ∈);};---MyClass2.hclass MyClass1; // A Declaration to declare EXISTENCEMyClass2{void func2(MyClass1 ∈);};---I have these in separate .cpp and .h files. To overcome compiler problems I pre-declare the other classes EXISTENCE early in the .h file.Everthing is ok because I do not USE the pre-declared classes until the function is written in the .cpp file (which has the required #include "myclass1.h" and #include "myclass2.h" at the start).But the question is. How is this overcome in Java where your .h and .cpp files are all in one?3. There are also some C++ Template issues I would like to ask, but I'll do those when I understand this.Yours with confusion,Jon   
  1. Yes, just use a static function in a (possibly empty) class. Math calls (Math.cos(), Math.sqrt() etc.) would be the obvious examples. But please stick to standard naming conventions - class names should be camel case with a leading capital (ie. Util rather than UTIL). ALL_UPPER_CASE is usually reserved for constants.2. This is a non-issue in Java. Just have each class 'import' the other and remove your forward declarations and it'll work just fine.   ;Thanks for the reply to question 1.As far as question 2 goes, I was asking how to do it in Java. In Java you have header and source code in one. In C++ if you use a predefined class in an inline function before the #include in the source file you get a compiler error - something like not allowed to use a class before it is defined. So the question I am asking is how do you do this in **Java** when source and header are in one.I'd also like to add another question...3. In c++ you have functions like operator< (and the other ordering operators) and operator= (which allows you to set up a new class with the same values as an old class). Are there any equivalents in Java?Thanks Jon;Quote:Original post by JonAkaJonAs far as question 2 goes, I was asking how to do it in Java. In Java you have header and source code in one. In C++ if you use a predefined class in an inline function before the #include in the source file you get a compiler error - something like not allowed to use a class before it is defined. So the question I am asking is how do you do this in **Java** when source and header are in one.You do it as I already said - just 'import' the other classes from each other and everything Just Works. The only reason this is a problem in C++ is because of it's dated compilation method.// In src/package1/Foo.javaimport package2.Bar;class Foo{  public Foo(Bar bar)  {    bar.frobnicate(this);  }  public void grobbit()  {  }}// in src/package2/Bar.javaimport package1.Fooclass Bar{  public void frobnicate(Foo foo)  {    foo.grobbit();  }}Quote:3. In c++ you have functions like operator< (and the other ordering operators) and operator= (which allows you to set up a new class with the same values as an old class). Are there any equivalents in Java?To create a new object from an existing one you can use Object.clone. By default this is a shallow copy, but you can override it to do something more complicated (like a deep copy) if you want to.For comparisons you make your class implement Comparable and write a suitable compareTo method (and make sure you follow the rules about also maintaining correct Object.hashCode and Object.equals behaviour).Should you want to sort an object which doesn't (or won't) implement Comparable, you can create a Comparator instead.   ;Quote:I am a proficient, self-taught c++ programmer and I would like to port my code to Java as painlessly as possible.I would suggest you forget everything you know, and start here.There is very little that C++ knowledge can bring to Java, unless you run across some rare need for obscure optimization.From practical perspective however, most of idiomatic approaches are big no-nos in C++.Quote:1. I have found there are no Global Functions. Can I just creat some class, eg UTIL and put these global functions in there? Then call UTIL.MyOldGlobalFunction(). Bit of a cheat but I think it sounds legit.Java has well defined naming convention. Make sure to use that.As for global functions, put it into a class, then use static public modifiers. This is the way its done, due to lack of namespaces or free functions.Quote:2. In C++ I have two classes that have refences to each other in their functions. egImport what you need, this problem doesn't exist in Java.Quote:3. In c++ you have functions like operator< (and the other ordering operators) and operator= (which allows you to set up a new class with the same values as an old class). Are there any equivalents in Java?Yes, Comparable and Comparator interfaces. Before going any further, go through Sun's tutorials, which cover standard library and its concepts. If you are familiar with Qt, you will find it's very similar.As a general rule, you should avoid cloning objects. It's not always possible, but there are very few actual scenarios where cloning is needed. Most of Java classes don't implement the mechanism at all.As it turns out, it's not rarely needed.Quote:3. There are also some C++ Template issues I would like to ask, but I'll do those when I understand this.If you are familiar with C++ templates, you will first need to forget everything you know.Java generics are not only broken by design due to excessive emphasis on backwards compatibility, but also operate on completely different concept of type erasure. They are just automated type casting mechanism, not meta-programming tool.   ;Thanks for the help guys.The answer to 2 is a nice feature of Java compared to c++.I have asked some more questions in a new thread.