- Using new operator - the most commonly used way by far. 'new' is an operator and the only operand it requires is a constructor which will be used for initializting the newly created instance. 'new' operator allocates the memory space and initializes the fields with their default values. Then it executes the code inside the specified constrcutor which normally re-writes the default values of some (or all) fields with the particular values mentioned in the constructor definition.
- Using clone() method - Cloning (Shallow or Deep) makes a new copy of the specified object. It doesn't call the 'new' operator internally. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data. Remember that even if we override the clone() method either to implement Deep Cloning or to make the clone() method as 'public' but then also we keep the line super.clone() inside the overriden definition which ultimately calls Object.clone() method only. You might wonder about what the 'new' operator actually translates into and how this approach is different from that. Okay... we all know that 'new' does three tasks - allocating the memory, initializing the fields with default values, and calling the specified constructor. Now the first task is same in both the approaches and there would probably be the same native allocator being used in both the cases. But in case of Cloning, the allocated memory is not initialized with default values and also no constructor is called in this case. Only a datacopy instruction will be executed which copies the data from the original object to the cloned object. Read more about Cloing in this article - Cloning in Java >>
Wednesday, June 5, 2013
How many ways of creating objects in Java?
Well...
there aren't many different ways I suppose. But from an application
programmer perspective here are the different possible ways of creating
objects in Java:-
Java Object oriented programming concepts with real time examples
Java Object oriented programming concepts with real time examples
OOPS Concepts are mainly 4
1.Abstraction
2.Encapsulation
3.Inheritance
4.Polymorphism
Abstraction:-Hiding non-essential features and showing the
essential features
(or)
Hiding unnecessary data from the users details,is called
abstraction.
Real Time example:1.TV Remote Button
in that number format and power buttons and other buttons
there.just we are seeing the buttons,we don't see the
button circuits.i.e buttons circuits and wires all are
hidden.so i think its good example.
Encapsulation:It is a process of binding or wrapping the data and the
codes that operates on the data into a single entity. This
keeps the data safe from outside interface and misuse. One
way to think about encapsulation is as a protective wrapper
that prevents code and data from being arbitrarily accessed
by other code defined outside the wrapper.
Real Time Example:
1.Ink is the important component in pen but it is hiding
by some other material
2.Medical Tablet
i.e one drug is stored in bottom layer and another drug is
stored in Upper layer these two layers are combined in
single Tablet.
Inheritance:The new classes, known as derived classes, take over (or
inherit) attribute and behavior of the pre-existing classes,
which are referred to as base classes (or Parent classes).
It is intended to help reuse existing code with little or no
modification.
Real Time Example:
1. Father and Son Relationship
Polymorphism:Single Form behaving differently in different Situations. A single
function or single operator has different characters in different places.
Real Time Example:
1.A girl plays a role of daughter at home and a manager at
office.
2. Person
Person in Home act is husband/son,
in Office acts Employer.
in Public Good Citizen.
<<<<<<<<<<<<<<<<------------------------->>>>>>>>>>>>>
OOPS Concepts with realtime examples
Abstraction helps to hide the non essential features from the user and makes the application user friendly.
Eg. Take your mobile phone. You are having buttons and know the purpose but no need to understand how it prints number on the screen when it is pressed.
2. Encapsulation
It bundles the code into a single unit which makes the code very easy to handle.
Eg.
Take computer keyboard. All the buttons have been enclosed.
3. Inheritance
It helps to inherit the superclass properties to subclasses.
Eg. Father - Son relationship.
4. Polymorphism
A entity which behaves differntly at different places.
Eg. A person behaves as a good husband in family.
He behaves as a good employee at company.
He also behaves as a good citizen in public.
Monday, May 20, 2013
switch case in jsp
<% switch (tabNavigator)
{
case QUALIFICATION:
%>
<jsp:include page="classmaster.jsp"></jsp:include>
<%
break;
case RACKSHELF:
%>
<jsp:include page="rackshelfmaster.jsp"></jsp:include>
<%
break;
case MEDIA:
%>
<jsp:include page="mediamaster.jsp"></jsp:include>
<%
break;
case MEMBER:
%>
<jsp:include page="membermaster.jsp"></jsp:include>
<%
break;
case PLAN:
%>
<jsp:include page="planmaster.jsp"></jsp:include>
<%
break;
case DEPARTMENT:
%>
<jsp:include page="departmentmaster.jsp"></jsp:include>
<%
break;
case VENDOR:
%>
<jsp:include page="vendormaster.jsp"></jsp:include>
<%
break;
default:
%>
<jsp:include page="index.jsp"></jsp:include>
<%
}
session.removeAttribute("NAVIGATOR_REQUEST");
}
%>
Tuesday, April 30, 2013
Dynamically adding textbox and labels in jsp
1)ajax.jsp:
<%@ page import="java.sql.*" %> <%
String name = request.getParameter("name").toString();
System.out.println(name);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee where name='"+name+"'");
while(rs.next())
{
data =rs.getString("address");
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>
<%@page import="java.sql.*"%><html>
<head>
<script type="text/javascript">
function showData(value){
xmlHttp=GetXmlHttpObject()
var url="getdata.jsp";
url=url+"?name="+value;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() { if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
var showdata = xmlHttp.responseText;
document.getElementById("lab").innerHTML="Address";
document.getElementById("address").style.visibility="visible";
document.getElementById("address").value= showdata;
} }
function GetXmlHttpObject(){
var xmlHttp=null;
try {
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<br><br>
<pre>
<label>Name</label> <input type="text" name="name" id="name" onkeyup="showData(this.value);">
<label id="lab"> </label> <input style="visibility:hidden" type="text" name="address" id="address">
</form> </html>
--------------------------------------------------
2)getdata.jsp:<%@ page import="java.sql.*" %> <%
String name = request.getParameter("name").toString();
System.out.println(name);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee where name='"+name+"'");
while(rs.next())
{
data =rs.getString("address");
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>
How to create drop down list using JSP
how to display values from database into table using jsp
Code----> Link
---------------------------------------------------------------------------------
Code----> Link
---------------------------------------------------------------------------------
<%@ page language="java" %><%@ page import="java.sql.*" %><script>
function change(){
var cid=document.getElementById("book").selectedIndex;
var val = document.getElementById("book").options[cid].text;
window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?id="+cid+"&&value="+val);
}
function extract(){
var ide=document.getElementById("info").selectedIndex;
var bookname = document.getElementById("info").options[ide].text;
window.location.replace("http://localhost:8080/examples/jsp/dependentDropdown.jsp?book="+bookname);
}
</script>
<%!
Connection conn = null;
ResultSet rs =null;
Statement st=null;
String query="";
%><%
String value=request.getParameter("value");
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root";, "root");
st = conn.createStatement();
rs = st.executeQuery("select * from book");
%><select id="book" onchange="change();">
<option value="0">--Please Select--</option>
<% while(rs.next()){ %>
<option value="<%=rs.getString("books")%>"><%=rs.getString("books")%></option>
<% if(rs.getString("books").equals(value)){%>
<option value="<%=value%>" selected disabled><%=value%></option>
<%
}
}
%></select>
<select id="info" onchange="extract(this)">
<option value="0">--Please Select--</option>
<%
String id=request.getParameter("id");
rs=st.executeQuery("select * from bookInformation where bookid='"+id+"'");
while(rs.next()){
%>
<option value="<%=rs.getString("id")%>" ><%=rs.getString("booknames")%></option>
<%
}
%></select>
<%
String book=request.getParameter("book");
String author="";
String price="";
rs=st.executeQuery("select * from bookInformation where booknames='"+book+"'");
while(rs.next()){
author=rs.getString("writer");
price=rs.getString("price");
}
if((book!=null)&&(author!=null)&&(price!=null)){
%><table >
<tr><td>Book Name</td><td><input type="text" value="<%=book%>"></td></tr>
<tr><td>Author</td><td><input type="text" value="<%=author%>"></td></tr>
<tr><td>Price</td><td><input type="text" value="<%=price%>"></td></tr>
</table>
<%
}
%></body>
</html>
-------------------------------------------------------
Book
CREATE TABLE `book` (
`bookid` bigint(20) NOT NULL auto_increment,
`books` varchar(255) default NULL,
PRIMARY KEY (`bookid`)
);
------------------------------------------------------
BookInformation
CREATE TABLE `bookinformation` (
`id` bigint(255) NOT NULL auto_increment,
`bookid` int(255) default NULL,
`booknames` varchar(255) default NULL,
`writer` varchar(255) default NULL,
`Price` double default NULL,
PRIMARY KEY (`id`)
);
Monday, April 29, 2013
CheckBox Example in jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show all names</title>
</head>
<body>
<form method="GET" action='Controller' name="showall">
<table>
<tr>
<td><input type="checkbox" name="id1" /></td>
<td>Jim</td>
<td>Knopf</td>
</tr>
<tr>
<td><input type="checkbox" name="id2" /></td>
<td>Jim</td>
<td>Bean</td>
</tr>
</table>
<p><input type="submit" name="delete" value="delete" />
<input type="submit" name="edit" value="edit" />
<input type="reset"
value="reset" /></p>
</form>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Show all names</title>
</head>
<body>
<form method="GET" action='Controller' name="showall">
<table>
<tr>
<td><input type="checkbox" name="id1" /></td>
<td>Jim</td>
<td>Knopf</td>
</tr>
<tr>
<td><input type="checkbox" name="id2" /></td>
<td>Jim</td>
<td>Bean</td>
</tr>
</table>
<p><input type="submit" name="delete" value="delete" />
<input type="submit" name="edit" value="edit" />
<input type="reset"
value="reset" /></p>
</form>
</body>
</html>
DoublyLinkedList Example in java
public class DoublyLinkedList {
private Link first;
private Link last;
public DoublyLinkedList() {
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public void insertFirst(long dd){
Link newLink = new Link(dd);
if (isEmpty())
last = newLink;
else
first.previous = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(long dd){
Link newLink = new Link(dd);
if (isEmpty())
first = newLink;
else {
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}
public Link deleteFirst(){
Link temp = first;
if (first.next == null)
last = null;
else
first.next.previous = null;
first = first.next;
return temp;
}
public Link deleteLast(){
Link temp = last;
if (first.next == null)
first = null;
else
last.previous.next = null;
last = last.previous;
return temp;
}
public boolean insertAfter(long key, long dd) {
Link current = first;
while (current.dData != key){
current = current.next;
if (current == null)
return false; // cannot find it
}
Link newLink = new Link(dd); // make new link
if (current == last) // if last link,
{
newLink.next = null;
last = newLink;
} else // not last link,
{
newLink.next = current.next;
current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true; // found it, insert
}
public Link deleteKey(long key){
Link current = first;
while (current.dData != key)
{
current = current.next;
if (current == null)
return null; // cannot find it
}
if (current == first) // found it; first item?
first = current.next;
else
current.previous.next = current.next;
if (current == last) // last item?
last = current.previous;
else
// not last
current.next.previous = current.previous;
return current; // return value
}
public void displayForward() {
System.out.print("List (first to last): ");
Link current = first; // start at beginning
while (current != null) // until end of list,
{
current.displayLink();
current = current.next; // move to next link
}
System.out.println("");
}
public void displayBackward() {
System.out.print("List : ");
Link current = last;
while (current != null){
current.displayLink();
current = current.previous;
}
System.out.println("");
}
public static void main(String[] args) {
DoublyLinkedList theList = new DoublyLinkedList();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertLast(33);
theList.insertLast(55);
theList.displayForward();
theList.displayBackward();
theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(11);
theList.displayForward();
theList.insertAfter(22, 77); // insert 77 after 22
theList.insertAfter(33, 88); // insert 88 after 33
theList.displayForward();
}
}
class Link {
public long dData; // data item
public Link next; // next link in list
public Link previous; // previous link in list
public Link(long d)
{
dData = d;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
Subscribe to:
Posts (Atom)