Showing posts with label corejava. Show all posts
Showing posts with label corejava. Show all posts

Monday 15 July 2013

wrapper classes in java

// siddhu vydyabhushana // 2 comments
Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.
The wrapper classes in java servers two primary purposes.
  • To provide mechanism to ‘wrap’ primitive values in an object so that primitives can do activities reserved for the objects like being added to ArrayList, Hashset, HashMap etc. collection.
  • To provide an assortment of utility functions for primitives like converting primitive types to and from string objects, converting to various bases like binary, octal or hexadecimal, or comparing various objects.
The following two statements illustrate the difference between a primitive data type and an object of a wrapper class:
int  x = 25;
Integer  y = new Integer(33); 
The first statement declares an int variable named x and initializes it with the value 25. The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y.
Below table lists wrapper classes in Java API with constructor details.
PrimitiveWrapper ClassConstructor Argument
booleanBooleanboolean or String
byteBytebyte or String
charCharacterchar
intIntegerint or String
floatFloatfloat, double or String
doubleDoubledouble or String
longLonglong or String
shortShortshort or String
Below is wrapper class hierarchy as per Java API
wrapper class image 1
As explain in above table all wrapper classes (except Character) take String as argument constructor. Please note we might get NumberFormatException if we try to assign invalid argument in constructor. For example to create Integer object we can have following syntax.
Integer intObj = new Integer (25); 
Integer intObj2 = new Integer ("25"); 
Here in we can provide any number as string argument but not the words etc. Below statement will throw run time exception (NumberFormatException)
Integer intObj3 = new Integer ("Two");
The following discussion focuses on the Integer wrapperclass, but applies in a general sense to all eight wrapper classes.
The most common methods of the Integer wrapper class are summarized in below table. Similar methods for the other wrapper classes are found in the Java API documentation.
MethodPurpose
parseInt(s)returns a signed decimal integer value equivalent to string s
toString(i)returns a new String object representing the integer i
byteValue()returns the value of this Integer as a byte
doubleValue()returns the value of this Integer as an double
floatValue()returns the value of this Integer as a float
intValue()returns the value of this Integer as an int
shortValue()returns the value of this Integer as a short
longValue()returns the value of this Integer as a long
int compareTo(int i)Compares the numerical value of the invoking object with that of i. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.
static int compare(int num1, int num2)Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2.
boolean equals(Object intObj)Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it returns false.
Let’s see java program which explain few wrapper classes methods.
  1. package WrapperIntro;  
  2.   
  3. public class WrapperDemo {  
  4.       
  5.     public static void main (String args[]){  
  6.         Integer intObj1 = new Integer (25);  
  7.         Integer intObj2 = new Integer ("25");  
  8.         Integer intObj3= new Integer (35);  
  9.         //compareTo demo  
  10.         System.out.println("Comparing using compareTo Obj1 and Obj2: " + intObj1.compareTo(intObj2));  
  11.         System.out.println("Comparing using compareTo Obj1 and Obj3: " + intObj1.compareTo(intObj3));  
  12.           
  13.         //Equals demo  
  14.         System.out.println("Comparing using equals Obj1 and Obj2: " + intObj1.equals(intObj2));  
  15.         System.out.println("Comparing using equals Obj1 and Obj3: " + intObj1.equals(intObj3));  
  16.                   
  17.         Float f1 = new Float("2.25f");  
  18.         Float f2 = new Float("20.43f");  
  19.         Float f3 = new Float(2.25f);  
  20.         System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2));  
  21.         System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3));  
  22.           
  23.         //Addition of Integer with Float  
  24.         Float f = intObj1.floatValue() + f1;  
  25.         System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+"=" +f );  
  26.     }  
  27.       
  28. }    

Output

wrapper class image 2

valueOf (), toHexString(), toOctalString() and toBinaryString() Methods:

