Saturday, 1 October 2016

Features of java

Features of java: 


  • The java programming language has a number of features that make it the language Of choice for most developers .The reason why java is so popular that it is object oriented platform independent does not use pointers has support for multi-threading has a robust exception handling mechanism has good security. Features allows us to create applets and Servlets for use on the web. 


1. Compiled and interpreted:

   A computer language is either compiled or interpreted.

   Java combines both features therefore it is two stage system.

   First java compiler translate source code into byte code and byte code is not machine
instruction therefore interpreter translate byte code into machine code.

2. Platform independent and interpreted:

   Java programs can be easily moved from one computer system to another system.

   If you upgrade in any operating system then it will not affect in the java programs.

3. Object oriented:

   Java is true object-oriented language.

   Almost everything in java is an object.

   Java is an extensive set of classes which are arranged in packages and we can use it by
inheritance.

4. Robust and secure:

   Java a robust language because it provide many safeguards to ensure reliable code.

   It will check data type compile time and runtime.

   Security becomes an important point for a java programming on internet.

5. Distributed:

   Java is designed as a distributed language for creating application on networks.

   It has the ability to share both data and programs.

   In java multiple programmers at multiple remote locations to collaborate and work
together on a single project.

6. Simple small and familiar:

   Java is a small and simple language.

   Many features of C and C++ are not come in java so it become small.

   For example, java does not use pointers, operator overloading, preprocessor header

files, go to statement, multiple inheritance.

   Java has more feature of C and C++ so that java is familiar to us.

7. Multithreaded and interactive:

   Multithreaded means handling multiple tasks simultaneously.

   Java support multithreaded means we need not wait for the application to finish one

task before beginning another task.

   For example, we can listen to an audio clip while scrolling a page.

   This improves interactive program.

8. High-performance:

   Java architecture is defined to reduce error and amount of time.

   So that java gives high performance.

9. Dynamic and extensible:

   Java is dynamic language because it is capable of dynamically linking a new class
libraries, method and classes.

   Java program support functions written in other language such as C and C++.

   These functions are known as native methods.


10.Monitoring and manageability:

   Java supports a number of APIs, such as JVM Monitoring and management interface.

   It manages java programs.


Why java is so popular?


The reason why java is so popular is that it is :

   object-oriented

   platform independent

   Does not use pointer

   support multi-threading

   robust exception handling mechanism

   Full network support

   Good security

   Allows you to create applets and servlets for use on the web.

However, most importantly java is popular because Java is Easy

Friday, 30 September 2016

Initialize Blocks and Class Initialize Java

 Discuss Initialize Blocks and Class Initialize blocks.



 First of all, there are two types of initialization blocks: 


1. Instance Initialization Blocks (Initialization Blocks)
2. Static Initialization Blocks (Class Initialization Blocks).



1. Initializer block


In a class definition, we can have a member block with no name. Such a block is known as the initializer block.

An initializer block is never invoked by any application directly since it does not have any name. However, it is always invoked on an instance as soon as it is allocated.

It is invoked just before the constructor is invoked on the instance. 




Rectangle(int l, int w)
{
 // remove the increment of count from the constructor
setDimensions(l, w);
}
{
 // initializer block, executed whenever any instance is
created.
count++;
 } 


The initializer block in a class is invoked whenever any of the constructor for the class is invoked. This block is always executed before the constructor code is executed.

The initialization process for an instance is thus executed in the following sequence. Whenever any constructor is invoked with the new operator to create a new instance, first the space is allocated for the instance depending on the instance variables declared in the class.

When this space is allocated, its instance variables have the default initial valueaccording to the type of variable, i.e. numeric types will be 0, boolean types will be false and reference types will be null; now in case the instance variable declaration has an assignment, then such an assignment will be executed on the instance; for example, in caseof the Rectangle class, if the instance variable length were declared as

int length = 7; // instance declaration with assignment


then the assignment length = 7 would be executed initially. Now after executing instance variable initialization,  the initializer block would be executed, and then it is followed by execution of the specific constructor code that is invoked.





2. Class Initializer Block

Just like we have the constructor for initializing the instance variables, we use the class initializer block to initialize the class variables.

A class initializer block is created just like the initializer block, but it is declared to be static. We also call this block as the static block.

We also call this block as the static block, e.g. we may declare a static block as given

below...


class Rectangle
{
                static int count;
                static
                {
                                count = 0;
                                System.out.println("Inside a class initializer
                                block");
                }
}



Just like the initializer block, the class initializer block does not have a name and is 
not invoked directly by an application. This block is automatically executed whenever the 
class is loaded.


A class is normally loaded only once at the runtime. The class would be loaded by 
the JVM only if the class is used in an application.

Difference between Initialize Block & Class Init Block


The static block is only loaded when the class object is created by the JVM for the 
1st time whereas init {} block is loaded every time class object is created. Also first the static 
block is loaded then the init block. 






public class LoadingBlocks {
          static
          {
                   System.out.println("Inside static");
          }
          {
                   System.out.println("Inside init");
          }
          public static void main(String args[])
          {
                   new LoadingBlocks();
                   new LoadingBlocks();
                   new LoadingBlocks();
          }
}
Output:
          Inside static
          Inside init
          Inside init
          Inside init







This code should illustrate in which order they both are executed
public class Test
{
                 static int staticVariable;
                 int nonStaticVariable;
                 // Static initialization block:
                 // Runs once (when the class is initialized)
                 static
                 {
                                                System.out.println("Static initalization.");
                                                staticVariable = 5;
                 }
                 // Instance initialization block:
                 // Runs each time you instantiate an object
                 {
                                 System.out.println("Instance initialization.");
                                 nonStaticVariable = 7;
                 }
                 public Test()
                 {
                                System.out.println("Constructor.");
                 }
                 public static void main(String[] args)
                 {
                                 new Test();
                                 new Test();
                 }
}
Output :
                Static initalization.
                Instance initialization.
                Constructor.
                Instance initialization.
                Constructor.




IN SHORT :

Initializer block is used to define the activity that is required to be carried out whenever any instance is created for the class. There can be any number of initializer blocks in a class definition. These would be combined and treated as one initializer block. This code is executed just before any of the constructor code is executed. 

Class initializer block is used to initialize the class variables. This block is executed only once when the class is loaded, and similar to the initializer block in a class definition we can have any number of class initializer blocks, which would be combined and treated as
one.

Labels

ADVERTISEMENT