Wednesday, August 27, 2008

Garbage Collection in Java

When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection.System.gc() method may be used to call it explicitly and does not force the garbage collection but only suggests that the JVM may make an effort to do the Garbage Collection.

Garbage Collection in Java

When an object is no longer referred to by any variable, java automatically reclaims memory used by that object. This is known as garbage collection.System.gc() method may be used to call it explicitly and does not force the garbage collection but only suggests that the JVM may make an effort to do the Garbage Collection.

Monday, August 25, 2008

Static in JAVA

access modifier. Signa: Variable-Static int b; Method- static void meth(int x)

1. When a member is declared as Static, it can be accessed before any objects of its class
are created and without reference to any object. Eg : main(),it must call before any object exit.

2. Static can be applied to Inner classes, Variables and Methods.

3. Local variables can’t be declared as static.

4. A static method can access only static Variables. and they can’t refer to this or super in any way.

5. Static methods can’t be abstract.

6. A static method may be called without creating any instance of the class.

7. Only one instance of static variable will exit any amount of class instances.

Wednesday, August 20, 2008

Constructor in Java

1. A constructor method is special kind of method that determines how an object is initialized when created.

2. Constructor has the same name as class name.

3. Constructor does not have return type.

4. Constructor cannot be over ridden and can be over loaded.

5. Default constructor is automatically generated by compiler if class does not have once.

6. If explicit constructor is there in the class the default constructor is not generated.

7. If a sub class has a default constructor and super class has explicit constructor the code will not compile.

What are access specifiers and access modifiers ?

Accesss specifiers Access modifiers
Public Public
Protected Abstract
Private Final
Static
Volatile Constant
Synchronized
Transient
Native

Interfaces in Java

Interfaces can be used to implement the Inheritance relationship between the non-related classes that do not belongs to the same hierarchy, i.e. any Class and any where in hierarchy. Using Interface, you can specify what a class must do but not how it does.



1. A class can implement more than one Interface.


2. An Interface can extend one or more interfaces, by using the keyword extends.


3. All the data members in the interface are public, static and Final by default.


4. An Interface method can have only Public, default and Abstract modifiers.


5. An Interface is loaded in memory only when it is needed for the first time.


6. A Class, which implements an Interface, needs to provide the implementation of all the methods in that Interface.


7. If the Implementation for all the methods declared in the Interface are not provided , the class itself has to declare abstract, other wise the Class will not compile.


8. If a class Implements two interface and both the Intfs have identical method declaration, it is totally valid.


9. If a class implements two interfaces both have identical method name and argument list, but different return types, the code will not compile.


10. An Interface can’t be instantiated. Intf Are designed to support dynamic method resolution at run time.


11. An interface can not be native, static, synchronize, final, protected or private.


12. The Interface fields can’t be Private or Protected.


13. A Transient variables and Volatile variables can not be members of Interface.


14. The extends keyword should not used after the Implements keyword, the Extends must always come before the Implements keyword.


15. A top level Interface can not be declared as static or final.


16. If an Interface species an exception list for a method, then the class implementing the interface need not declare the method with the exception list.


17. If an Interface can’t specify an exception list for a method, the class can’t throw an exception.


18 If an Interface does not specify the exception list for a method, he class can not throw any exception list.

------------------------------------------------
The general form of Interface is
Access interface name {
return-type method-name1(parameter-list);
type final-varname1=value;
}

-------------------------------------------------
Marker Interfaces : Serializable, Clonable, Remote, EventListener,

Java.lang is the Package of all classes and is automatically imported into all Java Program
Interfaces: Clonable , Comparable, Runnable


Declaration of access specifier and access modifiers

Class - Public, Abstract, Final
Inner Class - Public, Protected, Private, Final, Static,
Anonymous - Public, Protected, Private, Static
Variable - Public, Protected, Private, Final, Static, Transient, Volatile, Native
Method - Public, Protected, Private, Final, Abstract, Static, Native, Synchronized
Constructor - Public, Protected, Private
Free-floating code block - Static, Synchronized

Tuesday, August 19, 2008

Oops Concepts in java

Abstraction: Showing the essential and hiding the non-Essential is known as Abstraction.

Encapsulation: The Wrapping up of data and functions into a single unit is known as Encapsulation. Encapsulation is the term given to the process of hiding the implementation details of the object. Once an object is encapsulated, its implementation details are not immediately accessible any more. Instead they are packaged and are only indirectly accessed via the interface of the object.

Inheritance: is the Process by which the Obj of one class acquires the properties of Obj’s another Class. A reference variable of a Super Class can be assign to any Sub class derived from the Super class. Inheritance is the method of creating the new class based on already existing class , the new class derived is called Sub class which has all the features of existing class and its own, i.e sub class.Adv: Reusability of code , accessibility of variables and methods of the Base class by the Derived class.

Polymorphism: The ability to take more that one form, it supports Method Overloading & Method Overriding.

Method overloading: When a method in a class having the same method name with different arguments (diff Parameters or Signatures) is said to be Method Overloading. This is Compile time Polymorphism. Using one identifier to refer to multiple items in the same scope.

Method Overriding: When a method in a Class having same method name with same arguments is said to be Method overriding. This is Run time Polymorphism.o Providing a different implementation of a method in a subclass of the class that originally defined the method.