This is another approach to create wrapper objects. We can convert from binary or octal or hexadecimal before assigning value to wrapper object using two argument constructor. Below program explains the method in details.
  1. package WrapperIntro;  
  2.   
  3. public class ValueOfDemo {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Integer intWrapper = Integer.valueOf("12345");  
  7.         //Converting from binary to decimal  
  8.         Integer intWrapper2 = Integer.valueOf("11011"2);  
  9.         //Converting from hexadecimal to decimal  
  10.         Integer intWrapper3 = Integer.valueOf("D"16);  
  11.           
  12.         System.out.println("Value of intWrapper Object: "+ intWrapper);  
  13.         System.out.println("Value of intWrapper2 Object: "+ intWrapper2);  
  14.         System.out.println("Value of intWrapper3 Object: "+ intWrapper3);  
  15.         System.out.println("Hex value of intWrapper: " + Integer.toHexString(intWrapper));    
  16.         System.out.println("Binary Value of intWrapper2: "+ Integer.toBinaryString(intWrapper2));  
  17.     }  
  18. }  

Output

wrapper class image 3

Summary

  • Each of primitive data types has dedicated class in java library.
  • Wrapper class provides many methods while using collections like sorting, searching etc.
- See more at: http://www.w3resource.com/java-tutorial/java-wrapper-classes.php#sthash.TLq4jz3I.dpuf
Read More

Friday 5 July 2013

Implementing interfaces in java

// siddhu vydyabhushana // Leave a Comment

Once an interface has been defined, one or more classes can implement that interface.
To implement an interface, include the implements clause in a class definition, and then create the methods declared by the interface.
The general form of a class that includes the implements clause looks like this:
Access-specifier class classname [extends superclass] [implements interface, [, interface..]]
{
            // class body
      }
If a class implements from more than one interface, names are separated by comma.
If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface.
The methods that implement an interface must be declared as public.
The type-signature of implementing method must match exactly the type signature specified in the interface.
 interface religion
{
 String city = new String("Amritsar");
 void greet();
 void pray();
}

class gs implements religion
{
 public void greet()
 {
  System.out.println("We greet - ABCD");
 }
 public void pray()
 {
  System.out.println("We pray at " + city + " XYZ ");
 }
}

class iface1
{
 public static void main(String args[])
 {
  gs sikh = new gs();
  sikh.greet();
  sikh.pray();
 }
}

Output :
We greet - ABCD
We pray at Amritsar XYZ
EX 2 :
 interface i1
{
 void dispi1();
}

interface i2
{
 void dispi2();
}

class c1 implements i1
{
 public void dispi1()
 {
  System.out.println("This is display of i1");
 }
}

class c2 implements i2
{
 public void dispi2()
 {
  System.out.println("This is display of i2");
 }
}

class c3 implements i1, i2
{
 public void dispi1()
 {
  System.out.println("This is display of i1");
 }
 public void dispi2()
 {
  System.out.println("This is display of i2");
 }
}

class iface2
{
 public static void main(String args[])
 {
  c1 c1obj = new c1();
  c2 c2obj = new c2();
  c3 c3obj = new c3();   
  
  c1obj.dispi1();
  c2obj.dispi2();
  c3obj.dispi1();
  c3obj.dispi2();
  
 }
}

Output :
This is display of i1
This is display of i2
This is display of i1
This is display of i2
EX 3 : // Implementing interface having common function name
 interface i1
{
 void disp();
}

interface i2
{
 void disp();
}

class c implements i1, i2
{
 public void disp()
 {
  System.out.println("This is display .. ");
 }
}

class iface7
{
 public static void main(String args[])
 {
  c cobj = new c();
   
  cobj.disp();
 }
}

Output :
This is display .. 
Note : When implementing an interface method, it must be declared as public. It is possible for classes that implement interfaces to define additional members of their own.
Partial Implementation of Interface :
If we want to implement an interface in a class we have to implement all the methods defined in the interface.
But if a class implements an interface but does not fully implement the method defined by that interface, then that class must be declared as abstract.
EX :
 interface i1
{
 void disp1();
 void disp2();
}

abstract class c1 implements i1
{
 public void disp1()
 {
  System.out.println("This is display of 1");
 }
}

class c2 extends c1
{
 public void disp2()
 {
  System.out.println("This is display of 2");
 }
}

class iface
{
 public static void main(String args[])
 {
  c2 c2obj = new c2();
  c2obj.disp1();
  c2obj.disp2();
 }
}

Output :
This is display of 1
This is display of 2


Read More

Defining interfaces in java with syntax

// siddhu vydyabhushana // 1 comment

Defining interfaces in java with syntax:

