Hi,
i am writing the java interview concepts i am not giving complete details i am giving the overall structure for glance when ever interview is there it is for quick reference i hope this might help
Object class Details
Object Class is Super class of all java classes
Object class having the following methods
public final Class getClass()
public int hashCode()
public boolean equals(Object obj)
protected Object clone() throws CloneNotSupportedException
public String toString()
public final void notify()
public final void notifyAll()
public final void wait(long timeout) throws InterruptedException
protected void finalize() throws Throwable
--------etc
you will get all the implementation details and some where most of the people
ask about these details some questions are
Can we override finalize() method
Ans: yes we can override this method when ever we know object is going out of memory
that type object we call in finalize method. this work automatically done by java only
it has good alogitihm to remove the object from memory.
notify(), wait(),notifyAll() are related to threads but why there are overrided in
Object Class
Ans: this methods not only for Threads it is external resources also that's why they are
overrided inside Object class
Difference b/n equal (==) operator and equals() method why should we override
Ans: in equals() method also they are using == operator only but when ever we want to
compare content of the two objects and both objects content has same but when
we compare it it return false so we should have to override equals() in our class.
Now i am going to explain methods in Objects class with examples
1. toString()
Syntax: public String toString()
{
return " ";
}
> in String class toString() method is overrided. when ever you are working with String class you no need to override toString() method.
example without overriding toString()
public class Person
{
private String mname;
public Person(String aname)
{
mname= aname;
}
public static void main(String args[])
{
Person p = new Person("siva");
System.out.println("Name is " + p);
or //toString() method is Object class method so it will come for
any class
System.out.println("Name is " + p.toString())
}
}
ANS: Name is ClassName i.e Person@some hexadecimal value
here hexa decimal value is where the object stored in memory area
so result is not correct we need correct result for that we have to override
toString() method
attach this code for Person class
public String toString()
{
return mname;
}
Now run the program you will get result as ----- Name is siva
Wednesday, July 2, 2008
Tuesday, June 24, 2008
Wednesday, June 11, 2008
Simple Hello world in core with java installation
Learn java in easy way
The following steps are how to install java , how to set path and "Hello world"
1. First download JDK latest version from http://java.sun.com/javase/downloads/index.jsp
2. save your hard disk and double click on that
3.while instalation it will ask location to install the JRE and JDK
otherwise by default it will install inside Programfiles of your system.
for JDK choose another location and for JRE keep by default location.
4. you completed instalation successfully.
5. Now you need to tell your operating System where you installed java as well as
to run java program also for this you have to set path in your system.
setting path in different ways i am giving simple way
right click on My Computer -properties-Advanced-Environment variables
if you click on Environment Variables one box will open and in that all the paths will be
located .
in that we find two windows
UserVaribles
SystemVaribles
-- in System variable you find variable name as path edit this and come to end of that
line give semicolon(;)
After that copy your java (JDK) installation location eg:d:\java\jdk1.5 you need to copy upto bin(eg:d:\java\jdk1.5\bin) copy location and paste after semicolon ( ; ) and give semicolon after java path also.
now your set the path successfully.
if you want to see path is set fine or not just open command prompt and type java
if it gives some help text then your path is ok otherwise again set the path by follwing above steps carefully.
once set the path successfully. Then cretae one folder for your all java programs in your system
eg:d:\javaexamples
How To Write a Hello world Program
1. open notepad or edit plus
type the following code
class Hello
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
2.save this program in d:\javaexamples folder.
3.open the command prompt and open d:\javaexamples
4. now type like this javac Hello.java
5. if you have no errors in your program and while setting the path agin it will show
d:\javaexamples
6. after that you type java Hello
7. after run the program you will see "Hello world" in commond prompt
The following steps are how to install java , how to set path and "Hello world"
1. First download JDK latest version from http://java.sun.com/javase/downloads/index.jsp
2. save your hard disk and double click on that
3.while instalation it will ask location to install the JRE and JDK
otherwise by default it will install inside Programfiles of your system.
for JDK choose another location and for JRE keep by default location.
4. you completed instalation successfully.
5. Now you need to tell your operating System where you installed java as well as
to run java program also for this you have to set path in your system.
setting path in different ways i am giving simple way
right click on My Computer -properties-Advanced-Environment variables
if you click on Environment Variables one box will open and in that all the paths will be
located .
in that we find two windows
UserVaribles
SystemVaribles
-- in System variable you find variable name as path edit this and come to end of that
line give semicolon(;)
After that copy your java (JDK) installation location eg:d:\java\jdk1.5 you need to copy upto bin(eg:d:\java\jdk1.5\bin) copy location and paste after semicolon ( ; ) and give semicolon after java path also.
now your set the path successfully.
if you want to see path is set fine or not just open command prompt and type java
if it gives some help text then your path is ok otherwise again set the path by follwing above steps carefully.
once set the path successfully. Then cretae one folder for your all java programs in your system
eg:d:\javaexamples
How To Write a Hello world Program
1. open notepad or edit plus
type the following code
class Hello
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
2.save this program in d:\javaexamples folder.
3.open the command prompt and open d:\javaexamples
4. now type like this javac Hello.java
5. if you have no errors in your program and while setting the path agin it will show
d:\javaexamples
6. after that you type java Hello
7. after run the program you will see "Hello world" in commond prompt
Tuesday, May 20, 2008
AJAX simple example
Hi,
This is simple ajax example with jsp when key up in one textbox time will be displayed on another textbox
1.we need html page look follwing
<form name="myform">
Name:<input type="text" name="username" onkeyup="ajaxFunction()"/>
Time:<input type= "text" name="time"/> </form>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();//This is for other than IE browser creating
object.
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support ajax");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
document.myform.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
}
</script>
2. time.jsp in same location
<%@page import="java.text.SimpleDateFormat%>
< %@page import="java.util.Calendar"%>
<%
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");
String time = sd.format(cal.getTime());
out.println(time);
%>
if we run this application thru server we will get the result
This is simple ajax example with jsp when key up in one textbox time will be displayed on another textbox
1.we need html page look follwing
<form name="myform">
Name:<input type="text" name="username" onkeyup="ajaxFunction()"/>
Time:<input type= "text" name="time"/> </form>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();//This is for other than IE browser creating
object.
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support ajax");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4)
{
document.myform.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","time.jsp",true);
xmlHttp.send(null);
}
</script>
2. time.jsp in same location
<%@page import="java.text.SimpleDateFormat%>
< %@page import="java.util.Calendar"%>
<%
Calendar cal = Calendar.getInstance();
SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");
String time = sd.format(cal.getTime());
out.println(time);
%>
if we run this application thru server we will get the result
Subscribe to:
Posts (Atom)