C++ or Java

I have this humongous doubt of using which programming language for solving the questions !

I’ve been using java for solving all the questions so far !
But i’ve noticed that majority of the coders here use c++ , and prefer the same over java !

Does java have any kind of dis-advantage except the execution time for programs ?? and why does everyone prefer C++ over java ?

What should i be using to code ? which one is better ?

As I’ve seen it Java definitely has more inbuilt functionality than c++ , but still why do people prefer c++ ? or am i assuming things wrongly here ?
It’ll be of great help to me if people can elaborate about the fact !

5 Likes

I think that it’s only the way of seeing programs or thinking in problems that can influence you to choose one over the other, especially if the problem itself is OO (which is very rare imo).

But, I am now learning Java and C pointer manipulation to do my IT course assignments (never done anything alike before in my life) and I have been told that C++ and Java behave differently regarding the use of classes or structs concepts…

Apart from that, C++ can have a somehow nasty effect on the way you do Java programming, as programming in Java like you do in C++ can lead to serious errors…

A main difference that’s also worth mentioning is that Java is garbage collect whereas C++ is not…

Bruno

I think one reason is that most people know c++ better than java as they probably learned c++ first. Hence, they know of all the tricks/libraries. While others who know java better (you) will of course use java as use havent been that familiar with c++.

For me personally, sometimes i use freepascal instead of c++ because it offers ansistring (unlimited length string) which does make it really easier in some prob. While c++ has lots of libraries which i have to implement myself in pascal.

I completely agree with kuruma and tungnk1993, in what they have said. I will add following points-

  1. Main reason is definitely the speed of execution. C or C++ is far ahead of Java in this. I won’t elaborate as to why C++ is faster here.
  2. Also in C++ you can get away with procedural style of programming, but Java forces you to write OO program. Which is unnecessary burden for programming on codechef.
  3. No pointers? No pass by reference? Seriously this is a put off.
  4. Top programmers are wicked! And C++ allows you to do wicked things. For example macros. Can you do this in Java?
  5. C++ has STL, which is awesome. So your argument “Java definitely has more inbuilt functionality than c++” is invalid. Whenever I see C++ beating C, I immediately understand that it has got to do with STL.
  6. Taste. Yes a language is supposed to be tasty. Java syntax is rather mundane. C++ packs a lot of spicy operators.
  7. Generics are not as powerful as templates. I don’t know whether people use either of them on codechef, but it is a fact. With C++ templates, you can do a lot of work at compile time.
  8. Also it is possible that C++ is taught before Java (as in my case). Which I think is again for a good reason.

I have omitted many points of difference between C++ and Java, because they are not relevant in Codechef programs are quite small. But 80% of time, Java losses. Which is yet another reason for programming in C++ on Codechef.

Note 1 - I am not suggesting that Java programmers should become C++ programmer. Code in the language, you are comfortable in. And both languages are good enough for all sorts of programming. This answer is merely an answer to OP’s question.

Note 2 - Feel free to correct me, if I said anything wrong.

14 Likes

I THINK YOU SHOULD NOT BOTHERABOUT WHICH LANGUAGES YOU SHOULD CHOOSE OR NOT .BECAUSE THE LANGUAGES ARE ONY TOOLS TO REPRESENT ALGORITHM. FIRST TRY TO MAKE YOUR ALGORITHIMIC SKILL STRONG AND THEN CHOOSE ANY LANGUAGE WHICH IS BEST SUITED FOR YOU. DON’T BE BOTHER ABOUT WHAT OTHER ARE DOING.

1 Like

I personally think no language is better than other languages. The result vary depends on your proficiency in it. So i would say go with what u feel the best.

Really you should go for C++.This is like the A-B-C-D of programming.

I will not argue about the fact that c++ is in general definitely faster than java. And that of course is an advantage for coding contests. I will not lie, I like java more than I like c++(which in fact is my second favorite programming language) so I’ll try to show what I like in java better than c++ (even when it comes to contests).

Note: There is no “global better programming language”, there are only tastes and needs, you should choose your programming language considering this two points. And when you learn one programming language and become a reasonably good coder you should be able to adapt to other programming languages without much difficulty.

