Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Vector Class In Java

Vector Class In Java

  • vector is a one kind of dynamic array.
  • default capacity of vector is 10.
  • when overflow the vector class (Greater than) than its grow with double capacity.
  • Vector is synchronized.
  • it has a capacity() to ensure the vector capacity.
  • Vector contains some spacial methods that not a part of collection framework.
  • Vector proves to be very useful if you don't know the size of the array in advance
  • you need to import java.util.*;


NOTE : if equal capacity and size of elements than not grow.

Constructors :

Constructors
Vector() create a vector with initial capacity 10.
Vector(int size) create a vector with one parameter size is capacity you can define.
Vector(int size, int incr) create a vector with initial capacity and increement value when overflow it that you can define
Vector(Collection c) create a vector contains the Collection.


Methods :

Methods
void add(int index, Object element) you can add element at spacific index
boolean addAll(int index, Collection c) add Collection start at spacific index.
void addElement(Object obj) add element at last position of vector class.
int capacity() it returns the capacity (size) of an vector.
void clear() it is used to clear the vector.
boolean contains(Object elem) check wather elements is contains in vector.
boolean containsAll(Collection c) check wather entire collection in vector.
Enumeration elements() return enumeration of vector elements.
Object get(int index) get element by spacifying index.
boolean isEmpty() check wather the vector is empty or not.
Object remove(int index) remove element by giving index of elements.
boolean remove(Object o) remove element by giving object as parameter.
Object set(int index, Object element) you can replace element by spacifying position.
List subList(int fromIndex, int toIndex) create a sublist of inclusive fromIndex to toIndex.
Object[] toArray() return an array.
void trimToSize() Trims the capacity of this vector to be the vector's current size.
public int size() returns number of elements in vector.

Example of Vector class.
// Vector class
 
// class B that extends A and implements C
 
 class VectorExample{
  public static void main(String... str){
   
   // create a empty vector has default capacity is 10.
   Vector v=new Vector();
   
   // first we check initial capacity of vector class
   System.out.println("Capacity : "+v.capacity());
   
   // add one String object
   v.addElement("abc");
   
   // add one int value
   v.addElement(45);
   
   // add one float value
   v.add(22.22);
   
   // check vector class capacity
   System.out.println("Capacity : "+v.capacity());
   
   // add another 7 values to check capacity
   
   for(int a=0;a<7;a++){
    v.add(a+12);
   }
   
   // check vector class capacity
   System.out.println("Capacity : "+v.capacity());
   
   // now remove one
   v.remove(22.22);
   
   // check capacity
   System.out.println("Capacity : "+v.capacity());  
   
   // enumeration for print each elements of vector class
   // v.elements() is method of vector class that return enumeration
   
   Enumeration venum = v.elements();
      
   while(venum.hasMoreElements())
     System.out.print(venum.nextElement() + " ");
   
   // now clear vector class
   v.clear();
   
   // print size of vector class   
   System.out.println("Size : "+v.size());
  }
 }

regular expressions in java

regular expressions in java

Java provides the java.util.regex package for pattern matching with regular expressions.
regular expressions can be used to search, edit, or manipulate text and data.

java.util.regex consist of following classes : 

  • Pattern
  • Matcher
  • PatternSyntaxException
  1. Pattern : It is used to define pattern or input we need from user.pattern class is not provide any constructor to create its object so we have to write its static compile() method.
  1. Matcher : A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.
  1. PatternSyntaxException : A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.
Some Sub-expressions are given below :

Sub-Expressions Description
^ Matches beginning of line.
$ Matches end of line.
. Matches any single character except newline. Using m option allows it to match newline as well.
\w Matches word characters.
\W Matches nonword characters.
\s Matches whitespace.
\S Matches nonwhitespace.
\d Matches digits.
\D Matches nondigits.
{n} Matches exactly n number of occurrences

Example of testing a password string that contains must one lowercase,uppercase,number and spacial character and minimum 6 and maximum 15 characters.
 
// regex Password validation
 
 import java.util.regex.*;

 class reg{
 
 public static void main(String... str) throws Exception{
  
  
  Pattern p=Pattern.compile("(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+-]).{6,15}");
  
  Matcher m=p.matcher("1$6855146146Bbb");
  
  System.out.println(m.matches());
  
 }
 
}
Different between java v/s javac v/s javacpl vs. javaw

Different between java v/s javac v/s javacpl vs. javaw

javacpl.cpl :

  • javacpl.cpl is a java controll panel belonging to an java(TM) Platform SE 6 U5 from Sun Microsystems, Inc.
  • Non-system processes like javacpl.cpl originate from software you installed on your system. 
  • Since most applications store data in your system's registry.
  • To stop javacpl.cpl permanently uninstall 'Java(TM) Platform SE 6 U5' from your system.

javac :

  • javac is a compiler program. it compiles java source program [ .java ] into bytecode class file [ .class ]
  • To use javac you need to add java bin directory path in your system variable.
  • javac is inside the JDK_HOME/bin in java directory in windows.
  • How to compile java source program follow the steps.
  1. First if you don`t set Environment path then setup. and open "command prompt".
  2. press  [ windows key + R ] and type [ cmd ].
  3. Now, write javac filename.java

javaw :


  • javaw.exe is located in bin directory.
  • javaw.exe is very similar to java.exe. It can be considered as a parallel twin. It is a Win32 GUI application.
  • his is provided as a helper so that application launches its own GUI window and will not launch a console. Whenever we want to run a GUI based application and don’t require a command console, we can use this as application launcher. Eclipse is used javaw.exe.

import javax.swing.*;

public class HelloWorldSwing {
    private static void createAndShowGUI() {
        JFrame jFrame = new JFrame("HelloWorld Swing");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel helloLabel = new JLabel("Hello World!");
        jFrame.getContentPane().add(helloLabel);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

  • We can execute the above GUI application using both java.exe and javaw.exe If we launch using java.exe, the command-line waits for the application response till it closes. When launched using javaw, the application launches and the command line exits immediately and ready for next command

Difference between abstract class and interface

Difference between abstract class and interface


Abstract Class Interface
Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
Abstract class can have constructor. Interface can not have constructor.
Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
Calendar class is example of abstract class. ActionListener is example of interface.
Example:
 public abstract class Shape{
  public abstract void draw();
 }
Example:
 public interface Drawable{
  void draw();
 }

Example of abstract class and interface in Java

 // abstract class
 
 abstract class A{
  abstract void display();
 }

 // interface
 interface C{
  
  void test();
  void disp();
  
 }

 // class B that extends A and implements C
 
 class B extends A implements C{
  public static void main(String... str){
   B b=new B();
   
   // call interface methods
   
   b.disp();
   b.test();
   
   // call abstract class method
   
   b.display();
  }
  
  public void test(){
   System.out.println("test method.");
  }
  
  public void disp(){
   System.out.println("Disp method.");
  }
  public void display(){
   System.out.println("Display method.");
  }
 }