Difference Between in Java
Difference Between in Java
Difference between non-static and static variable ?
Non-Static method | Static method | |
---|---|---|
1 | These method never be preceded by static keyword Example: void fun1() { ...... ...... } | These method always preceded by static keyword Example: static void fun2() { ...... ...... } |
2 | Memory is allocated multiple time whenever method is calling. | Memory is allocated only once at the time of loading. |
3 | It is specific to an object so that these are also known as instance method. | These are common to every object so that it is also known as member method or class method. |
4 | These methods always access with object reference Syntax: Objref.methodname(); | These property always access with class reference Syntax: className.methodname(); |
5 | If any method wants to be execute multiple time that can be declare as non static. | If any method wants to be execute only once in the program that can be declare as static . |
Difference between Method and Constructor
Method | Constructor | |
---|---|---|
1 | Method can be any user defined name | Constructor must be class name |
2 | Method should have return type | It should not have any return type (even void) |
3 | Method should be called explicitly either with object reference or class reference | It will be called automatically whenever object is created |
1 | Method is not provided by compiler in any case. | The java compiler provides a default constructor if we do not have any constructor. |
Difference between package keyword and import keyword
- Package keyword is always used for creating the undefined package and placing common classes and interfaces.
- import is a keyword which is used for referring or using the classes and interfaces of a specific package.
Difference between Class and Object
Class | Object | |
---|---|---|
1 | Class is a container which collection of variables and methods. | object is a instance of class |
2 | No memory is allocated at the time of declaration | Sufficient memory space will be allocated for all the variables of class at the time of declaration. |
3 | One class definition should exist only once in the program. | For one class multiple objects can be created. |
Difference between Statement and PreparedStatement ?
Statement | PreparedStatement | |
---|---|---|
1 | Statement interface is slow because it compile the program for each execution | PreparedStatement interface is faster, because its compile the command for once. |
2 | We can not use ? symbol in sql command so setting dynamic value into the command is complex | We can use ? symbol in sql command, so setting dynamic value is simple. |
3 | We can not use statement for writing or reading binary data (picture) | We can use PreparedStatement for reading or writing binary data. |
Difference between throw and throws ?
throw | throws | |
---|---|---|
1 | throw is a keyword used for hitting and generating the exception which are occurring as a part of method body | throws is a keyword which gives an indication to the specific method to place the common exception methods as a part of try and catch block for generating user friendly error messages |
2 | The place of using throw keyword is always as a part of method body. | The place of using throws is a keyword is always as a part of method heading |
3 | When we use throw keyword as a part of method body, it is mandatory to the java programmer to write throws keyword as a part of method heading | When we write throws keyword as a part of method heading, it is optional to the java programmer to write throw keyword as a part of method body. |
Difference between checked Exception and un-checked Exception ?
Checked Exception | Un-Checked Exception | |
---|---|---|
1 | checked Exception are checked at compile time | un-checked Exception are checked at run time |
2 | e.g. IOException, SQLException, FileNotFoundException etc. | e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberNotFoundException etc. |
Difference between Abstraction and Encapsulation ?
Encapsulation is not provides fully security because we can access private member of the class using reflation API, but in case of Abstraction we can't access static, abstract data member of class.
Difference between Abstract and Interface ?
Abstract | Interface | |
---|---|---|
1 | It is collection of abstract method and concrete methods. | It is collection of abstract method. |
2 | There properties can be reused commonly in a specific application. | There properties commonly usable in any application of java environment. |
3 | It does not support multiple inheritance. | It support multiple inheritance. |
4 | Abstract class is preceded by abstract keyword. | It is preceded by Interface keyword. |
5 | Which may contain either variable or constants. | Which should contains only constants. |
6 | The default access specifier of abstract class methods are default. | There default access specifier of interface method are public. |
7 | These class properties can be reused in other class using extend keyword. | These properties can be reused in any other class using implements keyword. |
8 | Inside abstract class we can take constructor. | Inside interface we can not take any constructor. |
9 | For the abstract class there is no restriction like initialization of variable at the time of variable declaration. | For the interface it should be compulsory to initialization of variable at the time of variable declaration. |
10 | There are no any restriction for abstract class variable. | For the interface variable can not declare variable as private, protected, transient, volatile. |
11 | There are no any restriction for abstract class method modifier that means we can use any modifiers. | For the interface method can not declare method as strictfp, protected, static, native, private, final, synchronized. |
Difference between Overloading and Overriding ?
Overloading | Overriding | |
---|---|---|
1 | Whenever same method or Constructor is existing multiple times within a class either with different number of parameter or with different type of parameter or with different order of parameter is known as Overloading. | Whenever same method name is existing multiple time in both base and derived class with same number of parameter or same type of parameter or same order of parameters is known as Overriding. |
2 | Arguments of method must be different at least arguments. | Argument of method must be same including order. |
3 | Method signature must be different. | Method signature must be same. |
4 | Private, static and final methods can be overloaded. | Private, static and final methods can not be overrided. |
5 | Access modifiers point of view no restriction. | Access modifiers point of view not reduced scope of Access modifiers but increased. |
6 | Also known as compile time polymorphism or static polymorphism or early binding. | Also known as run time polymorphism or dynamic polymorphism or late binding. |
7 | Overloading can be exhibited both are method and constructor level. | Overriding can be exhibited only at method leve. |
8 | The scope of overloading is within the class. | The scope of Overriding is base class and derived class. |
9 | Overloading can be done at both static and non-static methods. | Overriding can be done only at non-static method. |
10 | For overloading methods return type may or may not be same. | For overriding method return type should be same. |
Difference between sleep() and suspend() ?
Sleep() can be used to convert running state to waiting state and automatically thread convert from waiting state to running state once the given time period is completed. Where as suspend() can be used to convert running state thread to waiting state but it will never return back to running state automatically.
Difference between String and StringBuffer ?
String | StringBuffer | |
---|---|---|
1 | The data which enclosed within double quote (" ") is by default treated as String class. | The data which enclosed within double quote (" ") is not by default treated as StringBuffer class |
2 | String class object is immutable | StringBuffer class object is mutable |
3 | When we create an object of String class by default no additional character memory space is created. | When we create an object of StringBuffer class by default we get 16 additional character memory space. |
Difference between StringBuffer and StringBuilder ?
All the things between StringBuffer and StringBuilder are same only difference is StringBuffer is synchronized and StringBuilder is not synchronized. synchronized means one thread is allow at a time so it thread safe. Not synchronized means multiple threads are allow at a time so it not thread safe.
StringBuffer | StringBuilder | |
---|---|---|
1 | It is thread safe. | It is not thread safe. |
2 | Its methods are synchronized and provide thread safety. | Its methods are not synchronized and unable to provide thread safety. |
3 | Relatively performance is low because thread need to wait until previous process is complete. | Relatively performance is high because no need to wait any thread it allows multiple thread at a time. |
1 | Introduced in 1.0 version. | Introduced in 1.5 version. |
Difference between Method and Constructor ?
Method | Constructor | |
---|---|---|
1 | Method can be any user defined name | Constructor must be class name |
2 | Method should have return type | It should not have any return type (even void) |
3 | Method should be called explicitly either with object reference or class reference | It will be called automatically whenever object is created |
1 | Method is not provided by compiler in any case. | The java compiler provides a default constructor if we do not have any constructor. |
Difference between non-static and static Method
Non-Static method | Static method | |
---|---|---|
1 | These method never be preceded by static keyword Example: void fun1() { ...... ...... } | These method always preceded by static keyword Example: static void fun2() { ...... ...... } |
2 | Memory is allocated multiple time whenever method is calling. | Memory is allocated only once at the time of class loading. |
3 | It is specific to an object so that these are also known as instance method. | These are common to every object so that it is also known as member method or class method. |
4 | These methods always access with object reference Syntax: Objref.methodname(); | These property always access with class reference Syntax: className.methodname(); |
5 | If any method wants to be execute multiple time that can be declare as non static. | If any method wants to be execute only once in the program that can be declare as static . |
Difference between non-static and static variable
Non-static variable | Static variable | |
---|---|---|
1 | These variable should not be preceded by any static keyword
Example:
class A { int a; } | These variables are preceded by static keyword.
Exampleclass A { static int b; } |
2 | Memory is allocated for these variable whenever an object is created | Memory is allocated for these variable at the time of loading of the class. |
3 | Memory is allocated multiple time whenever a new object is created. | Memory is allocated for these variable only once in the program. |
4 | Non-static variable also known as instance variable while because memory is allocated whenever instance is created. | Memory is allocated at the time of loading of class so that these are also known as class variable. |
5 | Non-static variable are specific to an object | Static variable are common for every object that means there memory location can be sharable by every object reference or same class. |
6 | Non-static variable can access with object reference.
Syntaxobj_ref.variable_name | Static variable can access with class reference.
Syntaxclass_name.variable_name |