C++ vs Java

learncpp.com is a very in-depth tutorial for beginners. It also teaches you how to use the compiler and what happens when your source code becomes a binary.

There is a stereotype in c++ that you need to know everything to use it. That’s fake news. C++ allows to use only what you need to use.

C++
int main()
{
}
vs.
class Main
{
public static void main(String args[])
{
}
}

Some will begin to say that if one doesn’t understand all the lvalue refernces and whatnot in C++, then the code will be less than optimal.

Fear not, C++ is massively faster the Java. Code with some sub-optimal stuff in C++ will still run faster than “perfect” code in Java which is sub-optimal as a whole(compared to C++ performance).

Basically, when you write Java code, it is basically C++ code that has been written using sub-optimal ways.

Plus, in C++, == does what == is supposed to do :wink:

2 Likes

Please do not advise teams with limited access to programming resources to use C++. This is bad advice.

16 Likes

You can actually get away with using less concepts in C++ and treat it as a simpler language, I’ve seen it before with great success in teams who had little experience in programming and was kinda hacking thingss together. I did get really offended by the poor coding structure and lack of use of the advanced features, but nevertheless, it did get them to State. Their code worked. And they knew little about C++.

Java requires everything to use OOP. And the == operator does not do what the == operator does because object variablels are actually references cough pointers in general with easier syntax cough So when you do something to them in a function, that could get really confusing to a start when the functions have side effects. Yes, the tutorial does warn of such, but a beginner will definitely forget and have to deal with the side effects.

C++ allows you to only use the functional programming aspect.
Less things to worry about.

The standard library of C++ allows one to not have to worry about memory management at all for a vast majority of cases.

Use java if you want to, but C++ is actually capable of being a simpler language than Java.

Java mandates that you bring the whole Jungle for the ride.

C++ allows you to just take the fruits you like.

Before you complain about how not using such advanced features in C++ makes the code slow, the code will still run decently and in most cases faster than Java.

At the risk of ending a language war, I would suggest using the language the most students and mentors are comfortable with.
My team’s high school does not have a C class, but does have a Java class. So for us, it’s Java.

5 Likes

Amen. I have nothing against Java(even if it isn’t too clear). Use Python or Batch scripts if you want to even(would heavy recommend Java or C++ since FRC has native support for such. But again, your choice. But I would be really really impressed XD)

A couple comments based on your statements:

For 99.8% of FRC teams (I did the math), the concern of speed should not be an issue. You bring it up multiple times as if it’s this holy grail benefit of C++. It’s not. There are way too many other optimization that should be done first. Many teams have brilliant programmers that have become so bogged down in optimizing the tiniest part of the robot that they forget the gold key to success in FRC, find the most reward for the least work and do that first.

Both languages in FRC can be heavily abstracted such that little to no coding experience is required. If you keep it simple, you don’t need hardly any expertise.
Also, you seem to imply that C++, even in the worse cases, still gets teams to state. Yet this is also true in java. Purely getting to state, in any district, does not require good code at all. Working code, yes, good code, no.

I mean, yeah, but the last time I had to compare two objects when writing FRC code was, well… never. Possible once in 2018 when comparing Strings?


My concern with your arguments are not the premise that C++ is a perfectly valid programming language, but rather that your argument has a very “Used Car Salesperson” feel to it. The cases you point out are important, but very minor. Yet in reading your statement, the ignorant to programming could very easily be persuaded to take on C++ for these supposed “massive benefits”. That’s concerning to me.


@Oblarg I would have loved to hear more follow up to your statement. I don’t feel it does the community much good without reasons backing the claim.


My suggestion to all those out there. @RobBlair makes a very good point, use what you have resources for. If you don’t have any preference, use Java. Much more of the community knows Java, so they will be able to help you more.

7 Likes

From my experience there’s two languages that i recommend for this scenario: Labview or Java.

If the concepts of OOP are foreign to you, I’d advise using Labview though it’s not currently the most supported language as of 2020. I mainly say this because it has a relatively low entry barrier when it comes to programming.

If you have a mild grip on OOP concepts, id say go for Java. It’s heavily supported by FIRST, is fairly easy to comprehend, and you can find step-by-step documentation for programming a robot on the https://docs.wpilib.org

C++ isn’t right for this application, mainly because it was built to be fast, not easy to comprehend, as well as it implements OO concepts in a way that’s difficult to understand as a beginner. So unless your main concern right now is making your programs faster (which for most teams it isn’t a concern) id advise one of the above.

As @RobBlair says in their post: use something you guys are comfortable with, and always remember to use your resources, if your school has an AP Computer Science class, it most likely teaches Java which means that teacher could be a tremendous help with learning concepts!

Best of luck with your programming!

1 Like

It is well-known that C++ is a more-difficult language to write - and, more importantly, to learn - than Java. I’m not going to write at length about why this is; it is beyond the scope of this thread.

Teams with limited programming resources should, for obvious reasons, use what is easiest.