Syntax :
[Access-specifier] interface interface-name
{
Access-specifier return-type method-name(parameter-list);
final type var1=value;
}
Where, Access-specifier is either public or it is not given.
When no access specifier is used, it results into default access specifier and if interface has default access specifier then it is only available to other members of the same package.
When it is declared as public, the interface can be used by any other code of other package.
       Interface-Name: name of an interface, it can be any valid identifier.
The methods which are declared having no bodies they end with a semicolon after the parameter list. Actually they are abstract methods;
Any class that includes an interface must implement all of the methods. Variables can be declared inside interface declarations.
They are implicitly final and static, means they can not be changed by implementing it in a class.
They must also be initialized with a constant value.
EX :
interface Item
{
       static final int code = 100;
       static final String name = "Fan";
       void display ( );
}

interface Area
{
       static final float pi = 3.14F;
       float compute ( float x, float y );
       void show ( );
}


Read More

introduction to Java Interface

// siddhu vydyabhushana // 1 comment
Interfaces are similar to abstract classes, but differ in their functionality. In interfaces, none of the methods are implemented means interfaces defines methods without body.

 Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. But, it can contain final variables, which must be initialized with values. 

Once it is defined, any number of classes can implement an interface. One class can implement any number of interfaces. If we are implementing an interface in a class we must implement all the methods defined in the interface as well as a class can also implement its own methods.

 Interfaces add most of the functionality that is required for many applications which would normally resort to using multiple inheritance in C++.
Read More

Constructor overloading in java

// siddhu vydyabhushana // 1 comment


Constructor overloading in java:

Along with method overloading, we can also overload constructors. Constructors having the same name with different parameter list is called constructor overloading.
EX :
 class Point
{
        int x;
        int y;
        Point(int a, int b)
        {
                x = a;
                y = b;
        }

}

class Circle
{
        int originX;
        int originY;
        int radius;

        //Default Constructor

        Circle()
        {
                originX = 5;
                originY = 5;
                radius = 3;

        }

        // Constructor initializing the coordinates of origin and the radius.

        Circle(int x1, int y1, int r)
        {
                originX = x1;
                originY = y1;
                radius = r;

        }


        Circle(Point p, int r)
        {
                originX = p.x;
                originY = p.y;
                radius = r;
        }

        void display()
        {
                System.out.println("--Center at " + originX + " and " + originY);
                System.out.println("Radius = " + radius);
        }

        public static void main(String args[])
        {
        
        Circle c1 = new Circle();
        Circle c2 = new Circle(10,20,5);
        Circle c3 = new Circle(new Point(15,25),10);

        c1.display();
        c2.display();
        c3.display();
        }
}

Output :
--Center at 5 and 5
Radius = 3
--Center at 10 and 20
Radius = 5
--Center at 15 and 25
Radius = 10
Above program is quite complicated here i am giving you perfect flow of program.
First of all note one thing that new ClassName() this is a short  syntax of creating object of any class.
And we all know that when we create object the constructor of that class will be called automatically.
So in our program first of all due to syntax Circle c1 = new Circle(); non parameterize constructor will be called for object c1 so we get output like Center at 5 and 5 Radius = 3 in c1.display().
Next due to syntax Circle c2 = new Circle(10,20,5); constructor which has 3 arguments will be called for object c2 so we get output like Center at 10 and 20 Radius = 5 in c2.display().
Now when we define object c3 our syntax is like Circle c3 = new Circle(new Point(15,25),10); so first of all it will create object for Point class so constructor of point class will be called and it will set parameter x and y.
Then constructor of circle class which has Point class object as an argument along with one int argument will be called and set all parameter as per program and we get output like Center at 15 and 25 Radius = 10 in c3.display().
We can also write
Point p1 = new Point(15,25);
Circle c3 = new Circle(p1,10);
This is how we can pass object as an argument in constructor.
We have already seen Call by reference in which we pass object as a method argument.
Now in next topic we will discuss about how you can return an object.
Read More

Command Line Argument in java

// siddhu vydyabhushana // Leave a Comment


Command Line Argument :
Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ). 
A command-line argument is the information that directly follows the program’s name on the command line when it is executed. 
To access the command-line arguments inside a Java program is quite easy—they are stored as strings in the String array passed to main( ).
class CommandLine
{
	public static void main(String args[]) 
	{
		for(int i=0; i < args.length; i++)
			System.out.println("args[" + i + "]: " +args[i]);
	}
}

