View RSS Feed

All Blog Entries

  1. Script Engines

    by , 06-12-2011 at 11:07 AM (JosAH's blog)


    Java is not Javascript and most of the times when questions end up in the wrong forum, they're moved to another forum as soon as possible. Accidentally this article talks about Javascript a bit but it belongs in this Java section.

    Since version 1.6 of Java a script engine comes packaged with the core classes. A script engine is an abstract little framework that offers support for other languages, scripting languages to be exact, ...

    Updated 06-12-2011 at 11:10 AM by JosAH

  2. Heap Sort II

    by , 06-12-2011 at 10:54 AM (JosAH's blog)

    This is the second part of the Heap Sort article (that dumb forum software only allows 10000 characters per article part. Here's the second part:

    The first part of this article explained a bit about the heap sort algorithm itself. Now let's get back to our original problem: given the two arrays:

    Java Code:
    String[] first= { "Fred", "Barney", "Wilma", "Betty", "Bambam", "Pebbles" };

    Updated 06-12-2011 at 05:30 PM by JosAH

  3. Heap Sort I

    by , 06-12-2011 at 10:52 AM (JosAH's blog)

    A lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data.

    Examples of two main problem scenarios are like this:

    1) an array containing a first name and another array containing a last name and possibly a third array containing the age of a person; how to sort the arrays according to, say, the first name.

    2) a List of objects where each object contains a first name, ...

    Updated 11-18-2013 at 09:46 AM by JosAH

  4. Lazy associations

    by , 06-02-2011 at 08:26 PM (My Java Tips)
    Hibernate uses lazy select fetching by default for collections and lazy proxy fetching for single-valued associations. This default behavior makes sense for almost all associations in almost all applications.

    Hibernate will use the batch fetch optimization for lazy fetching if hibernate.default_batch_fetch_size parameter is set. This optimization may also be enabled at a more granular level.

    Lazy fetching introduces a problem that should be taken care of. Remember that ...
  5. Criteria instance

    by , 06-02-2011 at 08:22 PM (My Java Tips)
    Hibernate provides an interface org.hibernate.Criteria, which represents a query against a particular persistent class.

    Review the following example:

    Java Code:
    Criteria crit = session.createCriteria(Student.class);
    List students = crit.list();
    Resultset can be narrowed according to requirement. The class org.hibernate.criterion.Restrictions defines factory methods for obtaining certain built-in Criterion types. ...
  6. one-to-many, sql

    by , 06-02-2011 at 08:20 PM (My Java Tips)
    Let me presnet a simple <one-to-many> association from Parent to Child.

    XML Code:
    <set name="children">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
    Assume that we wish to execute the following code:

    Java Code:
    Parent p = .....;
    Child c = new Child();
    Tags: one-to-many Add / Edit Tags
  7. Svn revert

    by , 06-02-2011 at 08:16 PM (My Java Tips)
    You must have used SVN while working in a team on a Java project. In this post, Ill write about 'revert' command.

    Let me present a scenario. You made some changes in a class and now you realize that the changes are not required/not correct. You wish to discard your changes. You have to use svn revert to discard your changes.

    Java Code:
    $ svn revert
    Following switches are available:

    Java Code:
    Tags: revert, svn Add / Edit Tags
  8. No Persistence provider for EntityManager

    by , 06-02-2011 at 08:15 PM (My Java Tips)
    You might have encountered the following exception while working with hibernate:

    Java Code:
    [java] Exception in thread "main" javax.persistence.PersistenceException: 
    No Persistence provider for EntityManager named  ****
    Make sure you have all the required jars in the class path.

    Java Code:
  9. Static/Non static methods - Example

    by , 06-02-2011 at 08:12 PM (My Java Tips)
    I will present an example to show the difference between static and non-static methods.

    Go through the example below:

    Java Code:
    public abstract class AbstractExample {
       public static void foo() {
          System.out.println("Static foo called.");
       public void bar() {
          System.out.println("Non-static bar called.");
    public class AbstractCaller extends AbstractExample {
    Java SE
  10. Factory Methods

    by , 06-02-2011 at 08:11 PM (My Java Tips)
    This post lists some facts about factory methods.

    Factory methods are static methods that return an instance of the native class. Some examples from JDK include :

    • LogManager.getLogManager
    • Pattern.compile
    • Collections.unmodifiableCollection
    • Collections.synchronizeCollection
    • Calendar.getInstance

    Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if ...
  11. Memory Leaks

    by , 05-25-2011 at 11:04 AM (My Java Tips)
    You are aware of the fact that garbage collection collects and removes all the unwanted objects from memory without used intervention. But sometimes, you run into memory leaks problems that demands investigation. I will shed some light on this issue in the next few posts.

    The Java GC process is a low-priority thread and it constantly searches memory for unreachable objects that are not reachable by any live thread. Different JVMs use different algorithms to determine how to collect ...
  12. Subclipse - Checkout

    by , 05-25-2011 at 11:02 AM (My Java Tips)
    Subclipse is a popular SVN plugin for Eclipse. This post is about how to do checkout using Subclipse.

    You will check out code from the trunk or branch of a project. First configure your repositories. Open the SVN Repository Perspective and then open the contents of the repository where your project is located. Locate the trunk/ or branch/ directory in the project and right-click the trunk/ directory or branch/ directory, and choose Checkout from the context-menu. You will be see a ...
  13. Committing code in Subversion

    by , 05-25-2011 at 11:00 AM (My Java Tips)
    Committing means persisting your changes. You may commit one file or multiple files at once.

    Subclipse makes committing easy and simple. When a file is modified, Subclipse displays an icon next to the file name (asterisk) which indicates that your local copy is different from what was in the repository
    when the file was checked out. This helps you know what to commit. Remember, if you create a new file, it will have a different icon(a small question-mark). The "changed" ...
    Tags: subclipse Add / Edit Tags
  14. Using interfaces in APIs

    by , 05-25-2011 at 10:59 AM (My Java Tips)
    When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

    Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

    Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

    Interfaces ...
    Tags: api, interface Add / Edit Tags
    Java SE
  15. Writing deployment descriptor

    by , 05-25-2011 at 10:57 AM (My Java Tips)
    If you writing J2EE applications, you need to deploy your application to J2EE container like JBOSS. I will shed some light on how to write simple deployment descriptors for your web applications.

    I am deploying my JSP application on JBOSS. For JBOSS, we need to put our application in WAR format in deploy folder. So deployment descriptor will have following 2 tasks:

    1. Archieve the application into WAR
    2. Copy that WAR to deploy folder of JBOSS

    Assuming ...
    Tags: j2ee, war Add / Edit Tags
    Java EE , JBoss
  16. Wrapping JDBC Objects

    by , 05-25-2011 at 10:55 AM (My Java Tips)
    In Glassfish connection pool has a property called "Wrap JDBC Objects" which can have value true or false. You may define it in domain.xml file.

    This property is defined as: "When set to true, application will get wrapped jdbc objects for Statement, PreparedStatement, CallableStatement, ResultSet, DatabaseMetaData".

    If you follow JDBC specifications, you will be see that obtaining connection using DataSource.getConnection() or other objects like ...
  17. hibernate.connection.rele ase_mode

    by , 05-25-2011 at 10:49 AM (My Java Tips)
    Hibernate JDBC/Connection's property hibernate.connection.release_mode is used to specify when Hibernate should release JDBC connections.

    A JDBC connection is held until the session is explicitly closed or disconnected by default. For a JTA datasource, one should use after_statement to aggressively release connections after every JDBC call. And for a non-JTA connection, it often makes sense to release the connection at the end of each transaction. will
  18. Hibernate Dialects

    by , 05-25-2011 at 10:46 AM (My Java Tips)
    To set SQL dialect, one has to set the hibernate.dialect property to correct org.hibernate.dialect.Dialect subclass for the database.

    Specify a dialect and then Hibernate will use defaults for some of the properties and this will save the effort of specifying them manually.

    Following dialects are supported.

    • DB2 org.hibernate.dialect.DB2Dialect
    • DB2 AS/400 org.hibernate.dialect.DB2400Dialect
    • DB2 OS390 org.hibernate.dialect.DB2390Dialect
    • PostgreSQL
  19. JSP Tags

    by , 05-25-2011 at 10:32 AM (My Java Tips)
    JSP tags are like HTML tags and they do not use <%. Instead they use < character.

    JSP tags can have a "start tag", a "tag body" and an "end tag". As you may have guessed, the start and end tag both use the tag name, enclosed in < and > characters. Just has HTNL tags, the ending JSP tag starts with a / character after the < character. The tag names have an embedded colon character : in them, the part before the colon describes the type ...
    Tags: jsp tags Add / Edit Tags
  20. XSLT Conditions

    by , 05-25-2011 at 10:30 AM (My Java Tips)
    Consider the scenario: You have xml data and you need to filter some data out. You may do that in Java but its open a good idea to keep the logic separate in XSLT filters.

    if-then-else condition is done the following way in XSLT:

    Java Code:
      <xsl:when test="...">
      <xsl:when test="...">
    Tags: xslt Add / Edit Tags
  21. Javap

    by , 05-24-2011 at 02:58 PM (My Java Tips)
    Javap is shiped with JDK and it really very useful when you want to see what your compiler is doing to your code. It generates source code from a compiled class file.

    Javap is a utility provided in Java Development Kit (JDK) that can be used to improve source code. this is done by generating source code from compiled code and seeing how our code was treated by the Compiler. Later code improvements can be made.

    A very interesting example can be to see how source code ...
  22. ArrayStoreException

    by , 05-24-2011 at 02:48 PM (My Java Tips)
    java.lang.ArrayStoreException is a runtimeException and is thrown when we try to store wrong type of object into an array of objects.


    Java Code:
    ArrayStoreException(String s)
    For example: Storing an Integer object in a Strign array will cause this exception.

    Java Code:
    Object x[] = new String[3];
    x[0] = new Integer(0);
    Since its a runtime exception, compiler won't ...
    Java SE
  23. Finalize

    by , 05-24-2011 at 02:46 PM (My Java Tips)
    All Java classes inherits the finalize() method from java.lang.Object and this method is called by the garbage collector when it determines no more references to the object exist.

    The finalize method performs no actions but it may be overridden by any class. It is overridden to clean-up non-Java resources ie closing a file, closing a connection etc. In overridding finalize() it is a good programming practice to use a try-catch-finally statement and to always call super.finalize(). ...
    Tags: finalize Add / Edit Tags
    Java SE
  24. Writing a file from JSP

    by , 05-24-2011 at 02:45 PM (My Java Tips)
    You may want to write a file from JSP page. For example, user fills a form and you want to persist the details on a text file on server. Now you need to know how to write text file from JSP.

    Remember, always give the path from root. In the given snippet, nameOfTextFile contains the file path. We will use FileOutputStream to write to the file.

    Java Code:
    <%@ page import="*"  %>
    String str = "form data goes here";
  25. Reading files from JSP

    by , 05-24-2011 at 02:44 PM (My Java Tips)
    Working in Servlets/JSP, you might have encountered The javax.servlet.ServletContext API. It provides methods to access resources in general.

    • getResource(String resource): will returns a URL for the resource in the web application.
    • getResourceAsStream(String resource): will returns an InpuStream for the resource in the web application
    • getResourcePaths(String path): will a directory-like listing of all the paths to resources within the web application.

    An example ...
  26. Using Java Beans in JSP

    by , 05-24-2011 at 02:42 PM (My Java Tips)
    JavaBeans are classes written in the Java programming language conforming to a particular convention.

    Consider the scenario: you are writing a JSP and you wish to write pure Java code in it. You may use scriptlets within JSP.

    Java Code:
    // Java code goes here
    Using scriptlets means that your JSP will contains business logic as well which is not a good design approach. Ideally you would like to keep business logic separately ...
  27. Fetching parameters from web.xml

    by , 05-24-2011 at 02:30 PM (My Java Tips)
    The file named web.xml is part of your web applications. You may use it for different purpose. Sometimes you wish to define some parameters for your web application in it. Obviously, you need to use this in your application. I will briefly tell you how to do this.

    At first, let me give you an example. We have a web application that needs to send its id and license code to some authentication server before doing some business operation. The id and license code are static and you may ...

    Updated 05-24-2011 at 02:34 PM by Java Tip

    JSP , Servlet , XML
  28. Stack traces

    by , 05-24-2011 at 02:28 PM (My Java Tips)
    You must have seen stack traces in console window of your Eclipse. Good this is that Java stack traces in the console appear with hyperlinks. This means you can click these to go to the referred place in the code. This helps in problem identification.

    Here is how is works: place the mouse over a line in a stack trace and the pointer changes to the hand. Pres the mouse button and associated Java source file will be opened and the cursor will be placed at the corresponding line. Pressing ...
    Tags: stack Add / Edit Tags
  29. Running code with compile errors

    by , 05-24-2011 at 02:26 PM (My Java Tips)
    While coding in Java using Eclipse, you might have experienced situations where your Java class has some compile errors but stilly you want to run it. The reason might be that the problematic area might be invoked in your execution. What ever is the reason, Ill briefly explain how to execute code with compile errors.

    In Eclipse, you can run and debug code that did not compile cleanly. You cannot do this using command prompt. If line of code with compile error is executed, following ...
    Tags: debug, debugger Add / Edit Tags
  30. Debugger: Code Assist

    by , 05-24-2011 at 02:25 PM (My Java Tips)
    You might have used code assistant while coding in Eclipse. Its really useful and saves time for trivial stuff. Most of us think that code assistant is only available in code writing context. Thatís not right.

    Code assistant is available in debug mode at following:

    • When entering a breakpoint condition,
    • In the Details pane of the Variables & Expressions view,
    • When entering a Detail Formatter code snippet,
    • When entering a Logical Structure code snippet,
    • When
Page 16 of 17 FirstFirst ... 614151617 LastLast