Way To Java

Way To Java

Share

Java , Android, C , C++, J2EE, Data Structure, Python, HTML,CSS, JavaScript, MS Office Enhance your skills in Java and Android.

21/07/2017

Important points about Generics
-------------------------------------------

Generics concept is only applicable at compile time, not at run time.

At the time of compilation, as last step generics syntax will be removed.

Hence for the JVM, generics syntax will not be available.

Example :
-------------
import java.util.ArrayList;
class My{
public static void main(String[]args){
ArrayList al = new ArrayList();
al.add(10);
al.add(20.5);
al.add(true);
System.out.println(al);
}
}

Output - [10, 20.5, true]

Note -

Since JVM is unknown to the generics template.
Hence all those declarations are valid.
For these ArrayList object we can add any type of objects.
All these declarations are exactly same.
----------------------------------------------------
ArrayList al = new ArrayList();

ArrayList al = new ArrayList();

ArrayList al = new ArrayList();

ArrayList al = new ArrayList();

ArrayList al = new ArrayList();

20/07/2017

static variables do not participate in Serialization. Hence declaring static variable as transient there is no use.

Declaring final variables as transient there is no use at all.

i.e. Declaring static and final variable as transient there is no impact on serialization.

20/07/2017

Serialization in Java
---------------------------
The process of saving the state of an object to a file is known as Serialization.
OR
The process of converting an object from normal java supported form to file supported form or network supported form. This conversion process is called Serialization.

Example :

import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

class Student implements Serializable{
int rollNo;
int age;

Student(int rollNo, int age){
this.rollNo = rollNo;
this.age = age;
}
}

class SerializationDemo{
public static void main(String[]args){

Student st1 = new Student(1001, 15);
try{
FileOutputStream fout = new FileOutputStream("student.txt");
ObjectOutputStream os = new ObjectOutputStream(fout);
os.writeObject(st1);
}catch(IOException e){}
System.out.println("Object Written Successfully !!");
}
}

19/07/2017

HashSet and LinkedHashSet class
-----------------------------------------------
Both the classes are present in java.util package.

HashSet is the implementation class for Set interface.

LinkedHashSet is the child class of HashSet class.

Insertion order is not preserved in HashSet, but insertion order is preserved in LinkedHashSet.

Data structure for HashSet is hash table, but data structure for LinkedHashSet is linked list + hash table.

HashSet class is introduced in 1.2 version of Java, but LinkedHashSet class is introduced in 1.4 version of Java.

Note -
LinkedHashSet is used to develop cache based applications.

19/07/2017

import java.util.ArrayList;
import java.util.Iterator;
class IteratorTest{
public static void main(String[]args){
ArrayList al = new ArrayList();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);

System.out.println(al);
System.out.println("--------------------");
Iterator itr = al.iterator();
while(itr.hasNext()){
Integer i = (Integer)itr.next();
System.out.println(i);
}
}
}

19/07/2017

Enumeration interface in java
---------------------------------------
Enumeration interface is present in java.util package.

Enumeration object is created by elements() method of Vector class.

Vector v = new Vector();

Enumeration e = v.elements();

StringTokenizer is the only implementation class for Enumeration interface.

There are only two methods present in Enumeration interface-
1. boolean hasMoreElements()
2. public Object nextElement()

Example :
-------------
import java.util.Vector;
import java.util.Enumeration;
class EnumerationDemo{
public static void main(String[]args){
Vector v = new Vector();
v.addElement("A");
v.addElement("B");
v.addElement("C");
v.addElement("D");
v.addElement("E");

System.out.println(v);
System.out.println("------------------------------");
Enumeration e = v.elements();
while(e.hasMoreElements()){
String s = e.nextElement().toString();
System.out.println(s);
}
}
}

Want your school to be the top-listed School/college in Noida?
Click here to claim your Sponsored Listing.

Category

Address


Noida