Explain how to force the garbage collection in Java.- First of all Garbage collection is an automatic process and can't be forced. Although, you can request it by calling System.gc().
- JVM does not guarantee that GC will be started immediately.
- Every class inherits finalize() method from java.lang.Object.
- The finalize() method is called by garbage collector when it determines no more references to the object exists.
- You can sent request to recycle the unused objects by calling System.gc() and Runtime.gc() , but there is no guarantee when all the objects will garbage collected.
Advantages and disadvantages of Java Sockets.Advantages of Java Sockets :
- Sockets are flexible and easy to implemented for general communications.
- Sockets cause low network traffic unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request.
Disadvantages of Java Sockets :
- Socket based communications allows only to send packets of raw data between applications.
- Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
What do you understand by Synchronization? why is it important?
- Synchronization is a process of controlling the access of shared resources by the multiple threads.
- It allows only one thread can access one resource at a time.
- It ensures one thread not to modify a shared object while another thread is in the process of using or updating the object's value.
- This often leads to significant errors. Synchronization prevents such type of data corruption. Synchronizing a function :
public synchronized void SyncMet()
// Appropriate method-related code.
Constructors and normal methods.- Constructors must have the same name as the class.
- Constructors can not return a value.
- Normal methods are only called once while regular methods can be called many times
- Normal methods can return a value or can be void.
What is an immutable class? How to create an immutable class?- Immutable class is a class which once created, it’s contents can not be changed.
- Immutable objects are the objects whose state can not be changed once constructed.
- Since the state of the immutable objects can not be changed once they are created they are automatically synchronized/thread-safe.
- Immutable objects are automatically thread-safe since the state of the immutable objects can not be changed once they are created
- All wrapper classes in java.lang are immutable, i.e. String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger
Difference between ArrayList and vector.- ArrayList is not thread-safe whereas Vector is thread-safe.
- In Vector class each method is surrounded with a synchronized block and thus making Vector class thread-safe.
- Both the ArrayList and Vector hold onto their contents using an Array.
- When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room.
- A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
What is the difference between Java and C++?- Java is compatible with C++ but the backward compatibility is not being given in the case of C++.
- Java format is influenced by C++ only and it is strongly typed language but C++ is loosely typed language.
- C++ provides write once and compile anywhere concept, but Java provides write once run anywhere concept.
- C++ allows procedural, functional, object oriented and template programming, whereas java provides only object oriented paradigm.
- C++ calls directly to the system libraries for program, but java has to call through an interface to access the java libraries.
- C++ supports pointer, but there is no concept of pointers in Java language.
What is a java object and java application?- Java object is an object that is provided by the execution of an application. When an application is compiled an object of that application is being made. Java application on the other hand is a program that is being written in Java and being read by the Java virtual machine.
What is the difference between multitasking and multithreading?Multitasking includes two ways for representation :
1. Preemptive multitasking: where the system terminates the idle process without asking the user. For example: Unix/Linux, Windows NT
2. Non-preemptive multitasking: where the system ask the process to give the control to other process for execution. For example: Windows 3.1 and Mac OS 9.
1. Multithreaded programs are the program that extend the functionality of the multitasking by dividing the program in thread and then execute the task as individual threads.
2. Threads run in a different area and each thread utilizes some amount of CPU and memory for execution.
What is the difference between multiple processes and multiple threads?- Multiple processes are the way to provide multitasking environment to the user to allow him to switch over to different programs quickly. In these processes, it consists of complete set of its own variables and data. Multiple threads share the same variable and same data. Multiple processes are safe to use but multiple threads are riskier in the sense that they share the same data.
- Multiple processes have much more overhead but multiple threads have less overhead and individual threads can be stand alone if other threads are destroyed.
- In multiple processes inter communication is slower and more restrictive, whereas communication between threads are faster.