1. Why is Java a platform independent language?
In Java ,compiler (javac) compiles the source code and generates the byte code .It is stored in .class file. .class file can be run on any operating system like macOS, Linux, Windows. This is the reason why Java is platform independent.
But the JVM (Java Virtual Machine) is platform dependent.
2. Why is Java not a pure object oriented language?
It is not wrong if we claim that java is the complete object-oriented programming language. Because Everything in Java is under the classes. And we can access that by creating the objects.
But Java supports primitive data types — byte, boolean, char, short, int, float, long, and double and hence it is not a pure object oriented language.
3. Pointers are used in C/ C++. Why does Java not make use of pointers?
Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.
Pointers are quite complicated and unsafe to use by beginner programmers. Moreover, the usage of pointers can make the procedure of garbage collection quite slow . Java makes use of references as these cannot be manipulated, unlike pointers.
4. How is Java different from C++?
C++ is only a compiled language, whereas Java is compiled as well as an interpreted language.
Java programs are platform independent whereas a c++ program can run only in the machine in which it is compiled. i.e C++ is platform independent.
C++ allows users to use pointers in the program. Whereas java doesn’t allow it. Java internally uses pointers.
C++ supports the concept of Multiple inheritances whereas Java doesn’t support this. And it is due to avoiding the complexity of name ambiguity that causes the major problem.
5.Difference between Heap and Stack Memory in java. And how java utilizes this.
When you create an object in Java using the new
keyword, memory for that object is allocated on the heap. The reference to the object can be stored in stack memory.
Primitive data types (e.g., int, char) and references to objects are stored on the stack when they are local variables within a method.
Java manages the allocation and deallocation of heap memory using a garbage collector, which automatically frees memory that is no longer reachable, preventing memory leaks.
Stack memory is managed by the program’s call stack. Memory is automatically allocated and released as method calls are made and returned.