View RSS Feed

My Java Tips

  1. JSF (introduction)

    by , 11-13-2011 at 12:31 PM (My Java Tips)
    It has been a problem with open source or community based developments that they provide more options than standards. When we look for java, standard/enterprise java and its development have always remain with standard, but while looking for some web framework in java, we get several names. From MVC based frameworks like Struts, JSF to specialized frameworks like Tapestry, Spring etc. Frameworks with similar and overlapping features making it difficult for a person to choose the best fit. When there ...
  2. Using Treeset

    by , 11-13-2011 at 12:29 PM (My Java Tips)
    TreeSet belongs to Java Collection Framework and is very rare situations. In this post, I will briefly discuss when to use TreeSet and how to use it.

    To use it, you have to import java.util.TreeSet in your class. TreeSet is not synchronized so if a TreeSet is concurrently accessed by threads then thread modifying the contents must be synchronized externally. TreeSet provides following constructors:
    TreeSet(Collection c)
    TreeSet(Comparator c)
    TreeSet(SortedSet ...
  3. Persisting state of objects

    by , 11-13-2011 at 12:24 PM (My Java Tips)
    Serializable interface has no methods and fields so a class that implementing this interface does not have to override any method. The state of objects of serializable class, can be saved and restored later. The state is saved on a container that may be transient (RAM-based) or persistent (disk-based).

    For network communication, the objects that you want to transfer through streams, should be Serializable. For instance, Vector, ArrayList etc implement Serializable interface so their ...
  4. Randomly Accessing Files

    by , 11-13-2011 at 12:12 PM (My Java Tips)
    I have seen a lot of Java guys struggling with accessing a file and playing the data in it. File input output operations are obviously slow as compared to "in memory" operations but then you have persistent data which is of good use. Someone can argue that databases should be used for input and output but then itís a separate debate. Just to keep it short, I would say that sometimes it is better to use text files for input and out.

    In this post, I will write about randomly ...
  5. Multithreading (basics)

    by , 11-13-2011 at 11:59 AM (My Java Tips)
    Multithreading is a concept where a program is broken into two or more parts called threads and all these threads run in parallel. Multithreading can make programs more responsive and effective and it increases its performance too. For example, today web pages need to display animations with sound effects and text at the same time. If this is done by using the traditional single-threaded event loop, the application will take a lot of time to get loaded, and time is a very crucial factor for web ...
    Java SE
  6. Why use multithreading?

    by , 11-13-2011 at 11:55 AM (My Java Tips)
    In this post, I will talk about the importance of multithreading and would like to answer the question: Why use multithreading ??

    Multithreading exploits the fact that most of the time the tasks (parts) of the same program are either waiting for the other resources to become free, or waiting for some timeout to occur. In the above example (spreadsheet), scroll operation is waiting for the calculation to be completed. If these parts or tasks can be described as independent threads, ...
    Java SE
  7. Java Threads

    by , 11-13-2011 at 11:51 AM (My Java Tips)
    A thread is a thread of execution in a program. Java environment has been built around the multithreading model. In fact all Java class libraries have been designed keeping multithreading in mind. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. If a thread goes off to sleep for some time, the rest of the program does not get affected by this. Similarly, an animation loop can be fired that will not stop the working of rest of the system. ...
  8. Thread priorities, synchronization and messaging

    by , 11-13-2011 at 11:47 AM (My Java Tips)
    I assume that you have the basic knowledge of threads. In this post, I will write about thread priorities, synchronization and messaging.

    In multithreading environment, one thread might require the attention of the CPU more quickly than other. In such a case that thread is said to be of high priority. Priority of a thread determines the switching from one thread to another. In other words, priority determines how a thread should behave with respect to the other threads.
  9. main() thread in Java

    by , 11-13-2011 at 11:44 AM (My Java Tips)
    The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.

    Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread ...
    Java SE
  10. Creating a Thread (extending Java Thread Class)

    by , 11-13-2011 at 11:41 AM (My Java Tips)
    There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.

    In case, ...
  11. Creating a Thread (implementing Java Runnable Interface)

    by , 11-12-2011 at 06:58 PM (My Java Tips)
    A better way to create a thread in Java is to implement Runnable interface. A thread can be created by extending Java Thread class also. Now the question arises why implementing Runnable interface is a better approach? Answer is, if the thread class you are creating is to be subclass of some other class, it canít extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads.
  12. Implementing ActionListener interface

    by , 11-12-2011 at 06:54 PM (My Java Tips)
    ActionListener interface is used to perform actions on a performed event. In this post, I will write about its importance and usage.

    ActionListener interface is part of java.awt.event package. It has only one abstract method that has to be implemented in the class implementing this interface.

    Method signature:
    void actionPerformed(ActionEvent e)

    Consider the following scenario: you developed an GUI form for an application comprising of text fields, ...
  13. Creating ZIP files

    by , 11-12-2011 at 06:49 PM (My Java Tips)
    We all use ZIP files in daily routine. They are of great use specially when we want to transfer the files over the network. In this post, I will write about how to create zip files in Java. package provides classes to play with zip files. So first step is to import the package. We will need ZipOutputStream and ZipEntry class from this package. Import the complete package or just these classes - it depends on you. It does not have any affect on the performance.
    Java SE
  14. Using PostgreSQL JDBC driver

    by , 11-12-2011 at 06:45 PM (My Java Tips)
    PostgreSQL is a popular open source object-relational database management system that is being used for small and medium sized applications. We have two options to connect to PostgreSQL. One is to use odbc/jdbc connection and the other is to use PostgreSQL JDBC driver. In this post, I will write about how to use PostgreSQL JDBC driver to connect to PostgreSQL database.

    First of all, you will need PostgreSQL database driver. It can be downloaded from ...
  15. Incorporating native code (C/C++)

    by , 11-12-2011 at 06:42 PM (My Java Tips)
    JNI (Java Native Interface) is a powerful feature that provides the developers to call legacy code written in C/C++ and Java. In the post, I will briefly introduce how JNI helps us in doing so.

    First step is to create a Java class that declares native method. Native methodís signature contains keyword native and it indicates that this method is implemented in another language. Native method declaration is terminated with a semicolon. Also note that native method does not contain implementation. ...
    Java SE
  16. Using PreparedStatement

    by , 11-12-2011 at 06:36 PM (My Java Tips)
    SQL statements are executed on a database tables using Statement object. If the sql statement takes parameter, then it is a wise decision to use PreparedStatement as they are more flexible and makes coding easier.

    Creating SQL statements that take parameters provides flexible statements to work with. We can use the same statement and supply it with different values each time we execute it.

    We use place holders (question marks) in the query, which are supplied values ...
  17. Internationalization

    by , 11-12-2011 at 06:31 PM (My Java Tips)
    Internationalizing Java applications is of great importance if you are planning to develop an application to be used in different parts of the world. In this post, I will try to develop a simple internationalized application.

    Different regions of the world have different languages, currency and time zone. Developing a Java application that can be used throughout the world without much changes is the requirement of this age. Internationalized applications are the solution. Internationalized ...
  18. Creating JAR files

    by , 11-12-2011 at 06:25 PM (My Java Tips)
    JAR (Java Achieve Files) are compressed files that are used to bundle multiple files into a achieve file so the distribution can be managed easily. In this post, I will write about how to create JAR files.

    JDK includes a JAR tool that is used for creating JAR files. Command to use is:
    jar cf jar-file input-file(s)

    c indicates that we want to create a JAR file.
    f indicates that ouput should go to the file
    jar-file is the name of the jar file that ...
    Java SE
  19. Calling stored procedures

    by , 11-12-2011 at 06:21 PM (My Java Tips)
    Stored procedures contain set of SQL statements and are stored on the database server. Their query execution plan is already define, so they run faster and improve the performance. In this post, I will talk about how to call stored procedures.

    An object of CallebleStatement (extends PrepareStatement) is used to execute store procedures. PrepareCall() method is use to create CallebleStatementís object.

    Java Code:
    CallebleStatement cst;
    Cst = conn.prepareCall(ď{call
  20. Simple JSF Application

    by , 11-12-2011 at 06:15 PM (My Java Tips)
    Java Server Faces (JSF) defines three layers of architecture: component architecture, a standard UI widget, and application infrastructure. Where architecture allows standard JSF UI widgets and provide platform for third party components. JSF strictly follow MVC2 pattern, with a more stress on view side. It uses standard jsp with customized tags.

    Here we will develop a simple web application to demonstrate JSF.

    Consider the following jsp file called welcome.jsp
  21. Types of JDBC drivers

    by , 11-11-2011 at 06:44 PM (My Java Tips)
    There are 4 different types of JDBC drivers that can be used in Java database applications. Which driver to use, depends on the requirements and available resources. I will briefly talk about each driver.

    Type 1 is JDBC-OBC bridge driver. It is pure Java and is include in java.sql.*. The client needs ODBC driver manager and ODBC driver for data source. It is ideal in situations, when ODBC driver is available for the database and is already installed on the client machine.
  22. Batch updates

    by , 11-11-2011 at 06:41 PM (My Java Tips)
    Updating a record from Java using executeUpdate() method of Statement object is used commonly by JDBC programmers. Thing to note is that this is a costly activity in terms of time, as a database trip is made. As a programmer/developer, our aim should be to write efficient Java code. In this post, I will write about how you can efficiently make several updates.

    Batch update allows us to execute several update statements in a batch. It means only one database trip will be make, thus ...
  23. Random Access File

    by , 11-11-2011 at 06:38 PM (My Java Tips)
    Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at ...
  24. Data Streams

    by , 11-11-2011 at 06:35 PM (My Java Tips)
    Data streams support binary I/O of primitive data type values (boolean, char, byte, short, int, long, float, and double) as well as String values. All data streams implement either the DataInput interface or the DataOutput interface.

    If you need to work with data that is not represented as bytes or characters then you can use Data Streams. These streams filter an existing byte stream so that each primitive data types can be read from or written to the stream directly.

  25. Buffered Streams

    by , 11-11-2011 at 06:33 PM (My Java Tips)
    When we use unbuffered I/O, each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. To reduce this kind of overhead, the Java platform implements buffered I/O streams.

    Class BufferedReader reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, ...
  26. Character Streams

    by , 11-11-2011 at 06:31 PM (My Java Tips)
    Character Streams handle I/O of character data, automatically handling translation to and from the local character set. All character stream classes are descended from Reader and Writer. As with byte streams, there are character stream classes that specialize in file I/O: FileReader and FileWriter. The Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local ...
  27. Byte Streams

    by , 11-11-2011 at 06:27 PM (My Java Tips)
    Java IO programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.

    There are many byte stream classes. To demonstrate how byte streams work, we'll focus on the file I/O byte streams, FileInputStream and FileOutputStream. Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed.

    A FileInputStream obtains input bytes from a file in a ...
    Tags: byte, java io, streams Add / Edit Tags
  28. System class

    by , 11-10-2011 at 07:23 PM (My Java Tips)
    The System class is a very useful which contains static fields and methods. It cannot be instantiated. In this post, I will briefly write about System class and will explore few interesting methods.

    System class belongs to java.lang package, so it can be used in any Java class without importing any package. System class provides static methods that are very useful. For example to get current time in milliseconds:

    Java Code:
    static long currentTimeMillis()
  29. Introduction to Standard Widget Toolkit

    by , 11-10-2011 at 07:18 PM (My Java Tips)
    AWT (the Abstract Windowing Toolkit) was the first Java GUI toolkit, introduced with JDK 1.0 as one component of the Sun Microsystems Java standard. The relatively primitive AWT wrapped Java code around native (operating system-supplied) objects to create GUI elements, such as menus, windows and buttons. AWT was a very thin wrapper around native widgets, exposing developers to platform specific code, quirks and bugs that limited how portable and native-looking applications could be across different ...
  30. SWT Text

    by , 11-10-2011 at 07:12 PM (My Java Tips)
    Text is a selectable user interface objects that allow the user to enter and modify text. Events associated with Text are: DefaultSelection, Modify, Verify. Different Text styles are CENTER, LEFT, MULTI, PASSWORD, SINGLE, RIGHT, READ_ONLY, WRAP. Please note that only one of the styles MULTI and SINGLE may be specified.

    The following are the important constructors of Text class.

    Text(Composite parent, int style)
    Constructs a new instance of this class given its ...
Page 5 of 15 FirstFirst ... 34567 ... LastLast