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.
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.
-----
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)
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)
Java Training Session 1 ( Introduction )
1. What is Java?
Ans : Java is a computer programming language. It is allow to write computer instructions using English based commands. It’s known as a “high-level” language because it can be read and written easily by humans. Java has a set of rules and regulations that determine how the instructions are written. These are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute.
2. How can we execute java program?
Ans: Run Command Prompt (found under All Programs/Accessories in the Start menu). Type
C:\> cd \myfolder
C:\myfolder> dir
C:\myfolder> set path=%path%;C:\Program Files\Java\jdk1.6.0\bin
C:\myfolder> javac filename.java
This runs javac.exe, the compiler. You should see nothing but the next system prompt...
C:\myfolder> dir
javac has created the filename.class file. You should see filename.java and filename.class among the files.
C:\myfolder> java filename
This runs the Java interpreter. You should see the program output:
Output
3. Advantages of Java Programming
Ans:
Java is simple:
--------------
Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. The reason that why Java is much simpler than C++ is because Java uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage.
Java is object-oriented:
------------------------
Java is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.
Java is platform-independent:
----------------------------
One of the most significant advantages of Java is its ability to move easily from one computer system to another.
The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels.
Java is distributed:
-------------------
Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.
Writing network programs in Java is like sending and receiving data to and from a file. For example, the diagram below shows three programs running on three different systems, communicating with each other to perform a joint task.
Java is interpreted:
--------------------
An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called bytecode.
The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform.
Java is secure:
---------------
Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind.
Java is robust:
--------------
Robust means reliable and no programming language can really assure reliability. Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages.
Java is multithreaded:
---------------------
Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other languages, operating system-specific procedures have to be called in order to enable multithreading. Multithreading is a necessity in visual and network programming.
4. Type of java programming
Ans:
SE (Standard Edition)
EE (Enterprise Edition)
ME (Micro Edition)
5. What is java Byte Code?
Ans: Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. In fact, Sun Microsystems, the original creators of the Java programming language, the Java virtual machine and other components of the Java Runtime Environment (JRE), have set aside 3 values to be permanently unimplemented.
6. What is java Applet?
Ans: A Java applet is an applet that delivered to the users in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM). Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.
7. Where can I get Java and Java IDE?
Ans: http://www.sun.com/download/index.jsp
Ans : Java is a computer programming language. It is allow to write computer instructions using English based commands. It’s known as a “high-level” language because it can be read and written easily by humans. Java has a set of rules and regulations that determine how the instructions are written. These are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute.
2. How can we execute java program?
Ans: Run Command Prompt (found under All Programs/Accessories in the Start menu). Type
C:\> cd \myfolder
C:\myfolder> dir
C:\myfolder> set path=%path%;C:\Program Files\Java\jdk1.6.0\bin
C:\myfolder> javac filename.java
This runs javac.exe, the compiler. You should see nothing but the next system prompt...
C:\myfolder> dir
javac has created the filename.class file. You should see filename.java and filename.class among the files.
C:\myfolder> java filename
This runs the Java interpreter. You should see the program output:
Output
3. Advantages of Java Programming
Ans:
Java is simple:
--------------
Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. The reason that why Java is much simpler than C++ is because Java uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage.
Java is object-oriented:
------------------------
Java is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code.
Java is platform-independent:
----------------------------
One of the most significant advantages of Java is its ability to move easily from one computer system to another.
The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels.
Java is distributed:
-------------------
Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.
Writing network programs in Java is like sending and receiving data to and from a file. For example, the diagram below shows three programs running on three different systems, communicating with each other to perform a joint task.
Java is interpreted:
--------------------
An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called bytecode.
The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform.
Java is secure:
---------------
Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind.
Java is robust:
--------------
Robust means reliable and no programming language can really assure reliability. Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages.
Java is multithreaded:
---------------------
Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other languages, operating system-specific procedures have to be called in order to enable multithreading. Multithreading is a necessity in visual and network programming.
4. Type of java programming
Ans:
SE (Standard Edition)
EE (Enterprise Edition)
ME (Micro Edition)
5. What is java Byte Code?
Ans: Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. In fact, Sun Microsystems, the original creators of the Java programming language, the Java virtual machine and other components of the Java Runtime Environment (JRE), have set aside 3 values to be permanently unimplemented.
6. What is java Applet?
Ans: A Java applet is an applet that delivered to the users in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM). Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.
7. Where can I get Java and Java IDE?
Ans: http://www.sun.com/download/index.jsp
Subscribe to:
Posts (Atom)
