Java Programming
1.
What
is Volatile variable in java?
Volatile keyword in
Java is used as an indicator to Java compiler and
Thread that do not cache value of this variable and always read
it from main
memory.
In Multi-threaded Programming, when multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever threads are updating the values, it is updated to the main memory. So, other threads can access the updated value.
In Multi-threaded Programming, when multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever threads are updating the values, it is updated to the main memory. So, other threads can access the updated value.
2.
What
is Deadlock? How to avoid Deadlock in java?
Deadlock is a situation where a thread
is waiting for an object lock that another thread holds, and this second thread
is waiting for an object lock that the first thread holds.
3.
What
is Transient Variable in java?
Transient variables cannot be serialized. The fields marked as a Transient in a
serializable object will not be transmitted in the byte stream. An Example
would be a file handle, a database connection, a system thread etc. such
objects are only meaningful locally. So, they should be marked as transient in
a serializable class.
4.
What
is Java Serial Version ID?
Say you create a “Car” class, instance it, and write
it out to an object stream. The flattened car object sits in the file system
for some time. Meanwhile, if the “car” class is modified by adding a new field.
Later, when you try to deserialize the flattened “car” object, you get the
java.io.InvalidClassException-because all serializable classes are
automatically given a Unique Identifier. This exception is thrown when the
identifier of the class is not equal to the identifier of the flattened object.
If you really think about it, the exception is thrown because of addition of
the new field. You can avoid this exception being thrown by controlling the
versioning yourself by declaring an explicit serialVersionUID. There is a small
performance benefit in explicitly declaring your serialVersionUID.
5.
What
is URL Rewriting?
URL Rewriting is another way to support anonymous
session tracking. With URL Rewriting, every local URL the user might click on
is dynamically modified, or rewritten, to include extra information. The extra
information can be in the form of extra path information, added parameters, or
some custom, server-specific URL change. Due to limited space available in
rewriting a URL, the extra information is usually limited to a unique session
ID. For example, the following URL’s have been rewritten to pass the Session ID
123,
6.
Are
Servlets Thread Safe? How to achieve thread safety in Servlets?
Servlets are not
thread-safe by itself. You can make it thread-safe by making the service method
synchronized, but this reduces the performance, as the servlet cannot process
the request from other user until the service method is finished. The best way
to keep your unsafe code thread-safe is keep the few lines of code inside
synchronized block.
7.
How
can I enable session tracking for JSP pages if the browser has disabled
cookies?
We
know that session tracking uses cookies by default to associate a session
identifier with a unique user. If the browser does not support cookies or if
cookies are disabled. You can still enable session tracking using URL
Rewriting. URL rewriting essentially include the session ID within the link
itself as name/value pair. However, for this effective, you need to embed the
session ID for each and every link that is part of your Servlet response.
Adding the session ID to a link is greatly simplified by means of a couple of
methods: response.encodeURL() associates a session ID with a given URL and if
you are using redirection, response.encodeRedirectURL() can be used by giving
the redirected URL as input. Both encodeURL() and encodeRedirectURL() first
determine whether supported by the browser. If so, the input URL is returned
unchanged since the session ID will be persisted as cookie.
8.
What is the Difference between byte stream and Character streams?
To generate User reports, send attachments through mails and spill
out data files from Java programs.
Byte stream: Byte
stream classes have been designed to provide functional features for creating
and manipulating streams and files for reading and writing in a form of bytes.
Since the streams are unidirectional, they can transmit bytes in only one
direction and, therefore, Java provides two kinds of byte stream classes:
Output stream classes
Input stream classes
- Performing
InputStream operations or OutputStream operations means generally having a
loop that reads the input stream and writes the output stream one byte at
a time.
- You can use
buffered I/O streams for an overhead reduction (overhead generated by each
such request often triggers disk access, network activity, or some other
operation that is relatively expensive).
Character streams: Character
streams work with the characters rather than the byte. In Java, characters are
stored by following the Unicode (allows a unique number for every character)
conventions. Like ByteStreams, there are two kinds of character stream classes
Reader stream classes
Writer stream classes
9.
What
is Autoboxing?
Autoboxing
is the process by which a primitive type is automatically encapsulated (boxed) into
its equivalent type wrapper whenever an object of that type is needed. There is
no need to explicitly construct an object. Auto unboxing is the process by
which the value of a boxed object is automatically extracted (unboxed) from a
type wrapper when its value is needed. There is no need to call a method such
as intValue( ) or doubleValue( ).
10.
What's
the difference between normal methods and constructors?
Normal
method can be called anywhere and any more of times but constructors can be
called only when an object is created. Normal method can have any name but
constructor name should be same as that of a class name. Normal method should
have a return type but constructor should not a have a return type.
11.
What
is the difference between the ServletConfig and ServletContext interface?
• ServletConfig
is implemented by the servlet container to initialize a single servlet using
init(). That is, you can pass initialization parameters to the servlet using
the web.xml deployment descriptor. For understanding, this is similar to a
constructor in a java class.
• ServletContext
is implemented by the servlet container for all servlet to
communicate with its servlet container, for example, to get the MIME type of a
file, to get dispatch requests, or to write to a log file. That is to get
detail about its execution environment. It is applicable only within a single
Java Virtual Machine. If a web applicationa is distributed between multiple JVM
this will not work. For understanding, this is like a application global
variable mechanism for a single web application deployed in only one JVM.
• The
ServletContext object is contained within the ServletConfig object. That is,
the ServletContext can be accessed using the ServletConfig object within a
servlet. You can specify param-value pairs for ServletContext object in
<context-param> tags in web.xml file.
• The
scope of the ContextInitParameter is within a web containter, whereas scope of the ServletInitParameter is for a
specific servlet or jsp
• The
Servlet code of the ServletContext is getServletContext(). Whereas for
ServletConfig is getServletConfig()
• Deployment
descriptor for ServletContext is within
the <Web-app> element byt not
within a specific <Servlet>element, whereas for ServletConfig
within the <Servlet> element for each specific servlet.
12.
What
is the difference between forward() and sendRedirect() method?
A forward()
performs forward internally by the servlet. Whereas A redirect is
performed in two steps process, where the web application instructs the browser
to fetch a second URL, which differ from the original.
13.
Why
session tracking is need in web application? How to perform Session tracking.
HTTP protocol is a “stateless” protocol. Each time a
client retrieves a web page it establish a separate connection to the web
server, and the server does bit automatically maintain contextual information about
a client. Even with servers that support persistent HTTP connections and keep a
socket open for multiple client requests that occur close together in time,
there is no built-in support for maintaining contextual information. This lack
of context causes a number of difficulties. For example, when clients at an
on-line shopping add an item to their shopping carts, the server doesn’t know
the things already in them. Similarly, when clients decide to proceed to
checkout, the server cannot determine the previously created shopping carts.
There
are three solutions to solve problem:
• Cookies
• URL-rewriting
• Hidden form fields
14.
How
to declare a method within a JSP page?
You can declare methods to use within your JSP pag
as declarations. The methods can then be invoked within any other methods you
declare, or within JSP scriptlets and expressions. We cannot access any JSP
implicit objects like request, response, out, session directly from within JSP
methods. However, we can able to pass any of the implicit JSP variables as
parameters to the methods you declare.
For
example:
<%!
String displaydata(String name){
return
name;
}
%>
<%=displaydata(“Hello”);%>
15.
What
is the difference between JSP include directive and JSP action include tag?
When a JSP include directive is used the included
file’s code is added into the added JSP page at the time of page translation,
that is this happens before the JSP page is translated into a Servlet. Whereas
in action include tag Includes a file at the time the page is requested.
No comments:
Post a Comment