Posts

Showing posts from July, 2016

Velocity Template Example in Java

Velocity is a simple to use, open source web framework designed to be used as the view component of an MVC architecture.  .. The main advantage of using Velocity over JSP is that Velocity is simple to use. The Velocity Template Language (VTL) is so constrained in its capabilities that it helps to enforce separation of business logic from the view.  You won't see any Java classes in a Velocity template (an HTML document with some Velocity placeholders).  . Add Three Jars : apache-commons-lang.jar , apache-velocity-velocity-1.5.jar , org.apache.commons.collections.jar /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package jaxb; import java.io.StringWriter; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext;

What is difference between Vector and ArrayList in Java?

One of the important interview question in java specially from Experience candidate Vector :-          /*          * Vector is synchronized and thread-safe.          * Performance wise slower then ArrayList.          * One Thread is running at a time.          * Once thread got completed it gives lock to other thread          * Thread Safe          */  ArrayList :-        /*          * ArrayList is not synchronized nor thread-safe.          * Performance wise faster then Vector.          * multiple Threads is running at a time.          * Not Thread Safe          */ Programme /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package highleveljava; import java.util.*; /**  *  * @author Admin  */ public class ArrayListandVector {     public Vector vector;     public List list;     public String fname = "Ajeet";     public String

How to Add and Fetch Class Data from List in Java

One of the most concept which is used in interview question as well as in program level . because if we add object into list and print the reference it will print whole data not referece But if we add Class level data it will print reference so we need to perform type casting so easily we can fetch the record and print it  programme:- /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package highleveljava; import java.util.*; /**  *  * @author Admin  */ public class GenericType {     /*      Global Variable      */     public static String customername;     public static String customerid;     public static String customerlocation;     /*      Setter and getter      */     public static String getCustomername() {         return customername;     }     public static void setCustomername(String customername) {         GenericType.customername

Nested while loop in java

Placing one while loop with in the body of another while is called Nested while loop in java programm. CODE public class NestedWhileExample { public static void main(String arg[]) { int outer = 1; while(outer < 3) { int inner = 5; while(inner < 8) { System.out.println(outer + " " + inner); inner++; } outer++; } } } Output : 1 5 1 6 1 7 2 5 2 6 2 7 DESCRIPTION Here outer loop is executed for values going from 1 till less than 3 i.e. 1 and 2. Similarly inner loop is executed from 5 till less than 8 i.e. 5, 6 and 7. So when outer is 1, inner will become 5, 6, and 7 and when outer is 2, then also inner will become 5, 6, and 7.

How to wait on file creation in Java

Here are some steps that we can remember while move in to coding part Iterate the (!file.exist()) blog till this block becomes as a false . so for that we can use Thread class and do Thread.sleep(10). that will wait for 10ns  and once time has been expire again it start same loop . means every 10 ns it will check on directory whether file is exist or not . if not then iterate.  once it got the file on directory then while loop iteration will stop and terminate the execution of while block   Programme to wait on file creation in Java /*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ import java.io.*; /**  *  * @author Anand  */ public class FileOperation {     public void ProcessFile() {         File statText = new File("statsTest1.txt");// Check for this file         while (!statText.exists()) {             try {                 Thre

Null Pointer Exception Class Implementation in Java

Here is the implementation of NullPointerException class in Java : -   1   /*     2    * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.     3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.     4    *     5    * This code is free software; you can redistribute it and/or modify it     6    * under the terms of the GNU General Public License version 2 only, as     7    * published by the Free Software Foundation.  Oracle designates this     8    * particular file as subject to the "Classpath" exception as provided     9    * by Oracle in the LICENSE file that accompanied this code.    10    *    11    * This code is distributed in the hope that it will be useful, but WITHOUT    12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    14    * version 2 for more details (a copy is included in the LICENSE file tha