All java c# asp.net .net servlet jdbc c sqlserver android ado.net javascript php wcf clearcase angularjs 2

What is Java Virtual Machine and how it is considered in context of Java’s platform independent feature?

When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by Java Virtual Machine (JVM) on whichever platform it is being run.

Platform independency is one of the key aspect that Java provides and the question you have asked is a very common and basic one. I'm trying to explain it below. Hope it will help.

Explanation :

The java compiler compiles a java program and converts it into bytecode because of which a java program can run on wide variety of platforms.

Java Virtual Machine(JVM) is a Java run time system. It is platform dependent i.e its implementation differs from platform to platform. It provides the runtime environment in which java bytecode can be executed.

In order to run a java program on different platforms, only we need to implement platform specific JVMs and these all will execute the same byte code.This is the feature due to which java is considered as platform independent.

Explanation :

The java compiler compiles a java program and converts it into bytecode because of which a java program can run on wide variety of platforms. 

Java Virtual Machine(JVM) is a Java run time systemIt is platform dependent i.e its implementation differs from platform to platform. It provides the runtime environment in which java bytecode can be executed.

In order to run a java program on different platforms, only we need to implement platform specific JVMs and these all will execute the same byte code.This is the feature due to which java is considered as platform independent.

Why Constructor name is same as Class name

Constructor is called immediately after an object of a class is created. To reduce the number of keywords in Java, name of the constructor is defined same as the Class name. The Java developers don't want to include new keyword for constructor. They want to keep it simple and precise.
So that it can be unambigously identified as the constructor. and how its get invoked implicitly when we create object of that class. It is invoked by the compiler because it has already been unambiguously identified because of its naming sheme.

Its more of a convenient convention rather than a logical reason. Methods in a class which have the same name as the class name are constructors - the easiest and simplest way to identify a method as a constructor. This is much better than telling the compiler about the constructor methods using config files or using annotations over methods…

Multithreading in java

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.

https://www.tutorialspoint.com/java/java_multithreading.htm

Multithreading in java is a process of executing multiple threads simultaneously.

Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Java Multithreading is mostly used in games, animation etc.

ADVANTAGES OF MULTI THREADING:

1) It doesn't block the user because threads are independent and you can perform multiple operations at same time.

2) You can perform many operations together so it saves time.

3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.

In this process we execute multiple threads simultaneously.Because thread is basically a lightweight sub-process, a smallest unit of processing.As we know Multiprocessing and multithreading, both are used to achieve multitasking purposes only.But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.And we use multithreading for animation,gaming etc.its major advantage is Threads are independent so it doesn't affect other threads if exception occur in a single thread and it can perform multiple operations at a time and saves our time to.

Rest API Authentication in Java

In token-based authentication, the client exchanges hard credentials (such as username and password) for a piece of data called token. Instead of sending the hard credentials in every request, the client will send the token to the server to perform authentication and authorization.

 in this RESTful services tutorial, we will see about how to do HTTP basic authentication. There are many ways to implement authentication in RESTful web services. HTTP basic authentication is the first step in learning security.

http://javapapers.com/web-service/restful-services-http-basic-authentication/

What is static polymorphism and dynamic polymorphism?

STATIC POLYMORPHISM:

A method which will bind at compile time will execute in runtime is called as static polymorphism or early binding or compile time polymorphism

DYNAMIC POLYMORPHISM:

A method which will bind at compile time will not execute, instead of that a method which will bind at runtime will execute is called as RUNTIME

POLYMORPHISM (or) DYNAMIC POLYMORPHISM is nothing but LATE BINDING.

Static polymorphism:

The process of binding the overloaded method within object at compile time is known as Static polymorphism due to static polymorphism utilization of resources (main memory space) is poor because for each and every overloaded method a memory space is created at compile time when it binds with an object. In C++ environment the above problem can be solve by using dynamic polymorphism by implementing with virtual and pure virtual function so most of the C++ developer in real worlds follows only dynamic polymorphism.

Dynamic polymorphism:

In dynamic polymorphism method of the program binds with an object at runtime the advantage of dynamic polymorphism is allocating the memory space for the method (either for overloaded method or for override method) at run time.

Polymorphism means one name, multiple forms. It allows us to have more than one function with the same name in a program.It allows us to have overloading of operators so that an operation can exhibit different behaviours in different instances.

What Is the difference between creating string with and without new operator?

The basic difference between them is memory allocation.

First option i.e

String s1 = "hello";

When you use this s1 is called as a string literal and memory for s1 is allocated at compile time.

But in 2nd case

String s2 = new String("hello");

In this case s2 is called as an object of String representing hello

When you tries to create two string literal using the first case, only one memory is referenced by those two literals. I mean String literals are working with a concept of string pool. when you create a 2nd string literal with same content, instead of allocating a new space compiler will return the same reference. Hence you will get true when you compare those two literals using == operator.

But in the 2nd case each time JVM will create a new object for each. and you have to compare their contents using equals() method but not with == operator.

If you want to create a new string object using 2nd case and also you don't want a new object, then you can use intern() method to get the same object.

String s = "hello";
String s1 = new String("hello").intern();
System.out.println(s == s1);

In this case instead of creating a new object, JVM will return the same reference s. So the output will be true

please see: https://goo.gl/6k8RTk

