Monday, February 16, 2015

How System.out.println() actually works in java ? #interview

System is a class name( class name begin with capital letter according to naming convention).
System is a final class,  and among it's functionality, are standard input, standard output and error output streams.

 out is a (public static final) variable of type PrintStream declared in the System class. PrintStream is another class.

Out is a static final member of the System class, and it's type is PrintStream. It is your standard output stream, that is ready to accept the output data to be sent to a console or any other destination.

 println() is an overloaded method defined in the PrintStream class.


So System class would be something like

class System
{
public static final PrintStream out;
//other things
}

and the PrintStream class

 class PrintStream
{
//variables defined
public void println()
{
//things it does
}

}


Java Reflection and its usage. #interview

The name reflection is used to describe code which is able to inspect other code in the same system (or itself).
Reflection is a key mechanism to allow an application or framework to work with code that might not have even been written yet! Reflection allows instantiation of new objects, invocation of methods, and get/set operations on class variables dynamically at run time without having prior knowledge of its implementation.

For example: say you have an object of an unknown type in Java, and you would like to call a 'doSomething' method on it if one exists. Java's static typing system isn't really designed to support this unless the object conforms to a known interface, but using reflection, your code can look at the object and find out if it has a method called 'doSomething' and then call it if you want to.

imagine the object in question is foo
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.


Reflection is important since it lets you write programs that does not have to "know" everything at compile time, making them more dynamic, since they can be tied together at runtime. The code can be written against known interfaces, but the actual classes to be used can be instantiated using reflection from configuration files.

Usage :Java reflection is useful while writing frameworks and all, spring seems to use it.
Reflection is usually slower then statically compiled methods, you loose compile time type safety.
Everywhere you want to be able to dynamically plug in classes into your code. Lot's of object relational mappers use reflection to be able to instantiate objects from databases without knowing in advance what objects they're going to use. Plug-in architectures is another place where reflection is usefull. Being able to dynamically load code and determine if there are types there that implement the right interface to use as a plugin is important in those situations

Classloader in java and working. #interview

The Java Classloader is a part of the JRE that dynamically loads Java classes into the JVM. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders.
Each Java class must be loaded by a class loader.


When the JVM is started, three class loaders are used:
(1) Bootstrap class loader:It loads the core Java libraries located in the <JAVA_HOME>/jre/libdirectory. Bootstrap class loade part of the core JVM and written in native code.
(2) Extensions class loader:It loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property)
(3)System class loader:It loads code found on java.class.path, which maps to the CLASSPATH.
Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap classloader is a parent of all other classloaders. Consequently, it is the only one without a parent.
Next comes the extension classloader. It has the bootstrap classloader as parent and is responsible for loading classes from all .jar files kept in the java.ext.dirs path–these are available regardless of the JVM’s classpath.
The third and most important classloader from a developer’s perspective is the system classpath classloader, which is an immediate child of the extension classloader. It loads classes from directories and jar files specified by the CLASSPATH environment variable, java.class.path system property or -classpath command line option.





overloading the main method in Java? #interview

As all of you are familiar with concept of polymorphism/overloading and overriding methods in Java. In case need more polymorphism rules refer Click here...


You can overload the main() method, but only public static void main(String[] args) will be used when your class is launched by the JVM.
For example:
public class Test1 {
    public static void main(String[] args) {
        System.out.println("main(String[] args)");
    }

    public static void main(String arg1) {
        System.out.println("main(String arg1)");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("main(String arg1, String arg2)");
    }
}
That will always print main(String[] args) when you run java Test1 ... from the command line, even if you specify one or two command-line arguments.
You can call the main() method yourself from code, of course - at which point the normal overloading rules will be applied.

Friday, November 14, 2014

public static void main in java


.
Why the word static is used in “public static void main(String arg[])” ?
When we enter the command “java HelloWorld psg”. Internally this command is converted to HelloWorld.main(“psg”).
We are trying to invoke the main function using class name. We are trying to invoke the main function without creating an object of HelloWorld class. Due to this reason we have to declare the main function as static.
.
Why the word void is used in “public static void main(String arg[])” ?
This means the main function does not return anything.
.
Why the word main is used in “public static void main(String arg[])” ?
When we enter the command “java HelloWorld psg”. Internally this command is converted to HelloWorld.main(“psg”).
The JVM will definitely will look for a main function in HelloWorld class.
Hence the method name must be “main”.

Thursday, November 28, 2013

What are the essential skills of a Java/J2EE or Enterprise Java developer?

According to me if you are seeking to be a really effective JEE Developer, these are the following skills I would recommend.
Technical

  • Knowledge of OOPs concepts.
  • Basic knowledge of how JVM works and concepts like hashing.
  • At least one framework( Struts or Spring).
  • JSP and one scripting language(Javascript,/JQuery). I would advise you to get a good grip on HTML/XHTML fundamentals too. Clients nowadays are very particular about UI design.
  • EJB( knowledge of CORBA would be a bonus).
  • At least one application server( Weblogic/Websphere/JBoss) you should be having some knowledge about.
  • Web Services, considering many organizations are switching to it, as a better alternative to EJB's.
  • One ORM tool like Hibernate.
  • Knowledge of one configuration management tool like CVS/SVN/Mantis/Accurev.
  • Knowledge of any build tool like ANT/Maven.
  • Unit testing tools like JUnit.
  • Good idea about open source tools available on the Net.
  • Knowledge of design patterns.

Overall, a really good JEE developer should be having a holistic knowledge of the whole application development process. The good old days, when candidates who just knew how to program Hello World in Java, were recruited, are gone.

Non Technical 

  • Communication skills is a must, you will be interacting with clients, your managers, QA team, and effective communication will go a long way, especially in a networked environment.
  • Good presentation skills, these would come in handy, when you seek to do a walk through with the client or  explain a new tool to the team.
/Deepak Pandey

Wednesday, September 25, 2013

Fibnocci series easiest way...

public static void main(String[] args) {
  int f = 0;
  int g = 1;

  for(int i = 1; i <= 10; i++)
  {
    System.out.print(f + " ");
    f = f + g;
    g = f - g;
  } 

  System.out.println();
}

Easiest way to reverse string in java and check for palindrome...

class RevString
{
    static String reverse(String str) {
       char[] c = str.toCharArray();
       char[] r = new char[c.length];
       int end = c.length - 1;
    
        for (int n = 0; n <= end; n++) {
           r[n] = c[end - n];
            }
           return new String(r);
        }

 bool isPalindrome(string str) {
  int end= str.length();
  for (int i=0;i<(end/ 2)+ 1;++i) {   
      if (str.charAt(i) != str.charAt(end- i - 1)) { 
         return false;
     }
  }

  return true;
}
 public static void main (String[] args)
 {
  System.out.println(RevString.reverse("hello"));
System.out.println(RevString.isPalindrome("liril"));
 }
}

Creating mirror of BST