How is Java more secure than other languages?
Java is pretty much "safe" unless a third party is trying to exploit the JVM. However, you can not consider it the safest.
Java is considered "safe" because:
- Java programs run inside a virtual machine (the JVM). Though the java program can even then have access to your files, it is pretty much safer. They have to belong to a trusted resource with a valid signature. Even if an untrusted application has gained this level of access, you can force quit the JVM, the application dies (can't access your info). It works pretty much like a sandbox, but it is exploitable to an extent. On the other hand, most C++ or C applications can easily continue running in the background, and even create new services to be safe from you. Obtaining the same results from a java application is tough.
- Everything the programmer writes in java is compiled to byte code. This byte code is not so easy to exploit/modify by third-parties. However, there are decompilers present which decompile the byte-code to the java code. However, this will be again time consuming for the third-party.
- Java code is verified before execution. This protects a java application from running a method which is flawed, hence usually saving the application from force exits. The variables are also null-checked to save errors in runtime.
- No use of pointers for memory management. Pointers can often cause data leaks to unauthorized applications. This is rare in the case of java (but yes, it is possible). Automatic garbage collection also plays a role here.
- Use of try-catch block to save a program from exiting due to exceptions. You can specify if a particular block of code generates an exception (error), and then tell the JVM what to do instead of closing the program in such a case.