Try executing this program, as shown here:
java CommandLine this is a test 100 -1
When you do, you will see the following output:
args[0]: this
args[1]: is
args[2]: a
args[3]: test
args[4]: 100
args[5]: -1
But first of all you should have the basic knowledge about command line and how any java program run through command prompt. CLICK HERE to know more.
Read More

Call by Refference in java

// siddhu vydyabhushana // 1 comment

Call by reference :
Here we pass reference as parameter in function calling.
We all know that reference means object so we pass object as parameter.
A reference to an argument (not value of argument) is passed to the parameter.
Inside the subroutine, this reference is used to access the actual argument specified in the call.
This means that changes made to the parameters will affect the argument used to call the subroutine.
When we pass an object to a method, the situation changes, because objects are passed by call-by-reference.
When we create a variable of a class type, we are only creating a reference to an object. Thus,
When you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument.
This effectively means that objects are passed to method do affect the object used as an argument. 
EX :
public class CallBy_Reference 
{
	public static void main(String[] args)
	{
	Reference r = new Reference(10,20);
	System.out.println("a and b before call............");
	System.out.println("a = "+r.a);
	System.out.println("b = "+r.b);
	r.call(r);		// CALL BY REFERENCE
	System.out.println("a and b after call.............");
	System.out.println("a = "+r.a);
	System.out.println("b = "+r.b);
	}
}

class Reference 
{
	int a,b;
	Reference(int i,int j)
	{
		a = i ;
		b = j;
	}
	void call(Reference r)
	{
		r.a = a * 2;
		r.b = b * 2;
	}
}

Output :
a and b before call............
a = 10
b = 20
a and b after call.............
a = 20
b = 40
You can see that after calling method value of original a and b is changed because of call by reference.
Here we pass "r" reference (Object) as parameter in method calling. So changes inside method will affect original variable of class.
Read More

Call by Value in java

// siddhu vydyabhushana // 1 comment
Now we all know that how to define and call the methods.
There are two types of calling method and those are
1. call by value
2. call by reference
Here we illustrate call by  value and in next topic we will look at call by reference.
In call by value when we call any method we pass value as method parameter so changing in local variables of the method doesn't`t affect the original variables of class.
This method copies the value of an argument into the formal parameter of the subroutine.
Therefore, changes made to the parameter of the subroutine have no effect on the argument.
In java, when we pass a primitive type to a method, it is passed by value.
Thus, what occurs to the parameter that receives the argument has no effect outside the method.
EX :
 public class CallBy_Value 
{
	public static void main(String[] args)
	{
		Value v = new Value(10,20);
		System.out.println("a and b before call............");
		System.out.println("a = "+v.a);
		System.out.println("b = "+v.b);
		v.call(v.a,v.b);		// CALL BY VALUE
		System.out.println("a and b after call............");
		System.out.println("a = "+v.a);
		System.out.println("b = "+v.b);
	}
}

class Value
{
	int a,b;
	Value(int i,int j)
	{
		a = i ;
		b = j;
	}
	void call(int a, int b)
	{
		a = a * 2;
		b = b * 2;
	}
}

Output :
a and b before call............
a = 10
b = 20
a and b after call............
a = 10
b = 20
You can see that after calling method we change value of a and b but it will not afect the original value of class` members because of call by value.
We pass value v.a and v.b as parameter and it will change local method`s a and b variables.
Read More

Method overloading in java

// siddhu vydyabhushana // 2 comments
Method overloading:
A class can contain any number of methods. Methods can be with parameter and without parameter.
The parameter in a method are called type signature.
It is possible in java to define two or more methods within the same class that share the same name, but with different parameter declarations (type signatures).
When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading.
Overloading methods demonstrate the concept of polymorphism.
When an overloaded method is invoked, java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to call.
Thus, overloaded methods must differ in the type and/or number of their parameters.
Overloaded methods may have different return types.
When java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
EX :
 
public class MethodOver  
{        
	int n1;
        int n2;
        MethodOver()
        {
		n1 = 10;
                n2 = 20;
        }
        void square()         
	{
        	System.out.println("The Square is " + n1 * n2);
        }
        void square(int p1)
        {         
        	n1 = p1;
                System.out.println("The Square is " + n1 * n2);     
   	}
        void square(int p1, int p2)        
	{
  		n1 = p1;
                n2 = p2; 
                System.out.println("The Square is " + n1 * n2);      
   	}
        public static void main(String args[])         
	{          
       		MethodOver obj1 = new MethodOver();
                obj1.square(); //call non parameterise method        
         	obj1.square(4);   //call method which has 1 argument   
              	obj1.square(7,8);  //call method which has 2 argument      
   	} 
}

