Why is minecraft so badly optimized?

I can run it on an Intel hd laptop about the same as an alienware 13 and my gaming desktop. And they all have random lag spikes. Is it because it's made in Java?

Yes, it's because the game is written in Java. You get the same random lag spikes on Android as well for the same reason.

Java is known for poor performance. C++ is about 50 times faster than Java, which is why most top games are written in c++. If they had written Minecraft in c++, it would run very well on older hardware and would perform very very well and take a lot less resources.

Java is known as the lazy programmers language. You don't have to worry about garbage collection, and in theory it will run everywhere that there's a JVM. In the real world, JVM version hell has caused a lot of problems because Java apps built using one version of the JVM often don't work with older versions. And if your system doesn't come with Java, good luck figuring out how to install the right version.