View RSS Feed

All Blog Entries

  1. Resolving OutOfMemoryExceptions

    by , 11-02-2011 at 07:13 PM (My Java Tips)
    You might have encountered OutOfMemoryException using Eclipse. This post is about solving this issue.


    OutOfMemoryException is caused by too little heap space for virtual machine. Adjusting the maximum heap space with something like -vmargs -Xmx256m works. This gives your Java apps more space to run and this always helps performance. There are situations where even with an enormous heap size you can still end up with OutOfMemoryException. This means that your VM is not running out ...
    Categories
    Eclipse
  2. Example - HashMap

    by , 11-02-2011 at 07:11 PM (My Java Tips)
    Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated. The HashMap class provides the primary implementation of the map interface. The HashMap class uses a hash table to implementation of the map interface. This allows the execution time of basic operations, such as get() and put() to be constant.


    This code shows the use of HaspMap. In this program HashMap maps the names to account balances.
    ...
    Categories
    Java SE
  3. Unary operators

    by , 11-02-2011 at 07:09 PM (My Java Tips)
    I will present an an example that will show the use of following uniary operators: Unary minus operator, Increment operator, Decrement operator, Logical compliment operator


    Unary plus operator (+) indicates positive value. This (+) operator is used to perform a type conversion operation on an operand. The type of the operand must be an arithmetic data type i.e. if a value of the integer operand is negative then that value can be produced as a positively applying unary plus (+) operator. ...
    Categories
    Java SE
  4. Flushing streams

    by , 11-02-2011 at 07:07 PM (My Java Tips)
    Flushing outputstreams has its own importance. You will realize this when working on socket programming.


    If you do not flush the output stream every time you write something on it, the data may not actually get written out to the socket, and the two programs will keep on waiting for the data forever.

    You can just call flush() after you write something important:

    Java Code:
    // OutputStream out;
    // byte[] data
    out.write(data);
    out.flush();
    ...
    Categories
    Java SE
  5. Loading images

    by , 11-02-2011 at 07:05 PM (My Java Tips)
    You can load GIF, JPEG, and (in SDK 1.3) PNG images using Toolkit's getImage() method:


    Java Code:
    Image i = Toolkit.getDefaultToolkit().getImage("car.png");



    However, Images use lazy data loading, so the data for the image won't start to be loaded until you try to display the image. You can use a MediaTracker to force the data to load, but it's a pain in the butt. An easier solution is to use one of ImageIcon's constructors, which does ...
    Categories
    Java SE
  6. Go to Line

    by , 11-02-2011 at 07:00 PM (My Java Tips)
    Jumping to a specific line is very useful feature. Ecplise provides line nos along the lines of code and you may just to the required line using 'Go to Line' feature.

    Name:  jump-line.PNG
Views: 143
Size:  26.4 KB
    Ctrl + L, enter line number and you are taken to the required line. Of course if the stack trace is in the Eclipse console, you can just click the hyperlink. But if itís in a log file or something, just use this shortcut to go to the line in a sec.

    Name:  jump-line.PNG
Views: 143
Size:  26.4 KB ...
    Categories
    Eclipse
  7. Go to other open editors

    by , 11-02-2011 at 06:56 PM (My Java Tips)
    You can have a lot of files open your Eclipse IDE and may want to switch the file while working. Eclipse provides a fast way of dong that.


    You can cycle through the editors using Ctrl + F6 but its not that useful since the keys are requires you to keep the Ctrl key down. Better way is to use Ctrl + E and then either use the arrow buttons, or type the name of the file youíre editing.

    Name:  editorswitch.PNG
Views: 139
Size:  57.6 KB
    I hope you will find this useful.
    Categories
    Eclipse
  8. Open a type

    by , 11-01-2011 at 07:26 PM (My Java Tips)
    Opening a type for example: a class, an interface) without clicking through interminable list of packages really is a handy feature. The shortcut key combination is: Ctrl + Shift + T.


    If what you want is a Java type, this shortcut will do the trick. Unlike the previous shortcut, this even works when you donít have the Java source file in your workspace for instance: when youíre opening a type from the JDK).

    Name:  typeopen.PNG