If your team already has experienced C++ programmers available, then I do not think that qualifies as having “limited programming resources.”

9 Likes

Every programming language allows you to use only what you need to use. This statement means literally nothing, as no application (even FRC) requires you use a totality of the language to get the job done.

Especially in FRC

Any argument that starts with “some will say” is obviously setting up something not based on real experience. As an aside, C++ is sometimes massively faster than Java. There are other times when Java out performs C++. A speed comparison is only as qualified as the benchmark being used to measure.

In the professional world, there’s a reason why a gigantic percentage of backend systems powering this great Internet of ours are written in Java and not C++…and it’s not because Java is slower.

Sub-optimal as measured how?

If you measure optimal code by type safety, then I think Java wins that race for example.

What is it supposed to do?

Should the == operator return equivalence, or identity of a given object? Two instances of a class are really, truly equal to each other only when they are the exact representation of itself, which means they are only true if their memory is equal bit for bit (which includes the pointer).

That isn’t that intuitive though, so some classes override the equality operator to be more human friendly like comparing strings, or numbers, etc.

"test" == "test1"

Should return false for both identity, and equivalence.

"test" == "test"

Probably should return true for equivalence, but false for identity…or something like that.

All of the arguments in favor of C++ don’t have any real reasoning behind them, they are tools that are both successful in their application here in FRC. For beginners, Python is probably the easiest syntax to pickup, but that doesn’t mean it’s the best language for beginner FRC teams to use because you have to add in all the various things that may help such as community support, quality of documentation, etc.

Java is the easiest barrier of entry for FRC teams in my opinion because of the massive availability of help they can receive.

3 Likes

I have no large hate against java. But any problems that can be solved without static objects, still need static objects because “Everything is an object in Java.”

First thigns first, one of the main reasons I wrote advocacies for C++ is to bring the C++ side. If you wrote a C++ advocacy, I would have written a Java advocacy

C++ is not sometimes faster than Java. It is very much faster than Java all the time. Java actually relies on C and C++ in certain areas to maintain speed just as C and C++ both rely on assembly at times.

So you are saying that comparing two primitive integers where as
bool fiveEqualsFive()
{
int x=5;
int y=5;
if(x==y)
{
return true;
}
else
{
return false;
}
}
Should actually yield false? There is no way talking out of that If I wanted to compare the addresses, i would use pointers. Most of the time, you want to see if those two objects are actually equal to each other. .equals() is messy syntax.

They do have reason behind them. I have provided evidence for the arguments in a logical manner. You however, have made mistakes in your argument.

If you are referring to the anecdote I have, no, because I do know that they were inexperienced because they thought they were programming in regular C, even though the syntax was obviously C++. Their code was in a single text file spanning about 700 lines containing all the teleop code along with the hardcoded autonomous. They even told me themselves. You do not need to be experienced in C++ to start writing code. That is a stereotype.

Yes, but that reason I mentioned such is because most people think that C++ has a high skill requirement and must be perfect or else it will shout louder than python(which is very strict on how to format stuff). It doesn’t. It can be very easy to program in if you drop the steroetypes.

You can do the same on Java, I didn’t mention such because most people already believe that you don’t need to write beautiful code to get things running in Java.

I use java myself actually along with python and gdscript and the glorious bash.

So, I’m guessing you’ve never written a compiler before…

This is an inherent misunderstanding of both how Java is written, compiled, and ultimately optimized at runtime. Actually, it’s also a lack of understanding of how C++ is compiled and linked as well.

The very nature of Just-in-Time compilation implementations is that they can do runtime optimizations, which given an application running on a wide array of architectures, with a variance in processor support, etc, will not be optimal in all circumstances.

It’s impossible to optimize a pre-compiled, pre-linked application to be optimal across all possible architectures. It’s not impossible for that to happen with a JIT implementation.

Other runtime things you need to care about are memory allocators, the efficiency of garbage collection, and how those are implemented for a given program.

So, what I said remains absolutely true. There are circumstances where Java does indeed outperform C++. With A LOT of effort, you can make C++ program improvements that will make it faster and more efficient than java in all possible cases if you don’t rely only the default allocators for memory management, and if you compile a new version of the program for EVERY possible architecture.

That being said, even if you did all those things above, C++ programs will never be able to perform runtime optimizations, as that’s the very nature of pre-compiled/linked binaries. Now, if you remove the ability for the JVM to do runtime optimization, then what you are saying could be true as long as you go through all of the effort to replace the defaults of the program.

All this talk of optimization is indeed dependent on what the benchmark program actually is, because for example, many years ago when I was in college I wrote a sample application in Java that crushed the C++ implementation of the same program in efficiency. All the program did was spawn and run thousands of basic threads that interacted with read/write locking data structures. Turns out that the Java threads and concurrency locks were typically faster than those of Posix threads and standard library concurrency pieces.

