Concepts On Basic Java
Concepts On Basic Java
The goal is to write one application (in this example, an interactive party Invitation) and
have it work on whatever device your friends have.
Source file Compiler Java Byte code file Interpreted by any machine having
Anatomy of Class
public class MyFirstClass
{
public static void main (String args[])
{
System.out.println(“Hi Boss…!!”);
}
}
Notes:-
• A Java application is nothing but objects calling other objects.
• The purpose of main() method is to launch the java application.
• The ‘instance variables’ are class variables.
• The ‘local variables’ are method variables.
Garbage Collection:
Each time object is created, it goes into HEAP MEMORY. When system is running out of
memory, JVM runs garbage collector which removes unreachable objects and free up
memory space.
Notes:
• myObj.bark() == means use the object referenced by myObj variable to invoke
the bark method.
• Difference between INSTANCE VARIABLE and LOCAL VARIABLE is that local
variables do not get default value. They must be initialized b4 its use.
Inheritance:
Somethinf will come here…
Polymorphism:
* Why we need abstract classes?
Ans: While doing Inheritance we need certain classes, such as Animal class which is
extended by child classes such as Tiger, Deer, Hippopotamus, etc. Here we need objects
of Tiger, Deer, Hippopotamus; but object of Animal Class isn’t required. To prevent such
classes from being instantiated we need to make these classes Abstract. By writing
keyword abstract .
Declarating class Animal As Abstract :
Exception
* What are checked and unchecked exceptions?
Ans:
* If you don’t want to handle the exception, what will you do?
Ans: Exception works on HANDLE or DECLARE concept. If you don’t want to handle
the exception, you can duck it by declaring it. The method with a risky code THROWS
exception and its calling method will follow HANDLE or DECLARE law. It goes till
main () method, if that too doesn’t handle it and duck it by declaring then it will go to
JVM and JVM will shut down.
Object Synchronization
* Why we need to save Objects?
Ans: Objects have state and behavior. Sometimes we need to save the state of java
program. For example Java game, etc. there you need to save Objects.
Servlets
* What is web server?
Ans: It is a computer program that delivers (serves) the content like web pages using
HTTP over web. It can be a computer or virtual machine running the program. Popular
web servers are Apache, Microsoft’s Internet Information Server, Google’s GWS
(Google Web Server).
* What is Servlet?
Ans: It is a java program that generates dynamic content and interacts with web clients
using request-response paradigm. We need servlets because, they inherits security from
web container, they maintain session for end-user, enhance server performance by
preventing frequent disk access.
The difference is in the nature of the connection they define between a client and a
server.Telnet and FTP, for example are stateful protocols
The client connects to the server, conducts a series of operations via that conneciton, and
then disconnects.Then server can associate all of the requests together and knows that thy
all came from the same user.
HTTP , on the other hand is a Stateless Protocol.It is concerned with requests and
responses,which are simple,isolated transactions.This is perfect for simple web browsing,
where each request typically results in a file(an HTML document or a GIF image etc)
being sent back to the client.The server does not need to know whether a series of
requests come from the same, or from different clients, or whether those requests are
related or distinct
Collection
1. Enumeration:
Note: This functionality has been duplicated by iterator interface with an added remove
operation and shorter method names.
2. Iterator:
while(iterator.hasNext()){
System.out.println("Arraylist contain : "+iterator.next());
}
iterator.remove();
The remove () method removes the last element returned by the iterator.
3. ListIterator:
4. List:
Here, cap is the new capacity. Conversely, if you want to reduce the size of the array that
underlies an ArrayList object so that it is precisely as large as the number of items that it
is currently holding, call trimToSize( ), shown here:
void trimToSize( )
Note: Methods in Vector are synchronised which means they are thread-safe and thus
preclude access to the Vector elements by concurrent threads.But this imposes additional
overhead on the JVM as it has to acquire and release locks on the vector objects under
consideration.
This is not possible in ArrayList since those methods are not synchronised and hence are
faster in performance.
Use Vector only if it will be accessed by multiple threads at a time else ArrayList is
always better.
5. LinkedList:
System.out.println("***************************************************
******************");
System.out.println("");
System.out.println("Remove elements from 2-4");
linkedList2.subList(2, 5).clear();
System.out.println("Now linkedList2 :");
listIterator2 = linkedList2.listIterator();
while(listIterator2.hasNext()){
System.out.println(listIterator2.next());
}
6. Map:
* What is Map?
Ans: Map is interface used to map keys to values. No duplicate keys allowed. Each key
can point to at most one value. Values can be duplicated.
The Comparator interface defines two methods: compare( ) and equals( ). The
compare( ) method, shown here, compares two elements for order:
int compare(Object obj1, Object obj2) obj1 and obj2 are the objects to be compared. This
method returns zero if the objects are equal. It returns a positive value if obj1 is greater
than obj2. Otherwise, a negative value is returned. The method can throw a
ClassCastException if the types of the objects are not compatible for comparison. By
overriding compare( ), you can alter the way that objects are ordered. For example, to sort
in reverse order, you can create a comparator that reverses the outcome of a comparison.
The equals( ) method, shown here, tests whether an object equals the invoking
comparator: boolean equals(Object obj)
obj is the object to be tested for equality. The method returns true if obj and the
invoking object are both Comparator objects and use the same ordering. Otherwise, it
returns false. Overriding equals( ) is unnecessary, and most simple comparators will
not do so.
* Explain Properties class?
Ans: Properties is a subclass of Hashtable. It is used to maintain lists of values in which
the key is a String and the value is also a String. The Properties class is used by many
other Java classes. For example, it is the type of object returned by
System.getProperties( ) when obtaining environmental values.
* StringTokenizer, explain?
Ans: StringTokenizer implements Enumeration interface which helps to enumerate
through tokens.
Java.lang.io
1. BufferedInputStream:
Exceptions
* Try, Catch, Throw, Throws, Finally what is the purpose of theses words and how
to use these words?
Ans:
1. Try: code capable of generating exception is written within try block.
2. Catch: On exception, it is handled here in rational manner.
3. Throw: To manually throw an exception throw is used.
4. Throws: Any exception that is thrown out of a method must be specified by
throws
5. Finally: code that must be run irrespective of exception is written in finally
block.
/**
* TestThrow class demonstrate throw statement in java to throw
exception manually.
* @param args
*/
}
class MyArithmeticException extends Exception{
MyArithmeticException(String str) {
super(str);
}
}
/* Output
* when divider is not zero ==>
* a=100 b=0 c=20
* Result: 5.0
*
* when divider is zero ==>
* a=100 b=0 c=20
* Arithmatic: myException.MyArithmeticException: can't divide by zero..
*/
Example:
// This is now correct.
class ThrowsDemo {
String
* State the difference between equals () and == operators?
Ans: It is important to understand that the equals( ) method and the == operator perform
two different operations. As just explained, the equals( ) method compares the
characters inside a String object. The == operator compares two object references to
see whether they refer to the same instance.
Examples:
// equals() vs ==
class EqualsNotEqualTo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
Output:
Hello equals Hello -> true
Hello == Hello -> false