Views: 144
Size:  77.8 KB


    try this. ...

    Updated 11-01-2011 at 07:29 PM by Java Tip

    Categories
    Eclipse
  9. Quering an XML document with a fixed XPath expression

    by , 11-01-2011 at 07:22 PM (My Java Tips)
    I will present a complete program to query an XML document with a fixed XPath expression.


    The method can throw several exceptions that must be declared in try- catch blocks. Do proper exception handling.

    Java Code:
    import java.io.IOException;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    
    public class XPathExample {
    
      public static void main(String[]
    ...
    Categories
    XML
  10. Binding a single namespace

    by , 11-01-2011 at 07:16 PM (My Java Tips)
    The XPath expression that finds the titles of all of Laiq's books will be //pre:book[pre:author="Laiq"]/pre:title/text(). However, you have to map the prefix pre to the URI.



    It's a little strange that the Namespace Context interface doesn't have a default implementation in the Java software development kit (JDK) or JAXP, but it doesn't. However, it's not hard to implement. Following simple implementation is for one namespace. You should map the xml prefix as well. ...
    Categories
    XML
  11. Java Naming and Directory Interface

    by , 11-01-2011 at 07:14 PM (My Java Tips)
    I will introduce Java Naming and Directory Interface (JNDI) in this post. Its an API for accessing different kinds of naming and directory services.


    Many people believe that JNDI is specific to a particular naming or directory service, which is not correct. JNDI can be used to access many different kinds of systems including file systems; distributed objects systems (CORBA, Java RMI, and EJB) and directory services like LDAP, Novell NetWare, and NIS+.

    JNDI is similar ...
    Categories
    JBoss
  12. JNDI lookup with a corbaname URL

    by , 11-01-2011 at 07:09 PM (My Java Tips)
    A corbaname can be useful at times as a lookup name. If, for example, the target object is not a member of the federated name space and cannot be located with a qualifiied name, a corbaname can be a convenient way to look up the object.


    A lookup with a corbaname URL follows.

    Java Code:
    // Get the initial context as shown in a previous example.
    ...
    // Look up the home interface using a corbaname URL.
    try {
       java.lang.Object ejbHome = initialContext.lookup(
    ...
    Categories
    JBoss
  13. JNDI - Fixed qualified names

    by , 11-01-2011 at 07:08 PM (My Java Tips)
    If the target object has a cell-scoped fixed name defined for it, you can use its qualified form instead of the topology-based qualified name. Even though the topology-based name works, the fixed name does not change with the specific cell topology or with the movement of the target object to a different server.




    An example lookup with a qualified fixed name follows.


    Java Code:
    // Get the initial context as shown in a previous example.
    // Using the
    ...
    Categories
    JBoss
  14. JNDI - Fixed qualified names

    by , 11-01-2011 at 07:07 PM (My Java Tips)
    If the target object has a cell-scoped fixed name defined for it, you can use its qualified form instead of the topology-based qualified name. Even though the topology-based name works, the fixed name does not change with the specific cell topology or with the movement of the target object to a different server.




    An example lookup with a qualified fixed name follows.


    Java Code:
    // Get the initial context as shown in a previous example.
    // Using the
    ...
    Categories
    JBoss
  15. Deprecated annotation

    by , 11-01-2011 at 07:04 PM (My Java Tips)
    You will learn about deprecated annotation in this post.




    Deprecated is a standard annotation and itís a marker annotation. We use Deprecated to annotate a method that shouldn't be used anymore. Note: Deprecated should be placed on the same line as the method being deprecated.


    Java Code:
    package com.domian.a.test;
    
    public class DeprecatedClass {
    
      @Deprecated public void doSomething() {
        // some code
      }
    ...
    Categories
    Java SE
  16. Override annotation

    by , 11-01-2011 at 07:02 PM (My Java Tips)
    Tiger provides many built-in annotation types and Override is one of them. Override should be used only on methods to indicate that the annotated method is overriding a method in a superclass. An example is presented below:





    Java Code:
    package com.domian.a.test;
    
    public class OverrideTester {
    
      public OverrideTester() { }
    
      @Override
      public String toString() {
        return super.toString() + " [Override Tester
    ...
    Categories
    Annotations
  17. Simple Thread Examples

    by , 11-01-2011 at 06:58 PM (My Java Tips)
    I will present an example that will show start, stop, suspend, and resume threads. It uses the Runnable interface.


    Such threads are useful for things like controlling animation sequences or repeatedly playing audio samples. This example uses a thread that counts and prints a string every second. The thread starts when the applet is initialized. It continues to run until the user leaves the page. If the user returns to the page, the thread continues from where it left off. This allows ...
    Categories
    Java SE
  18. Adding MIME types to JBOSS

    by , 11-01-2011 at 06:51 PM (My Java Tips)
    Consider that you have an application deployed on JBOSS application server. You offer different files in your application that can be opened or downloaded. But someone complains that the web application shows code of the file imbedded in the web page instead of opening or downloading it. What problem can there be?


    Actually you have to tell your JBOSS application server of the supported files. For this, you need to add an entry in web.xml. For instance:


    <mime-mapping> ...

    Updated 11-01-2011 at 06:54 PM by Java Tip

    Categories
    JBoss , XML
  19. LinkedList - insertion/deletion

    by , 11-01-2011 at 06:48 PM (My Java Tips)
    LinkedList has a benefit over ArrayList/Vector when it comes to insert and delete operation. LinkedLists can add and delete any element in the list very efficiently because only the node pointers are to be updated. While talking about the memory consumption, each element/object in a LinkedList takes a bit more memory as compared to ArrayList/Vector because of the pointers involved.



    ArrayList, Vector and LinkedList all have their importance. The important thing is to use them ...
    Categories
    Java SE
  20. Writing/Retriving ArrayList to disk

    by , 11-01-2011 at 06:31 PM (My Java Tips)
    Since ArrayList implements the Serializable interface, therefore ArrayList can be serialized which means it can be persisted on the disk. Following example does exactly that.



    Code example follows:

    Java Code:
    ArrayList arrayList = new ArrayList();
    arrayList.add("Mars");
    arrayList.add("Satrun");
    arrayList.add("Pluto");
    
    FileOutputStream f_out = new FileOutputStream("C:\arraylist.data");
    ...
    Categories
    Java SE
  21. Java IO tasks

    by , 11-01-2011 at 06:28 PM (My Java Tips)
    IO is always a performance issue. I will write about this in this post.


    IO is a performance issue but now its really simple and efficient to do IO related tasks. Always use buffering if you need performance. Because if you directly use FileInputStream, JVM is going to issue file read system call everytime you read a byte of data. In case of buffering, whole buffer is read in one go and only if there's no data in the buffer, JVM issues a system call to read the disk.
    Categories
    Java SE , Performance
  22. Avoiding null pointer exception

    by , 11-01-2011 at 06:26 PM (My Java Tips)
    One should try avoiding null pointer exception. In case of coding an application that needs high degree of reliability, don't take risks. I will present an example.



    Check the following piece of code.


    Java Code:
    private boolean isNullString(String string) {
    return (string.equals(""));
    }
    It seems a normal piece of code. But still there's a hidden trap here. What is the string which is passed is null? So try to rephrase ...
    Categories
    Performance , Java SE
  23. HashMap - useful methods

    by , 11-01-2011 at 06:24 PM (My Java Tips)
    I will present few important methods of HashMap class in this post.


    The most useful methods are: get and put.

    get(Object key) returns the value associated with specified key. It returns null if there is no value for this key.
    put(K key, V value) associates the specified value with the specified key.

    Other useful methods are:

    containsKey(Object key) - (boolean) returns true if this map contains a value for the specified key
    ...
    Categories
    Java SE
  24. xsl output tag

    by , 11-01-2011 at 06:21 PM (My Java Tips)
    As you know, you may apply XSLT transformers and filters on XML documents in Java. To do so, you should know some basic XSLT tags. I will present xsl:output tag in this post.


    It is a top-level element, and must appear as a child node of or . The element defines the format of the output document.

    Example follows:

    <xsl:output
    method="xml|html|text|name"
    version="string"
    encoding="string"
    omit-xml-declaration="yes|no" ...
    Categories
    XML
  25. XML Prolog

    by , 11-01-2011 at 06:15 PM (My Java Tips)
    Developer who are working with XML, should know about XML prolog. This post will brief you about the said topic.


    Its an optional component of the XML document. If you wish to include it, then it must appear before the root element. A prolog consists of two parts:

    Part 1 comprises of XML declaration
    Part 2 comprises of Document Type Declaration (DTD)

    You can include both depending on your needs.

    Example:

    <!--?xml ...
    Categories
    XML
  26. Defining own annotation type

    by , 10-31-2011 at 06:49 PM (My Java Tips)
    The Java language supports defining new annotation types. An annotation type looks similar to an ordinary class, but it has some unique properties. One can use it with the at sign in the classes to annotate your other Java code.


    Its right to say that defining a new annotation type is similar to creating an interface. We have to precede the interface keyword with the @ sign.

    The following example shows the simplest possible annotation type:

    Java Code:
    package com.domain.tiger.a;
    ...
    Categories
    Annotations
  27. Custom annotations - Adding a member

    by , 10-31-2011 at 06:45 PM (My Java Tips)
    Annotation types can have member variables. You will understand its importance when you use annotations for metadata, not just raw documentation. These member variables provides very useful information to code-analysis tools.




    Data members in annotation types are set up to work using limited information. The way to add member is to define a single method that should be named after the member which you want to allow for. The data type should be the return value of the ...
    Categories
    Annotations
  28. Custom annotations - default values

    by , 10-31-2011 at 06:40 PM (My Java Tips)
    I'll focus on setting some default values for the annotation.


    Yes, you want the users to specify values for the annotations but only if their values differ some default values. Default values should be provided to make things easier.

    The following example shows how one can associate default values with custom annotations.

    Java Code:
    package com.domain.tiger.a;
    
    // Annotation type with default values
    public @interface GroupTODO {
    ...
    Categories
    Annotations
  29. Annotating an annotation

    by , 10-31-2011 at 06:32 PM (My Java Tips)
    I will briefly write about annotating annotations. You should have good idea about the set of predefined annotation types we have seen in the last few posts.


    When you start writing your own annotation types, the main purpose of your annotation types is to provide basic documentation. Also you write annotation types that are specific to a certain member type,
    or perhaps a certain set of member types. This requires you to supply some sort of metadata on your annotation type, ...
    Categories
    Annotations
  30. Adding public documentation

    by , 10-31-2011 at 06:24 PM (My Java Tips)
    Documented is a meta-annotation and its a marker annotation. A marker annotations have no member variables.


    Documented annotation is used to indicate that an annotation should appear in the Javadoc for a class. Remember that the default behavior is that annotations are not included in Javadocs. The presented example shows what the Documented meta-annotation looks like in use:

    Java Code:
    package com.domain.tiger.a;
    
    import java.lang.annotation.Documented;
    ...
    Categories
    Annotations
Page 44 of 48 FirstFirst ... 344243444546 ... LastLast