Good News......

Hello my Friends, if you would like to create your web and window based applications, Web site and ECommerce Applications, please contact with Unique IT Solutions Pte Ltd , go and view their services.

Java Training Session 2 (Introduction to the Java Programming Language)

1. Key Concept of Java Programming

Sample Java Code

class FirstJava
{
public static void main(String args[ ]) // main Method()
{
System.out.println(“Hello World!”); // Display the output message
}
}

Java program will be start with the class and you can give the name of the class whatever you want. When you save the file, just give the file name using the class name. Eg. If you class name is FirstJava, you need to give file name in helloWorld.java. When you give the filename, you need to check where is the main method. If your class has main method, you can give the file name the same with the class name that have main method. All the program must have at least one main method().

Eg. You have two class.

class A
{

}
class B
{
public static void main(String args[])
{
//some code here
}
}

The file name should be B.Java.


Compilation Stage

C:\> cd \myfolder
C:\myfolder> dir
C:\myfolder> set path=%path%;C:\Program Files\Java\jdk1.6.0\bin
C:\myfolder> javac FirstJava.java
C:\myfolder> dir
C:\myfolder> java FirstJava

Output: Hello World!
(if you are not clear how to execute the program, please go to Session 1)

Theories and Concepts


1. Why is main() method static?


The method can be access directly with the help of ClassName. So when a program is started the jvm search for the class with main method and calls it without creating an object of the class.


2. What is the difference between static methods and instance methods?

Instance method belongs to the instance of a class therefore it requires an instance before it can be invoked, whereas static method belongs to the class itself and not to any class instance so it doesn’t need an instance to be invoked. Instance methods use dynamic (late) binding, whereas static methods use static (early) binding.

3. What is difference between abstract class and interface?

1) A class is called abstract when it contains at least one abstract method. It can also contain n numbers of concrete method.Interface can contain only abstract( non implemented) methods.

2) The abstract class can have public,private,protect or default variables and also constants. In interface the variable is by default public final. In nutshell the interface doesnt have any variables it only has constants.

3) A class can extend only one abstract class but a class can implement multiple interfaces.

4. Does Java has multiple inheritance?


Java doesnt support multiple inheritance but it provide a way through which it can enact it.

5. What are the different types of references in java?

types of references—soft, weak, and phantom references.
A SoftReference can be used to implement a cache. An object that is not reachable by a strong reference (that is, not strongly reachable), but is referenced by a soft reference is called softly reachable. A softly reachable object may be garbage collected at the discretion of the garbage collector. This generally means that softly reachable objects will only be garbage collected when free memory is low, but again, it is at the discretion of the garbage collector. Semantically, a soft reference means "keep this object unless the memory is needed."
A WeakReference is used to implement weak maps. An object that is not strongly or softly reachable, but is referenced by a weak reference is called weakly reachable. A weakly reachable object will be garbage collected during the next collection cycle. This behavior is used in the class java.util.WeakHashMap. A weak map allows the programmer to put key/value pairs in the map and not worry about the objects taking up memory when the key is no longer reachable anywhere else. Another possible application of weak references is the string intern pool. Semantically, a weak reference means "get rid of this object when nothing else references it."

A PhantomReference is used to reference objects that have been marked for garbage collection and have been finalized, but have not yet been reclaimed. An object that is not strongly, softly or weakly reachable, but is referenced by a phantom reference is called phantom reachable. This allows for more flexible cleanup than is possible with the finalization mechanism alone. Semantically, a phantom reference means "this object is no longer needed and has been finalized in preparation for being collected."

6. Java supports pass by value or pass by reference?
Java supports only pass by value. The arguments passed as a parameter to a method is mainly primitive data types or objects. For the data type the actual value is passed.
Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references.Java copies and passes the reference by value, not the object.

7. What is memory leak?
A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected.

8. Can an abstract class have a static method?

Yes an abstract class have a static method and it can be accessed by any other class(even not a concrete class).

9. Can an abstract class have a constructor?


Yes an abstract class have a default and parameterized constructors.

10. Why static methods cannot access non static variables or methods?

A static method cannot access non static variables or methods because static methods doesnt need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be intialized since the object is not created and this could result in an error.

11. What is the difference between final, finally and finalize() in Java?

final - constant declaration. A final variable act as constant, a final class is immutable and a final method cannot be ovrriden.

finally - handles exception. The finally block is optional and provides a mechanism to clean up regardless of what happens within the try block (except System.exit(0) call). Use the finally block to close files or to release other system resources like database connections, statements etc.

finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.


2. Introduction to Object Oriented Analysis Concept and basic Designing class (UML)

3. Developing and Testing Java Program

Discussion Topic 12 ( ADO.Net )

A DataSet is a collection of DataTable
A DataTable is a collection of DataRow
A DataRow is a collection of data elements
A TableAdapter is a device that uses a hidden DataAdapter (which is based on a hidden DataReader) to fill a DataTable with DataRows from a data source such as a database, and send updates (changes to the DataRows) back to the data source TableAdapters do not fill DataSets. DataSets do not hold data directly.

In .NET 2.0 controls are usually bound to data through a BindingSource A BindingSource sits on top of a block of data (such as a DataTable) and maintains knowledge of position; changing the .Position of a BindingSource changes what row of data all controls that bind through that BindingSource, are looking at BindingSources can also filter and sort the rows they present, independently of the DataTable or DataRelation upon which they are based A DataRelation acts as a filter device to only show records from a child data source (usually a DataTable) whose foreign key matches the primary key of the current row of the relation's parent table. Because the concept of "Current Parent Row" is needed for a DataRelation to work, they are usually found within BindingSources; the Parent BindingSource exposes all the DataRelations whose parent table is the the table upon which the BindingSource sits.

A BindingNavigator is a device that alters the position of a BindingSource through use of its [|<], [<], [>] and [>|] buttons, and can be used to invoke common actions on the BindingSource, such as .AddNew(), .Delete() and .Update()

A DataReader is the only way data comes out of a database. Anything else is an additional device to fill the need for some functionality.

Discussion Topic 11 ( MVC Model )

Model
-----
This is where your data is stored. The DataTable is the Model.

View
----
This is the component that shows the data found in the Model. The DataGridView is the View here. When used in data-bound mode, you dont copy from the datatable into the grid, the grid just shows what it finds in the Model. Change the Model, the View changes what it shows

Controller
----------
I mentioned changing the Model; that's what this does. Most of the time, the thing that changes the Model is the same thing that Views the Model. It doesnt really make sense (usually) to show some text from your database, in a Label, but then have a TextBox to edit it.. because the TextBox is capable of being the View.
But suppose you have a button called [Clear] that removes all the information from the Model. Pressing this button clears the Model and suddenly the View (grid) updates to show nothing. This is an example of a Controller; it affects the Model, but it doesnt behave as a View of anything in the Model.


MVC is a good OO concept. Put your data here, show it using that component there. "There" and "here" shouldnt be the same thing. Dont store your data into your View components.

<< Question and Answer Section >>

Hi,

If you would like to ask something, please leave the question in the comment box and we will answer in next day. We always waiting for your IT related questions? If you want to hide your question, we will just drop the answer to your email.

Best Regards,
Admin (Unique Training Hub)