Output :
The Square is 200
The Square is 80
The Square is 56
You can see that here we have 3 square methods with different argument.
Its called method overloading.
Read More

First java program

// siddhu vydyabhushana // Leave a Comment
This is demonstrating simple example program that displays the text,” My First JAVA program...............” on the console.
 public class FirstProg
{	//This is a first simple java program.
	public static void main(String[] args)
	{
		System.out.println("My First JAVA program...............");
	}
}

Explanation :
public :
A keyword of the Java language that indicates that the element that follows should be made available to other Java elements.
As a result, this keyword indicates that the FirstProg class is a public class, which means other classes can use it.
class :
Another Java keyword that indicates that the element being defined here is a class. All Java programs are made up of one or more classes.
A class definition contains code that defines the behavior of the objects created and used by the program.
FirstProg :
An identifier that provides the name for the class being defined here. While keywords, such as public and class, are words that are defined by the Java programming language, identifiers are words that you create to provide names for various elements you use in your program.
In this case, the identifier FirstProg provides a name for the public class being defined here.
{ :
The opening brace on line 2 marks the beginning of the body of the class. The end of the body is marked by the closing brace on line 7.
Everything that appears within these braces belongs to the class. As you work with Java, you’ll find that it uses these braces a lot.
//This is a first simple java program. :
This is a comment. Like most other programming languages, Java lets you enter a remark into a program’s source file.
The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code.
In this case, the comment describes the program and reminds you that the source file should be called FirstProg.java. Of course, in real applications, comments generally explain how some part of the program works or what a specific feature does.
Java supports three styles of comments. The one shown at this program is called a single-line comment. This type of comment must begin with // as shown in the program.
The second style of the comment is multiline commentThis type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment may be several lines long.
For example :
          /*This is a first
          simple java program. */
The third style of the comment is documentation comment. This type of comment is used to produce an HTML file that documents your program.
The documentation comment begins with a /** and ends with a */. Documentation comments are explained in next chapter.
public :
The public keyword is used again, this time to indicate that a method being declared here should have public access.
That means classes other than the FirstProg class can use it. All Java programs must have at least one class that declares a public method named main.
The main method contains the statements that are executed when you run the program.
static :
If you are want to execute any elements (properties) without an object means before the object creating then it must declare a static so compiler directly execute those static elements.
Here the main method is executed before the any object is creating so it must declare a static. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class.
This is necessary since main( ) is called by the Java interpreter before any objects are made.
You find all about the static keyword in next Chapter.
For now, just take my word that the Java language requires that you specify static when you declare the main method.
void :
In Java, a method is a unit of code that can calculate and return a value.
For example :
you could create a method that adds two numbers. Then, the addition would be the return value of the method.
If a method doesn’t need to return a value, you must use the void keyword to indicate that no value is returned. Because Java requires that the main method not return a value, you must specify void when you declare the main method.
main :
Finally, the identifier that provides the name for this method.
Java requires that this method be named main because main( ) is the method called when a Java application begins.
Besides the main method, you can also create additional methods with whatever names you want to use.
(String[] args) :
It’s called a parameter list, and it’s used to pass data to a method.
Java requires that the main method must receive a single parameter that’s an array of String objects.
By convention, this parameter is named args. If you don’t know what a parameter, a String, or an array is, don’t worry about it You can find out what a String, parameters and arrays are in the next chapter.
You have to write (String[] args) on the declaration for the main methods in all your programs. In this case, args receives any command-line arguments present when the program is executed.
This program does not make use of this information, but other programs shown later in further chapter for command-line argument.
Another { :
Another set of braces begins at line 4 and ends at line 6. These mark the body of the main method.
Notice that the closing brace in line 6 is paired with the opening brace in line 4, while the closing brace in line 7 is paired with the one in line 2.
This type of pairing is commonplace in Java. In short, whenever you come to a closing brace, it is paired with the most recent opening brace that hasn’t already been closed — that is, that hasn’t already been paired with a closing brace.
 System.out.println(“My First JAVA program...............”); :
This is the only statement in the entire program. It calls a method named println that belongs to the System.out object.
System is a predefined class, it is automatically included in your programs that provides access to the system and out is the output stream that is connected to the console.
The println method displays a line of text on the console. The text to be displayed is passed to the println method as a parameter in parentheses following the word println.
In this case, the text is the string literal My First JAVA program............... enclosed in a set of double quotation marks. As a result, this statement displays the text My First JAVA program............... on the console.
Note: In Java, statements end with a semicolon. Because this is the only statement in the program, this line is the only one that requires a semicolon.
Java is case-sensitive. Thus, Main is different from main.
} :
Line 6 contains the closing brace that marks the end of the main method body that was begun by the brace on line 4.
Another } :
Line 7 contains the closing brace that marks the end of the FirstProg class body that was begun by the brace on line 2. Because this program consists of just one class, this line also marks the end of the program.
To run this program, first of all save a text file named FirstProg.java . Then, compile it by running this command at a command prompt:
javac FirstProg.java
This command creates a class file named FirstProg.class that contains
the Java bytecodes compiled for the FirstProg class.
Now run the program by entering this command:
java FirstProg
Output :
My First JAVA program...............
Read More

