Showing posts with label Core Java FAQS. Show all posts
Showing posts with label Core Java FAQS. Show all posts

Sunday, January 23, 2011

FAQS on threads concept on JAVA


         Answer: Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction.
The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications. 

Question: What is a thread?

Answer: In Java the Thread class represents a single independent path of execution in a Java Virtual Machine. When you run a Java program it implicitly starts a single thread of execution. The Thread class enables programmers to create additional threads and set them running. A number of threads may run in parallel, but only one is actively executed at a given moment.
The Java runtime system uses fairly complex thread scheduling mechanisms to coordinate the execution of threads, but this does not require privileged knowledge or detail level intervention by programmers. Programmers can manage the high level creation, initiation and distribution of tasks amongst threads through simple Application Programming Interface (API) methods.
The example below shows the simplest approach to thread creation and task execution; construct a new Thread with a Runnable argument and start it.


Question: How do Java threads make the environment asynchronous?

Answer: The thread mechanism in Java begins with the main entry point thread the runtime environment creates to start a Java program. When you use that initial thread create secondary threads, each one runs independently of the other. The Java virtual machine manages the execution of the threads so they behave as if they all run at the same time, in fact each thread briefly takes turns at execution.
In its simplest form there may be no communication or synchronization between multiple threads in a Java program and they each run to completion independently of each other. In this respect Java threads are fundamentally asynchronous, there is no master clock that governs when threads will run and when they synchronize variables to “catch-up” with each other.
        It is often necessary and more useful if threads do check ready states before progressing, synchronize read and write access to shared variables and call-back to each other when their work is done. This is where the synchronized keyword and the various sleep(), wait() and notify() methods are used to more closely schedule the interaction between asynchronous threads.

Wednesday, January 19, 2011

What is the difference between thread amd process?

Process:
                     A process is an instance of a computer program that is executed sequentially. It is a collection of instructions which are executed simultaneously at the run time. Thus several processes may be associated with the same program. For example, to check the spelling is a single process in the Word Processor program and you can also use other processes like printing, formatting, drawing, etc. associated with this program.

Thread:
                A thread is a lightweight process which exist within a program and executed to perform a special task. Several threads of execution may be associated with a single process. Thus a process that has only one thread is referred to as a single-threaded process, while a process with multiple threads is referred to as a multi-threaded process.

                  In Java Programming language,  thread is a sequential path of code execution within a program. Each thread has its own local variables, program counter and lifetime. In single threaded runtime environment, operations are executes sequentially i.e. next operation can execute only when the previous one is complete. It exists in a common memory space and can share both data and code of a program. Threading concept is very important in Java through which we can increase the speed of any application. You can see diagram shown below in which a thread is executed along with its several operations with in a single process.

Saturday, January 15, 2011

Difference between an interface and an abstract class?

 An abstract class may contain code in method bodies, which is not allowed in an interface.
With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

Wednesday, November 10, 2010

String, StringBuffer & stringBuilder in JAVA

1] Differnces between the above three is:
   String is immutable
   StringBuffer & StringBuilders are mutable.

2] Differnces between the StringBuffer& stringBuiler:
   StringBuffer is synchronized.
   StringBuilder is unsynchronized.

3] On which scenario we have to use the above three?
   String: If the text is not going to change use a string Class because a String object is    immutable.
   StringBuffer: If the text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
   StringBuilder : If the text can change and will only be accessed from a single thread, then use StringBuilder because StringBuilder is unsynchronized.

Sunday, July 4, 2010

Setting the class path to JAVA on Windows XP and 2000

Setting the class path to JAVA on Windows XP &2000

• Open the System Properties window (by right-clicking on My Computer and selecting Properties, or through the Control Panel - Start -> Control Panel -> System).
• On the Advanced tab, click the Environment Variables button.
• In user variables, select the new tab and give the variable name as JAVA_HOME and value as path of the java folder (Example: C:\Java)
• In System variables we have to set the CLASSPATH; if it is already exists then add the java path up o lib folder (Example C:\Java\lib)

Sunday, February 24, 2008

CORE JAVA FAQS.............

Java is an object-oriented programming language developed by Sun Microsystems. Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level, which means that Java programs (applets and applications) can run on any machine that has the Java virtual machine installed.

Java is Platform independent, Platform independence means, the ability of a program to move easily from one computer system to another-is one of the most significant advantages that Java has over other programming languages, particularly if your software needs to run on many different platforms.

The Java language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devices. Java's rapidly growing popularity is due to the Web. But Java's inherent power does not come from the fact that it is a Web programming language. The talented software engineers at Sun, in bringing Java to the Web, have elegantly solved a much broader and more significant problem-how to develop network-capable windowing software that will run on almost any 32-bit computer and operating system.

A software developer writes programs in the Java language that use predefined software packages of the Java API. The developer compiles his or her programs using the Java compiler. This results in what is known as compiled bytecode. Bytecode is in a form that can be executed on the Java virtual machine, the core of the Java runtime system. You can think of the virtual machine as a microprocessor that is implemented in software and runs using the capabilities provided by your operating system and computer hardware. Since the Java virtual machine is not a real microprocessor, the Java bytecode is interpreted, rather than executed directly in the native machine instructions of the host computer.





Java is exceptionally well suited to distributed networking applications because of its built-in networking support and the runtime system's capability to dynamically load Java bytecode across the network. Java also provides the capability to dynamically utilize new content and protocol handling software. The HotJava browser, written in Java, is an excellent example of Java's distributed networking capabilities.

The Java API provides full support of multithreaded programming. Multithreaded programs can be developed in a single, consistent manner, independent of the vagaries of the host operating system interface.

Java classes and objects directly support the object-oriented concepts of encapsulation, inheritance, messages and methods, and data hiding. Java interfaces provide support for multiple inheritance and polymorphism. The Java language retains all the benefits of object-oriented programming without the performance impacts associated with pure object languages, such as Smalltalk.

The Java API provides extensive support of windowing and graphical user interface development without the complexities associated with maintaining multiple window class libraries. Several visual programming tools have been developed for Java.