1. In Over loading there is a relationship between the methods available in the same class ,where as in Over riding there is relationship between the Super class method and Sub class method.

2. Overloading does not block the Inheritance from the Super class , Where as in Overriding blocks Inheritance from the Super Class.

3. In Overloading separate methods share the same name, where as in Overriding Sub class method replaces the Super Class.

4. Overloading must have different method Signatures , Where as Overriding methods must have same Signatures.

Dynamic dispatch: is a mechanism by which a call to Overridden function is resolved at runtime rather than at Compile time , and this is how Java implements Run time Polymorphism.

Dynamic Binding: Means the code associated with the given procedure call is not known until the time of call the call at run time. (it is associated with Inheritance & Polymorphism).

Program to generate 10 Random nos

/*Write a program to generate 10 Random nos. between 1 to 100, and it

should not follow with decimal point.

*/


class RandomDemo{

public static void main(String args[]){

for(int i=1;i<=10;i++){

System.out.println((int)(Math.random()*100));

}

}

}

Sunday, August 17, 2008

Java Hello World Program

Lets take a simple java program, we will write a program to display "Hello world"(as usual).

1. Open a notepad or text file, and start type the following coding.

2. Save this file with the name HelloWorld.java.
Tips: java file name and the class should always same - java compiler checks for this.
____________________________________________________________________
Code :
/** Comments * program to display "Hello world". */
class HelloWorld

{
public static void main (String args[])

{
System.out.println("Hello World!"); //Displays the enclosed String on the Screen Console
}

}
____________________________________________________________________

3. save this code under the path "C:\Program Files\Java\Jdk****\bin"
Note: **** It depends on java version installed in the system

4. open the command prompt, and get into the location C:\Program Files\Java\Jdk****\bin

5. Now Compile Java code.
From a command line, the command to compile this program is :
javac HelloWorld.java

6. Now run the program
java HelloWorld

You have written your first Java program! Congratulations :)

Tuesday, August 12, 2008

Flow Control Keywords in java

These are the following keywords used to control the flow through a block of code

1. switch : specifies the variable to be compared with the case statements.

2. case : Executes a block of code, dependent on what the switch searching for.

3. default : Executes this block of code if none of the switch-case statements match.

4. break : Tt helps, to exit from the block of code in which it resides.

5. continue : Stops the rest of the code following continue statement inside the loop and then begins the next iteration of the loop.

6. for : It is used to perform a conditional loop for a block of code.

7. if : Used to perform a logical condition based on true or false.

8. else : Executes an alternate block of code if an if condition is false.

9. return : It makes the flow to return from a method without executing code that follows.
It can also return a variable(optional)

10. while : if a condition is true then it executes a block of code Repeatedly.

11. do : It executes the block of code one time without any condition and checks for a while statement, whether the block of code to be executed again.

Class, Method, and Variable Modifiers in java

1. class : Keyword used to specify a class.

2. interface : Keyword used to specify an interface.

3. extends : Indicate the superclass that a subclass is extending.

4. final : It makes impossible to
i) Extend a class
ii) override a method
iii) reinitialize a variable.

5. abstract :
i) If abstract is used to declare a class then it can’t be instantiated.
ii) If abstract used in a method then must be implemented by nonabstract subclass.

6. implements : To indicate the interfaces that a class will implement.

7. new : Used to instantiate an object by invoking the constructor.

8. native : Indicates that a method is written in a platform-dependent language.

9. synchronized : Indicates that a method can be accessed by only one thread at a time.

10. static : Makes a variable or a method belong to a class as opposed to an instance.

11. transient :
i) It Prevents fields from ever being serialized.
ii) Transient fields are always skipped when objects are serialized.

12. volatile : Indicates a variable may change out of sync because it is used
in threads.

Sunday, August 10, 2008

Access Modifiers in java

1. private : Method or a variable can be accessible only from within its
own class.

2. protected : Method or a variable can be accessible only to classes in the same Package.

3. public : Class, method, or variable can be accessible from any other class in the same package or other packages.

Same class - Public, Protected, and Private
Same-package & subclass - Public, Protected
Same Package & non-sub classes - Public, Protected
Different package & Sub classes - Public, Protected
Different package & non- sub classes - Public

Naming Conventions in java

1. If a name has more the one word, then except the first word others should start with the uppercase.
Examples: setLayout, addLayoutComponent.

2. Variables, fields, and methods names should begin with a lowercase letter.
Examples: vehicle, myVehicle.

3. classes and interfaces names should begin with an uppercase letter.
Examples: Cube,ColorCube.

4. Named constants like final variables and fields should be written entirely in uppercase, and the parts of composite names are separated by underscores (_).
Examples: CENTER,MAX_VALUE.

5. Package names are sequences of dot-separated with lowercase names.
Example: java.awt.event.

Thursday, August 7, 2008

Java Keywords

1. Keywords are reserved words in java.

2. Keywords cannot be used as a Identifiers (names) for
i) Classes
ii) Methods
iii) Variables

3. Keywords have special meaning to the compiler.

4. Compiler with the help of keywords finds, what the source code is trying to do.

here is the list of JAVA Keywords


Tips: None of the keywords will have capital letters.

private methods on Interface

  Private methods can be used on interface from java 9.   This will help in     Avoiding code duplication.     Ensuring code re-usab...