Programs written in programming languages like C and C++ are compiled to native machine code for the computer that you are compiling them on. This makes them run fast, but also makes them specific for that type of computer.
One of the strengths of Java is that it is "write once, run anywhere". You can compile a Java program on a Windows machine and the resulting class file runs on any computer, no matter the operating system, for which there is a Java runtime environment available. The Java compiler compiles your Java source code to
bytecode, which consists of machine code instructions for a
virtual machine. The JVM (Java virtual machine) interprets and executes the bytecode when you run your Java program.
In principle, this is slower than how it works with C or C++, because extra work needs to be done when you run your program - the bytecode has to be interpreted while your program runs. In the early days of Java, Java programs indeed ran much slower than native code.
But over the years, Sun's implementation of Java has become very sophisticated. The current JVM contains a
just-in-time compiler which translates the bytecode to native machine code on the fly, so that your program runs almost as fast as a native C or C++ program.
In theory, a JIT-compiled program could run even faster than an ahead-of-time compiled program, because the JIT-compiler can employ optimizations based on information of the runtime behaviour of your program. A traditional ahead-of-time compiler can't take advantage of such information. (Although some sophisticated compilers, such as Intel's C / C++ compiler, contains a feature where you can provide the compiler with runtime profiling information about your program, which the compiler uses to optimize the code when you compile it the second time.)