Again, many years ago, posix stuff and fresher versions of C++ may have caught up. However, back then, in that instance Java was significantly faster than C++. Not a very useful benchmark because the program actually didn’t do anything useful in the real world, but it illustrated a point for sure.

No, maybe you’re still learning the nuances here rather than just misrepresenting what I said to form your argument… so I’ll explain.

What I said was “Two instances of a class”. Just in case you aren’t clear on it, primitives in a programming language are different than instances of a class. Instances of a class are the objects that are created by calling a given class definition’s constructor. Primitives are things like integers, doubles, floats, and in some languages (like C++) string literals.

So, nothing I said is related to comparing the equivalence of primitives, what I was saying is that two instances of a class (objects) being compared should thought of as a different case perhaps in the case of equivalence and identity.

Given the Java code:

Integer one_a = new Integer(5);
Integer one_b = new Integer(5);

// What should this return?
(one_a == one-b)

So, what should that return? They are not the same object, so they are not bit for bit identical. So, from the identity perspective, they shouldn’t be considered equal. They are different objects, with different memory layouts, etc.

From the equivalence perspective, they are indeed objects that are trying to represent the integer 5. So they are “equal”, but only from that perspective.

So, the essence of my point is that the equivalence operator doing what it should or should not do is relative to the implementation and design of the class. There’s a reason why you can override the equivalence operator for a class right? Because, the answer to the question as to what should this do is “It depends”.

I believe wholeheartedly you believe that your arguments are logical, and in simplistic terms some of them actually could be. However, the mistakes you think I’ve made in my arguments are really just a lack of understanding of how these things are actually done at the low levels of the architecture.

Hopefully I’ve given you enough keywords that you can Google and find out more for yourself. I wouldn’t expect you to just take my word for it.

6 Likes

You compile on that system. That’s why you compile. Even if the Java bytecode is not written for that architecture, the JVM written for that architecture or JVM in general must have certain components to deal with specific architectures.

This is why various programs for various linux distros have different programs(as each distro has different releases for different architectures). And specialized programs use assembly that is architecture specific.

A lot of projects list any platform and give the source code to download as the compiler for that architecture will refine for that architecture.

But the architectures had to be programmed in in the first place.

At some point, the JVM has to be made for each architecture as well. But the dominant ones being i368 and x86_64. You also have ARM and whatnot. Either way, you still have to make a version or add a vast array of instructions to deal with each architecture at some point.

They don’t have to because they have already be compiled for that architecture like how the JVM has had to be made for each architecture or given the instructions to handle a specific architecture.

Yes, C++ compilers sucked heavily back then, but recently, they have gotten really close or caught up to C. And multiple benchmarks have been done and has proven than C++ is faster(I did not say superior. I said faster. LIke how assembly is mega-fast) than Java.

This debate should not references C++ in the 90s, that’s 30 years ago. I do feel Linius Torvalds when he had a curse-filled rant against C++.

There is no difference. A primitive int x is still an instance of primitive int x. You haven’t put into consideration how structs work and why oop was made in the first place.

What

But most cases call for their values being compared. And pointers are different from the actual instance themselves in reality.

If I wanted to know that they were stored at two different memory locations, I would have used the address of operator… But most of the time one wants to comapre the values. It’s the intiuitive way. The more user friendly way. Java has taken steps to be simpler(at first glance), but the == shenanigans are not.

My bet is that JVM implementations will be more optimally written than high school robotics code

2 Likes

Well, I’m pretty certain at this point you’re just trying to argue that you’re not actually having a discussion so I’ll bow out.

If you are truly interested in understanding what I’m saying I’d say spend a little time Googling what I’ve posted. You have a very shallow understanding of what you are talking about. For example, the idea that you think that object instances are the same thing as primitives disqualifies you from having any sort of strong opinion about the relative merits of any object oriented programming approach. The idea that you somehow think that C++ programs are strictly optimal because of the information known at compile time, and that they somehow couldn’t benefit from runtime optimizations leads me to believe that you either don’t understand compile-time or run-time optimizations. Again, I stand by what I said earlier in that I don’t think you’ve ever written a compiler…so I would assume that out of the two of us, I’m the only one that has. You truly don’t have the basis of understanding that you think you do, I just don’t want anyone reading this thread to make bad decisions based on misinformation.

3 Likes

The Compiler, which has been designed to make optimizations, has been written more optimally than high school robotics code as well…

image

5 Likes

Again, I have no agenda against java, I’m just using my knowlege to argue for C++ just to provide a different side. It’s like how you have a lawyer for the prosecution and the defense. It’s their job to take one side. I’m more often on the fence however.

I do not have a very shallow understanding. But are you also aware of JIT spraying? JIT code is less secure.

Continuing,
JIT means that the code is being processed during run-time which means processing power is being used for optimizing. And that could have been done during compile-time logically.

I am now wondering, have you? But that’s irrelevant, we both know that compilers are designed to make optimizations.

Why do the optimizations later when you can do them now?

Because if the runtime updates, you can get optimizations without recompiling your entire program :smiley: