Category: JAVA


Design Patterns in Java

Whole File:- Java Design Pattern

Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

Design Pattern Overview

Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

What is Gang of Four (GOF)?

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson und John Vlissides published a book titled Design Patterns – Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.

These authors are collectively known as Gang of Four (GOF). According to these authors design patterns are primarily based on the following principles of object orientated design.

  • Program to an interface not an implementation
  • Favor object composition over inheritance

Usage of Design Pattern

Design Patterns have two main usages in software development.

Common platform for developers

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.

Best Practices

Design patterns have been evolved over a long period of time and they provide best solutions to certain problems faced during software development. Learning these patterns helps un-experienced developers to learn software design in an easy and faster way.

Types of Design Pattern

As per the design pattern reference book Design Patterns – Elements of Reusable Object-Oriented Software , there are 23 design patterns. These patterns can be classified in three categories: Creational, Structural and behavioral patterns. We’ll also discuss another category of design patterns: J2EE design patterns.

S.N.

Pattern & Description

1 Creational Patterns                             
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.
2 Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
3 Behavioral Patterns
These design patterns are specifically concerned with communication between objects.
4 J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center.

****MANY MORE******

Advertisements

java interview with sbdsisaikat

The Whole File:- java interview with sbdsisaikat

Dear readers, these Java Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Java Programming Language. As per my experience good interviewers hardly planned to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer:

Q: What are the supported platform by Java Programming Language?

A: Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS etc.

Q: List any five features of Java?

A: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

Q: Why is Java Architectural Neutral?

A: It’s compiler generates an architecture-neutral object file format which makes the compiled code to be executable on many processors, with the presence Java runtime system.

Q: How Java enabled High Performance?

A: Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor.

Q: Why Java is considered dynamic?

A: It is designed to adapt to an evolving environment. Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to objects on run-time.

Q: What is Java Virtual Machine and how it is considered in context of Java’s platform independent feature?

A: When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run.

Q: List two Java IDE’s?

A: Netbeans, Eclipse etc

Q: List some Java keywords(unlike C, C++ keywords)?

A: Some Java keywords are import, super, finally etc

Q: What do you mean by Object?

A: Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication.

Q: Define class?

A: A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

Q: What kind of variables a class can consist of?

A: A class consist of Local variable, instance variables and class variables.

****many more****

The Whole File:-Java Programming Examples

Find the best practical and ready to use Java Programming Examples.
Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
These examples would be very useful for your projects and learning.

  • Example – Home
  • Example – Environment
  • Example – Strings
  • Example – Arrays
  • Example – Date & Time
  • Example – Methods
  • Example – Files
  • Example – Directories
  • Example – Exceptions
  • Example – Data Structure
  • Example – Collections
  • Example – Networking
  • Example – Threading
  • Example – Applets
  • Example – Simple GUI
  • Example – JDBC
  • Example – Regular Exp

The Whole File:- J2EE Interview Questions and Answers

What is J2EE?
J2EE is an environment for developing and deploying enterprise applications. The J2EE platform consists of a set of services, application programming interfaces (APIs), and protocols that provide the functionality for developing multitier, web-based applications.

What is the J2EE module?
A J2EE module consists of one or more J2EE components for the same container type and one component deployment descriptor of that type.

What are the components of J2EE application?
A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and communicates with other components. The J2EE specification defines the following J2EE components:
Application clients and applets are client components.
Java Servlet and JavaServer PagesTM (JSPTM) technology components are web components.
Enterprise JavaBeansTM (EJBTM) components (enterprise beans) are business components.
Resource adapter components provided by EIS and tool vendors.

What are the four types of J2EE modules?
1. Application client module
2. Web module
3. Enterprise JavaBeans module
4. Resource adapter module

What does application client module contain?
The application client module contains:
–class files,
–an application client deployment descriptor.
Application client modules are packaged as JAR files with a .jar extension.

What does web module contain?
The web module contains:
–JSP files,
–class files for servlets,
–GIF and HTML files, and
–a Web deployment descriptor.
Web modules are packaged as JAR files with a .war (Web ARchive) extension.

What are the differences between Ear, Jar and War files? Under what circumstances should we use each one?

There are no structural differences between the files; they are all archived using zip-jar compression. However, they are intended for different purposes.
–Jar files (files with a .jar extension) are intended to hold generic libraries of Java classes, resources, auxiliary files, etc.
–War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.
–Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web applications.
Each type of file (.jar, .war, .ear) is processed uniquely by application servers, servlet containers, EJB containers, etc.

What is the difference between Session bean and Entity bean ?
The Session bean and Entity bean are two main parts of EJB container.
Session Bean
–represents a workflow on behalf of a client
–one-to-one logical mapping to a client.
–created and destroyed by a client
–not permanent objects
–lives its EJB container(generally) does not survive system shut down
–two types: stateless and stateful beans
Entity Bean
–represents persistent data and behavior of this data
–can be shared among multiple clients
–persists across multiple invocations
–findable permanent objects
–outlives its EJB container, survives system shutdown
–two types: container managed persistence(CMP) and bean managed persistence(BMP)

What is “applet”  ?
A J2EE component that typically executes in a Web browser but can execute in a variety of other applications or devices that support the applet programming model.

****many more****

The Whole File:- Servlet Interview Questions and Answers

What is Servlet?
A servlet is a Java technology-based Web component, managed by a container called servlet container or servlet engine, that generates dynamic content and interacts with web clients via a request\/response paradigm.

Why is Servlet so popular?
Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web server.

What is servlet container?
The servlet container is a part of a Web server or application server that provides the network services over which requests and responses are sent, decodes MIME-based requests, and formats MIME-based responses. A servlet container also contains and manages servlets through their lifecycle.

When a client request is sent to the servlet container, how does the container choose which servlet to invoke?
The servlet container determines which servlet to invoke based on the configuration of its servlets, and calls it with objects representing the request and response.

If a servlet is not properly initialized, what exception may be thrown?
During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.

Given the request path below, which are context path, servlet path and path info?
/bookstore/education/index.html

context path: /bookstore
servlet path: /education
path info: /index.html

What is filter? Can filter be used as request or response?
A filter is a reusable piece of code that can transform the content of HTTP requests,responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.

When using servlets to build the HTML, you build a DOCTYPE line, why do you do that?

I know all major browsers ignore it even though the HTML 3.2 and 4.0 specifications require it. But building a DOCTYPE line tells HTML validators which version of HTML you are using so they know which specification to check your document against. These validators are valuable debugging services, helping you catch HTML syntax errors.

What is new in ServletRequest interface ? (Servlet 2.4)
The following methods have been added to ServletRequest 2.4 version:
public int getRemotePort()
public java.lang.String getLocalName()
public java.lang.String getLocalAddr()
public int getLocalPort()

Request parameter How to find whether a parameter exists in the request object?
1.boolean hasFoo = !(request.getParameter(“foo”) == null || request.getParameter(“foo”).equals(“”));
2. boolean hasParameter = request.getParameterMap().contains(theParameter);
(which works in Servlet 2.3+)

How can I send user authentication information while making URL Connection?
You’ll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

Can we use the constructor, instead of init(), to initialize servlet?
Yes , of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

****many more****

The Whole File:- JSP Interview Questions and Answers

What is a JSP and what is it used for?
Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the background to generate a Servlet from the JSP page.

What is difference between custom JSP tags and beans?
Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:
1. the tag handler class that defines the tag\’s behavior
2. the tag library descriptor file that maps the XML element names to the tag implementations
3. the JSP file that uses the tag library
When the first two components are done, you can use the tag by using taglib directive:
<%@ taglib uri=”xxx.tld” prefix=”…” %>
Then you are ready to use the tags you defined. Let’s say the tag prefix is test:
MyJSPTag or
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags to declare a bean and use to set value of the bean class and use to get value of the bean class.

<%=identifier.getclassField() %>
Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:
Custom tags can manipulate JSP content; beans cannot.
Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans.
Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.
Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.

What are the two kinds of comments in JSP and what’s the difference between them ?
<%– JSP Comment –%>
<!– HTML Comment –>

What is JSP technology?

Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.

What is JSP page?
A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.

What are the implicit objects?
Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:
–request
–response
–pageContext
–session
–application
–out
–config
–page
–exception

How many JSP scripting elements and what are they?
There are three scripting language elements:
–declarations
–scriptlets
–expressions

****many more****

The Whole File:- Java Interview Questions and Answers

What is Collection API ?
The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

Is Iterator a Class or Interface? What is its use?
Answer: Iterator is an interface which is used to step through the elements of a Collection.

What is similarities/difference between an Abstract class and Interface?
Differences are as follows:
Interfaces provide a form of multiple inheritance. A class can extend only one other class. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
Similarities:

Neither Abstract classes or Interface can be instantiated.

Java Interview Questions – How to define an Abstract class?
A class containing abstract method is called Abstract class. An Abstract class can’t be instantiated.
Example of Abstract class:
abstract class testAbstractClass {
protected String myString;
public String getMyString() {
return myString;
}
public abstract string anyAbstractFunction();
}

How to define an Interface in Java ?
In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Emaple of Interface:

public interface sampleInterface {
public void functionOne();

public long CONSTANT_ONE = 1000;
}

