Why would one need try-finally block combination

If we dont wanna handle the exception and instead propogate it up the hierarchy then we go for try-finally combination

how does polymorphism works within the constructors

First of all it is not good practice to call any instance methods inside the constructor, if we cannot avoid then do as little as possible.

What happens:

Assume we call a over-ridden method within the constructor and if that method manipulates data that has not been initialized (because constructor is not yet completed) yet then it will lead to disaster.

The only safe methods inside the constructor to call are final methods in base class.

How does DriverManager identify the type of driver loaded into JVM

Typical JDBC code to connect to the database is:

New Changes in JDK7

JDK7 is packaged with these new changes
– Small language changes
– NIO-2 Enhanced NIO
– invokedynamic
– other features

Small language Changes:

  • Now integer can accept binary format (along with octal and hexadecimal values)
<pre>int i = 0b10110
  • Switch statement can accept string variables. Since String object in Java is considered immutable, java community has come up with this concept

usage:

public int useSwitch(String strCase) {
switch(strCase) {
case "A":
return 1;
case "B":
return 2;
...
default:
return 10;
}
}
  • Inferring types with <>: while using generics RHS of stmt can be replaced with just <>

usage:

</pre>
List<String> list = new ArrayList<>();
<pre>

Note: No need to explicitly mentioned type on the RHS

  • Easy rethrow (replace multiple catch blocks with single catch block)

usage:

try {
// Reflective operations calling Class.forName,
// Class.newInstance, Class.getMethod,
// Method.invoke, etc.
...
} catch(final FileNotFoundException |
SQLException |
ClassNotFoundException) {
log(e);
throw e;
}

// try with resources
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(ds)) {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
}

NIO-2 Enhanced NIO
NIO-2 (Enhanced Asynchronous NIO)

invokedynamic
Dynamic linking of method by the JVM is introduced

Other Features:
Fork joins (for recursive functionalities) – Multi process environment

different ways of getting a new object in Java

For now I can think of getting a new object in 4 different ways

  • ‘new’ operator
  • reflection
  • deserialization
  • cloning

Using new operator

MyClass mc = new MyClass();

– Here MyClass.java availability will be checked at compile time.
– This is the most common way of creating objects in day-2-day activities of java programmer

using Class.forName(“<fully-qualified-class-name>”)

Class c = Class.forName("com.techforum.MyClass");
c.newInstance();

– Here code will compile without any issues, ie, with/without presence of MyClass.java in the classpath. But at runtime if the class is not found then it will throw run time exception. For this reason we are forced to handle ClassNotFoundException, it is an compile time exception, while loading a class at runtime

Using clone method

MyClass mc = new MyClass();
MyClass mcClone = mc.clone();

– Here we should make sure MyClass.java is implementing Cloneable interface in order to clone its object.

Deserialization

ObjectInputStream ois = new ObjectInputStream(is);
// Here is 'is' is stream representing the flat file
// that is generated while serializing the object
MyClass object = (MyClass) ois.readObject();

– Here we should make sure we are implementing Serializable interface if we wanna serialize/deserialize object

Java Security Model and its anatomy

Java Sandbox Model
Java has circumvented the virus or Trojan horse problems that plagued other models of software distributions. Java sandbox model is responsible for protecting a number of resources and it does it at number of levels.
Java programs are considered safe because they cannot run, install or propagate viruses and program itself cannot perform any action that is harmful to the user’s computing environment.

Anatomy of java application in SECURITY

Java Sandbox Model

Java Sandbox Model

The components drawn in the above figure play important role in java security model.

Byte Code verifier:
It ensures that all java class files that are loaded into JVM follow the rules of the Java language. In terms of resources, it helps enforce memory protections for all java programs. As the figure implies not all the class files are subjected to byte code verification.

Class Loader
One or more class loaders load all the java classes. Programmatically, the class loader can set permissions for each class it loads.

Access Controller
It allows OR prevents access from the core API to the operating system, based upon the policies set by the end user or administrator.

Security Manager
This is the primary interface between Core API and Operating System, however it exists for historic reasons

Security Package
It allows you to add security features to your applications as well as providing the basis by which java classes may be signed. This is a complex API and is further broken into following

  • Security Provider Interface– the means by which different security implementations can be plugged into security package.
  • Message Digests
  • Keys and certificates
  • Digital Signatures
  • Encryption (JCE & JSSE)
  • Authentication (through JAAS)

Key database
Key database is a set of keys used by the security infrastructure to create or verify digital signatures.
With respect to the sandbox, digital signatures play an important role because they provide authentication of who actually provided java class

Can we propagate exceptions in threads?

Prior to Java1.5 we cannot because the run() method in Runnable interface is written such that there is no return value as well as return exceptions in the method signature and hence the overridden method cannot propagate exceptions.

In Java1.5 SUN has released java.util.concurrent package in which alternative to Runnable i.e, Callable is introduced which can return any type and throw any subtype of Exception.

So,
– Using Runnable interface, one cannot propagate the exception and cannot return any value (return type is ‘void’), hence we should go for try-catch block to handle the exception
– Using Callable interface, we can propagate exception( or return a value) to the parent thread where we invoked the callable thread.

Sample code demonstrating the usage is uploaded in GITHub
https://github.com/ntallapa/TechForumWorkspace