String str = new String("String"); always create a new object on the heap. Here creates a new String having for value the value of the constant "String" and assignates its reference to the variable str. String str = "String"; uses the String pool Here assignates the reference associated to the constant "String" to the variable str

Explain servlet life cycle.

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.

  • The servlet is initialized by calling the init () method.
  • The servlet calls service() method to process a client's request.
  • The servlet is terminated by calling the destroy() method.
  • Finally, servlet is garbage collected by the garbage collector of the JVM.
The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet:
  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. init method is invoked.
  4. service method is invoked.
  5. destroy method is invoked
there are three states of a servlet: new, ready and end. The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.

1) Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

2) Servlet instance is created

The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:
  1. public void init(ServletConfig config) throws ServletException  
 

4) service method is invoked

The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:
  1. public void service(ServletRequest request, ServletResponse response)   
  2.   throws ServletException, IOException  
 

5) destroy method is invoked

The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:
  1. public void destroy()  
 
  1. Loading Servlet Class : A Servlet class is loaded when first request for the servlet is received by the Web Container.
  2. Servlet instance creation :After the Servlet class is loaded, Web Container creates the instance of it. Servlet instance is created only once in the life cycle.
  3. Call to the init() method : init() method is called by the Web Container on servlet instance to initialize the servlet.

    Signature of init() method :

    public void init(ServletConfig config) throws ServletException
    
  4. Call to the service() method : The containers call the service() method each time the request for servlet is received. The service() method will then call the doGet() or doPost() methos based ont eh type of the HTTP request, as explained in previous lessons.

    Signature of service() method :

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    
  5. Call to destroy() method: The Web Container call the destroy() method before removing servlet instance, giving it a chance for cleanup activity.

What is Downcasting?

suppose you have a base class, and a class that derives from that base class either directly or indirectly. Then, anytime an object of that base class type is type cast into a derived class type, it is called a downcast.

When reference variable of child cl;ass refers to the object of parent class, it is known as downcasting, viceversa of upcasting. By the real time example i ll explain.

 If you want to give somebody a gift. You can chose from multiple things like pen, jewelry ,keychain etc. Now every item itself is a type of gift.When you give a gift you can chose to give it directly as item itself or as a gift by putting it inside a gift box.Now when we say gift = pen it is correct. But when we want to get a pen inside the gift box, then we need to UNWRAP the gift box like pen = (pen) (from the gift box).Let us see this in terms of the code. We have parent class gift and many children classes like Pen, KeyChain etc as mentioned above.

class Gift { }

class Pen extends Gift { }

So here upcasting means Gift objGift = new Pen() //it is automatic as pen is a gift

But down casting means Pen objPen = (Pen) (objGift) //It is explicit as gift needs to be unwrapped to get the pen inside.

UpCast -> From lower level class type to upper level class type

Downcast -> From upper level class type to lover level class type

Downcasting is assigning parent class reference object to the sub class which is not allowed in Java. However, if you do downcasting, there will not be any compiler error. But, there will be runtime exception in java.lang.ClassCastException.  Downcasting is legal in some scenarios where the actual object referred by the parent class is of sub class.

Example:
public class DownCastingExample {

	public static void main(String[] args) {
		SuperClass superClass1 = new SuperClass();
		SuperClass superClass2 = new SubClass();

		//valid downcasting
   SubClass subClass1 = (SubClass)superClass2;
        subClass1.method();
        //Invalid down casting
        SubClass subClass2 = (SubClass)superClass1;
    }
}

The reason it’s called a downcast is because of the way that inheritance diagrams are normally written – base classes are at the top and derived classes are down below the base classes. So, in downcasting, you are going down the inheritance diagram by taking an object of a base class (at the top), and then trying to convert into the type of one of the derived classes (going down).So,It is the casting from a general to a more specific type, i.e. casting down the hierarchy.

DownCasting is also a kinf of Boxing . Where One Class is created as Reference of the Other Class. For Example class Hotel { } class Room: Hotel { } Here Hotel contains many rooms if (room is Hotel) { Hotel m = (Hotel)room; }

What is drawable folder in android?

A compiled visual resource that can used as a backgrounds,banners, icons,splash screen etc.
By default Android maintain three folders for the images with different resolution reason behind it is the use or the resolution of the Android Device on which the application gonna execute. hdpi image folder maintain images for the Android Broad Screen set or Android Phones with the Higher resolution. ldpi for Lower images quality which supports by the earlier sets of the android mdpi for medium images support xhdi images folder for devices with maximum resolution. Android OS select the image it self by checking the compatible device and its resolution. Hope it helps. Accept if you get your explanation.

What are key OOPS principals in java?

There are mainly four concepts called 1. Inheritance, 2. Ploymorphism, 3. Abstraction and 4. Encaptulation and these four things are referred as OOP principals in Java. It is a very basic question and there are a lot of answer you will be able to find out if you google it. Below is a link for your benefit.
  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

There are three main features of OOPS:

  1. Encapsulation
  2.  Inheritance
  3.  Polymorphism

Encapsulation

Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes. 

Inheritance

the ability to create classes that share the attributes and methods of existing classes, but with more specific features. Inheritance is mainly used for code reusability. So you are making use of already written the classes and further extending on that. 

Polymorphism

In Core, Java Polymorphism is one of easy concept to understand. Polymorphism definition is that Poly means many and morphos means forms. It describes the feature of languages that allows the same word or symbol to be interpreted correctly in different situations based on the context. There are two types of Polymorphism available in Java.