Saturday 12 July 2014

Direct Memory Access Java

Does Java Allow Direct Memory Access

Well, there is a sun.misc.Unsafe class. It allows direct memory access, so you can implement some magic like reinterpret casts and so on. The thing is you need to use hacky reflection approach to get the instance and this class is not realy well documented. In general you need a very good reason to use this kind of tool in production code.
Here's an example how to get it:

Field f = Unsafe.class.getDeclaredField("theUnsafe");
f
.setAccessible(true);
Unsafe unsafe = (Unsafe) f.get(null);
 
 
There are 105 methods, allowing different low-level stuff. These methods are devoted to direct memory access:
  • allocateMemory
  • copyMemory
  • freeMemory
  • getAddress
  • getInt - Gets you a value from the memory whose

 

Wednesday 9 July 2014

Method Overidding vs Method Overloading

Difference between overloaded methods and overidden methods in Java



Overloaded Method Overridden Method
Arguments Must change Must not change
Return type Can change Can’t change except for co-variant returns
Exceptions Can change Can reduce or eliminate. Must not throw new or broader checked exceptions
Access Can change Must not make more restrictive (can be less restrictive)
Invocation Reference type determines which overloaded version is selected. Happens at compile time. Object type determines which method is selected. Happens at run-time.

Abstraction Vs Encapsulation

 Difference  between Abstraction and encapsulation 

  • Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it’s inside view, where the behavior of the abstraction is implemented.
  • Abstraction solves the problem in the design side while Encapsulation is the Implementation.
  • Encapsulation is the deliverable of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs. 
eg . Interface and the class implementing the interface 

eg . public interface ABC {
             void display();
}

Here abstraction is provided by the interface meaning it will be the face of the class implementing the interface ABC

Encapsulation can be done as 

public class DEF implements ABC{
             void display(){
                    System.out.println("DEF");
             }
}

In the above example 

Inteface ABC provides abstraction , while class DEF provides encapsulation i.e. binding the data and the code together.