Features of Java

// siddhu vydyabhushana // Leave a Comment

Features of Java:

1) Simple and Powerful
If the user already understands the basic concepts of object-oriented programming, learning Java with be much easier, Because Java inherits the C/C++ syntax and many of the object-oriented features of C++.so we can say that Java was designed to be easy to learn and use.
java provides a small number of clear ways to achieve a given task.
Unlike other programming systems that they provide dozens of complicated ways to perform a simple task.
2) Secure
Using Java Compatible Browser, anyone can safely download Java applets without the fear of viral infection or malicious intent because of its key design principle. So anyone can download applets with confidence that no harm will be done and no security will be violated.
Java achieves this protection by confining a Java program to the Java execution environment and by making it inaccessible to other parts of the computer.
3) Portable
Many types of computers and operating systems are in use throughout the world—and many are connected to the Internet. Java makes it possible to have the assurance that any result on one computer with Java can be replicated on another. So the code is run in the different platform has a same result.
4) Object-oriented
Java support the all the features of object oriented programming language such as Abstraction, Encapsulation, Inheritance, Polymorphism and Dynamic binding etc….
So with the help of these features user can reduce the complexity of the program develops in JAVA.
Java gave a clean, usable, realistic approach to objects so we can say that the object model in Java is simple and easy to extend.
5) Robust
Most programs in use today fail for one of the two reasons:
(i) memory management mistakes
For example, in C/C++, the programmer must manually allocate and free all dynamic memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, sometimes try to free some memory that another part of their code is still using. Java virtually eliminates these problems by managing memory allocation (with the help of new operator) and deallocation. (deallocation is completely automatic, because Java provides garbage collection for unused objects.)
(ii) mishandled exceptional conditions
With the help of Exception Handling (try……….catch block), the programmer can easily handle an error or exception so user can prevent the program by automatically stop the execution when an exception found.
Thus, the ability to create robust programs was given a high priority in the design of Java.
6) Multithreaded
Java supports programming, which allows the user to write programs that perform many functions simultaneously.
The two or more part of the program can run concurrently then each part of such a program is called a Thread and this type of programming is called multithreaded programming.
Each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking.
7) Architecture-neutral
The Java designers worked hard in achieving their goal “write once; run anywhere, anytime, forever” and as a result the Java Virtual Machine was developed.
Java is Architecture-neutral it generates bytecode that resembles machine code, and are not specific to any processor.
8) Interpreted and High performance
The source code is first compile and generates the code into an intermediate representation called Java bytecode which is a highly optimized set of instruction code.
This code can be interpreted on any system that has a Java Virtual Machine and generates the machine code.
Java bytecode was carefully designed by using a just-in-time compiler so that it can be easily translated into native machine code for very high performance.
Most of the earlier cross-platform solutions are run at the expense of performance.
9) Distributed
Java allows the object can access the information across the network with the help of RMI (Remote Method Invocation) means this allowed objects on two different computers to execute procedures remotely. So this feature supports the client/server programming.
10) Dynamic
Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time. This makes it possible to dynamically link code in a safe and perfect manner.
Read More