View RSS Feed

All Blog Entries

  1. Why use multithreading?

    by , 11-13-2011 at 10: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
  2. Java Threads

    by , 11-13-2011 at 10: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. ...
  3. Thread priorities, synchronization and messaging

    by , 11-13-2011 at 10: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.
  4. main() thread in Java

    by , 11-13-2011 at 10: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
  5. Creating a Thread (extending Java Thread Class)

    by , 11-13-2011 at 10: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, ...
  6. Creating a Thread (implementing Java Runnable Interface)

    by , 11-12-2011 at 05: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.
  7. Implementing ActionListener interface

    by , 11-12-2011 at 05: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, ...
  8. Creating ZIP files

    by , 11-12-2011 at 05: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
  9. Using PostgreSQL JDBC driver

    by , 11-12-2011 at 05: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 ...
  10. Incorporating native code (C/C++)

    by , 11-12-2011 at 05: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
  11. Using PreparedStatement

    by , 11-12-2011 at 05: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 ...
  12. Internationalization

    by , 11-12-2011 at 05: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 ...
  13. Creating JAR files

    by , 11-12-2011 at 05: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
  14. Calling stored procedures

    by , 11-12-2011 at 05: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
  15. Simple JSF Application

    by , 11-12-2011 at 05: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
  16. Types of JDBC drivers

    by , 11-11-2011 at 05: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.
  17. Batch updates

    by , 11-11-2011 at 05: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 ...
  18. Random Access File

    by , 11-11-2011 at 05: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 ...
  19. Data Streams

    by , 11-11-2011 at 05: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.

  20. Buffered Streams

    by , 11-11-2011 at 05: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, ...
  21. Character Streams

    by , 11-11-2011 at 05: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 ...
  22. Byte Streams

    by , 11-11-2011 at 05: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
  23. System class

    by , 11-10-2011 at 06: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()
  24. Introduction to Standard Widget Toolkit

    by , 11-10-2011 at 06: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 ...
  25. SWT Text

    by , 11-10-2011 at 06: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 ...
  26. SWT Menu

    by , 11-10-2011 at 06:10 PM (My Java Tips)
    Menu are user interface objects that contain menu items. The Menu support styles BAR, DROP_DOWN, POP_UP, NO_RADIO_GROUP, LEFT_TO_RIGHT, RIGHT_TO_LEFT. Please note that only one of BAR, DROP_DOWN and POP_UP may be specified. Only one of LEFT_TO_RIGHT or RIGHT_TO_LEFT may be specified.

    The following are the important constructors of Menu class.

    Menu(Control parent)
    Constructs a new instance of this class given its parent, and sets the style for the instance so ...
    Tags: menu, menu items, swt Add / Edit Tags
    SWT / JFace
  27. SWT List

    by , 11-10-2011 at 06:07 PM (My Java Tips)
    List is a selectable user interface object that displays a list of strings and issues notification when a string selected. A list may be single or multi select. Events associated with List are Selection, DefaultSelection. Please note that only one of SINGLE and MULTI style may be specified.

    The following are the important constructors of List class.

    List(Composite parent, int style)
    Constructs a new instance of this class given its parent and a style value describing ...
    SWT / JFace
  28. SWT Label

    by , 11-10-2011 at 06:02 PM (My Java Tips)
    A label displays a string or an image. The string can be changed by the application, but a user cannot edit it directly. For Label, when SEPARATOR is specified, displays a single vertical or horizontal line.

    The following are the important constructors of Label class.

    Label(Composite parent, int style)
    Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

    The following are the important ...
    Tags: label, separator, swt Add / Edit Tags
    SWT / JFace
  29. SWT Group

    by , 11-10-2011 at 05:59 PM (My Java Tips)
    A group is also a control that can contain other controls. A group is surrounded by a border and may, optionally, contain a title. As with composites, the position of each control inside a group is relative to the group, so if the group is moved on the shell, the controls inside the group retain their relative positions.

    Group provide an etched border with an optional title. Shadow styles are hints and may not be honored by the platform. To create a group with the default shadow style ...
    Tags: control, group, swt Add / Edit Tags
    SWT / JFace
  30. SWT Dialog

    by , 11-10-2011 at 05:57 PM (My Java Tips)
    Dialog class is the abstract superclass of the classes that represent the built in platform dialogs. A Dialog typically contains other widgets that are not accessible. A Dialog is not a Widget.

    This class can also be used as the abstract superclass for user-designed dialogs. Such dialogs usually consist of a Shell with child widgets. The basic template for a user-defined dialog typically looks something like this:

    Java Code:
     import org.eclipse.swt.*;
     import org.eclipse.swt.widgets.*;
    Tags: dialog, subclass, swt Add / Edit Tags
    SWT / JFace
Page 7 of 17 FirstFirst ... 56789 ... LastLast