If a class is located in a package, what do you need to change in the OS environment to be able to use it?

You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let’s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you’d need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:\>java com.xyz.hr.Employee

How many methods in the Serializable interface?
There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.

How many methods in the Externalizable interface?
There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal() and writeExternal().

*****many more****

The Whole File: Advanced Java interview

1. What is a transient variable?

A transient variable is a variable that may not be serialized.

2. Which containers use a border Layout as their default layout?

The Window, Frame and Dialog classes use a border layout as their default layout.

3. Why do threads block on I/O?

Threads block on I/O (that is enters the waiting state) so that other threads may execute while the I/O Operation is performed.

4. How are Observer and Observable used?

Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.

5. What is synchronization and why is it important?

With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.

6. Can a lock be acquired on a class?

Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object..

7. What’s new with the stop(), suspend() and resume() methods in JDK 1.2?

The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

8. Is null a keyword?

The null is not a keyword.

9. What is the preferred size of a component?

The preferred size of a component is the minimum component size that will allow the component to display normally.

10. What method is used to specify a container’s layout?

The setLayout() method is used to specify a container’s layout.

11. Which containers use a FlowLayout as their default layout?

The Panel and Applet classes use the FlowLayout as their default layout.

12. What state does a thread enter when it terminates its processing?

When a thread terminates its processing, it enters the dead state.

13. What is the Collections API?

The Collections API is a set of classes and interfaces that support operations on collections of objects.

****many more****

The Best JAVA Tutorial

The Java Tutorials are practical guides for programmers who want to use the Java programming language to create applications. They include hundreds of complete, working examples, and dozens of lessons. Groups of related lessons are organized into “trails”.

The Java Tutorials primarily describe features in Java SE 7. For best results, download JDK 7.

Trails Covering the Basics

These trails are available in book form as The Java Tutorial, Fifth Edition. To buy this book, refer to the box to the right.

  • Getting Started — An introduction to Java technology and lessons on installing Java development software and using it to create a simple program.
  • Learning the Java Language — Lessons describing the essential concepts and features of the Java Programming Language.
  • Essential Java Classes — Lessons on exceptions, basic input/output, concurrency, regular expressions, and the platform environment.
  • Collections — Lessons on using and extending the Java Collections Framework.
  • Deployment — How to package applications and applets using JAR files, and deploy them using Java Web Start and Java Plug-in.
  • Preparation for Java Programming Language Certification — List of available training and tutorial resources.

The Whole Tutorial FILE :- Sun java

.

Creating Graphical User Interfaces

  • Creating a GUI with Swing — A comprehensive introduction to GUI creation on the Java platform.
  • Creating a JavaFX GUI — A collection of JavaFX tutorials.

The Whole Tutorial FILE :- **will be updated soon**

.

Specialized Trails and Lessons

These trails and lessons are only available as web pages.

  • Custom Networking — An introduction to the Java platform’s powerful networking features.
  • The Extension Mechanism — How to make custom APIs available to all applications running on the Java platform.
  • Full-Screen Exclusive Mode API — How to write applications that more fully utilize the user’s graphics hardware.
  • Generics — An enhancement to the type system that supports operations on objects of various types while providing compile-time type safety. Note that this lesson is for advanced users. The Java Language trail contains a Generics lesson that is suitable for beginners.
  • Internationalization — An introduction to designing software so that it can be easily be adapted (localized) to various languages and regions.
  • JavaBeans — The Java platform’s component technology.
  • JDBC Database Access — Introduces an API for connectivity between the Java applications and a wide range of databases and a data sources.
  • JMX— Java Management Extensions provides a standard way of managing resources such as applications, devices, and services.
  • JNDI— Java Naming and Directory Interface enables accessing the Naming and Directory Service such as DNS and LDAP.
  • JAXP — Introduces the Java API for XML Processing (JAXP) 1.4 technology.
  • JAXB — Introduces the Java architecture for XML Binding (JAXB) technology.
  • RMI — The Remote Method Invocation API allows an object to invoke methods of an object running on another Java Virtual Machine.
  • Reflection — An API that represents (“reflects”) the classes, interfaces, and objects in the current Java Virtual Machine.
  • Security — Java platform features that help protect applications from malicious software.
  • Sound — An API for playing sound data from applications.
  • 2D Graphics — How to display and print 2D graphics in applications.
  • Sockets Direct Protocol — How to enable the Sockets Direct Protocol to take advantage of InfiniBand.

The Whole Tutorial FILE :- **will be updated soon**

****** The tutorial is provided above is written in oracle sun java site,, i have just put it in a file for offline reading ,, the original document is written by them only.

click the link to download the whole file:

JAVA interview Q & A