• Post Reply Bookmark Topic Watch Topic
  • New Topic

Going from C++ to java  RSS feed

 
Carlos Hernandez
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have programmed in C++, JavaScript, and python. They have all been very easy to get into but I can't seem to wrap my head around java. Maybe I'm just ranting here on the subtle differences in libraries and how java is very strictly object oriented. I did use classes in c++ too but it was just different.

I can see that java and c++ is very similar but as for now I find it very hard to retain how to create methods and do simple things like creating objects which are now followed by an = and a new every time. What is the best way to transition from c++ to java?
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would recommend you to get "Java in a nutshell" book. Very good reference book.
Author spent quite a lot time for explaining similarities and differences with c and c++.

Next step - spend lots of time in JavaRanch, so you could see lots of code on daily basis.

Best luck
LV
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Maybe you're having difficulties because you are used to the way that C++ works, and in Java a lot of things are different - so you have to make an effort to unlearn the C++ way of thinking when you program in Java. When you're programming in Java, try to approach it with a fresh mind, and don't constantly compare it to how things work in C++.

One major difference to keep in mind is that in Java, variables of non-primitive types are references to objects, they are not objects themselves. A non-primitive type variable in Java is a little bit like a pointer in C++.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learned c++ after I learned Java, and I had the mirror image of the problem you're having. I could not understand why we did not have '= new ...' after every object declaration. I really didn't get how the constructor could be called without any brackets. It took me quite some time to stop trying to write java-like code in c++.

As mentioned, Java does not put objects on the stack and no field or variable in Java holds an object value. Java does not have copy-constructors (at least not inbuilt into the compiler and called on assignment) and it does not have anything like move-semantics or move constructors.

I think the Java syntax is a fair bit simpler than c++ syntax. I've looked in a couple of the boost headers and it can be very difficult to even understand how it is compilable, let alone what its doing. I think there is a lot less syntax to learn in Java, so if you can put C++ concepts aside when you're learning java it shouldn't take too long to get the basics.

Note there are big differences between java and python too. For example everything is an object in python, but that's not the case in Java.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!