Ok, here goes (feel free to criticize and correct me as much as you need but if your argument is not strong enough and I’m not convinced I’ll probably try to show you why):

  1. Java is easier to learn than c++ and I would recommend any person who wants to learn programming to start with java:
  2. Java has THE VERY BEST documentation by far(and I honestly doubt anyone can argue with this).
  3. With Java you don’t have to worry about headers and I think this is a big plus, you have less files to worry about and it’s easier to focus on what’s important (this is not really important here but you never know).
  4. In java you don’t have to specify the size of an array on creation… there are ways to do this in c++ but I think java’s approach is way better.
  5. Java naming system is a big help, basically what you see is what you get:
    • I really don’t like how c++ shortens the names of methods and objects like str, strcmp, I mean you have to deduce or memorize what those words mean, I know a lot of people will most likely call me lazy but I think it’s really harder to learn when you’re not used to those things yet. However in java you can learn those things without much effort and with a well organized api it becomes a lot easier to learn and enjoy programming.
  6. Ok, c++ is definitely faster than java but in no way java is slow. If your algorithm is good it will run fast. If you are really needing some kind of speed optimization and you already pushed the most out of the java code just write the same code in c++(java’s syntax is based in c like c++'s).
  7. It’s easier for beginners to handle exceptions in java.

In general I would strongly recommend start programming in java, and when you’re good enough you can start learning c++ (which is what I’m doing) and really it’s not that harder to translate from one to the other… But like the others said , you have to choose the language that makes you more comfortable to be your main language… And with that I don’t mean you should code a little more on one than the other… I mean you should become the best you can in one of them… Know the important stuff, know which approach works best in which situation and what can the language provide that can help you and you’ll see that whenever you want to pass your code to any other programming language and you already know this stuff you won’t have a lot of problems… I hope that my answer can at least prevent you from choosing something just for one reason, whatever your choice may be.

2 Likes

Check that project GitHub - charb/jcpp: C++ development done differently, the Java way with introspection, RMI, GC (Garbage Collection) and much more
It brings Java world to C++

yeah! I had like to know as well!

Thanks for your response kuruma,
i can see you have elucidated the disparate qualities of java and c++, but what i wanted to know was why people prefer c++ over java ! majority of the people i’ve seen are doing so !mainly all the top programmers here in codechef have submitted almost all their solutions in c++ …only a handful have even attempted to code in java…
is it because that c++ is more famaous than java and that more people started off with learning c++ than java since its been around for a long time … or is there some other specific reason(s) to this ?

Thanks for your response Nguyen,your answer might be the case… but still top programmers have been using c++ more than java, i can’t shake off the feeling by just assuming that they are more familiar with c++ ! i still keep asking myself, if they thought java was more efficient for problem solving than c++ wouldn’t they have considered mastering java ? why stick with c++ till the end ! its really bugging me :expressionless: and seeing you, you yourself have submitted majority of your solutions in c++ !

Thanks vinayak,nicely said and that has made me clear of a lot of things !
and one more thing !! Would you suggest a java coder(like me) to shift to C++ ? and are you telling that it is easier to find a solution in c++ than in java ? (about all the wicked things you mentioned which wouldn’t have been possible in java)

@mecodesta: I will suggest C++ to anyone! People love it for some reason. I won’t say C++ makes finding solution easier. It’s the algorithm that solves the problem. C++ STL can provide you a Data Structure to support the algorithm however. By macro (a wicked thing) you can write code quickly. Like you can write a loop just by FOR(i). All those #define’s you see in C++ code are macros.

Thanks mate !! that really helped !

the main advantage of c++ in contest is the shorter syntax compared to other languages (java/pascal/…).

For example, in java, to declare an array or inputreader it is really long and time consuming whilst it is shorter and more efficient in c++. And for complicated problems, c++ becomes the better choice :slight_smile:

1 Like

Excellent post! Great information here. thank you!
Spring Framework Online Course

I use to code in JAVA earlier, I switched to c++ when I observed these things

  • SPOJ has strict time limit for java users.

  • The power of MACROS to fasten your coding time.

  • Most of the champion coders code using c++, following their codes becomes easy if you follow the same language else you need to map every data structure from one language to other.

  • c++ execution time is way faster than java.