View RSS Feed

All Blog Entries

  1. Using custom Ant build script for Eclipse projects

    by , 02-05-2012 at 06:25 AM
    Ant is XML-based script for building Java applications. Ant is popularly used in Java development because of its powerful and flexibility. It makes developer’s life easier by automatically processing repetitive tasks that happen before, during and after building of Java applications. Eclipse is a great IDE which supports Ant by integrating Ant’s libraries as its plug-in.

    Generally, you may not deal with any Ant script during development of a Java project in Eclipse. However if your ...
    Tags: ant, eclipse Add / Edit Tags
    Categories
    Eclipse Ant
  2. Working with Java Servlets in Eclipse

    by , 02-04-2012 at 06:56 PM
    Servlet is a Java program that runs on a web server to handle requests and returns response to clients. It is widely used for Java enterprise development, and Eclipse is one of the best IDE for Java development. So this article will walk you through a Java project using Eclipse IDE to create a web application that plays around with a Java servlet.

    Set up Eclipse development environment
    The following software programs are required to be installed on your computer:
    ...
  3. Initializing

    by , 02-01-2012 at 07:23 PM
    Java Code:
    package myPackage;
    
    public class SprinklerSystem 
    {
    	private String valve1;
    	private int valveStrength;
    	private WaterSource source = new WaterSource();
    	
    	static 
    	{
    		System.out.println("You are in Static block!");
    	}
    
    	{
    		System.out.println("You are in initi block!");
    		this.valve1 = "Valve1 ";
    	}
    	
    
    	
    	SprinklerSystem()
    ...
    Categories
    Uncategorized
  4. Casting with Class: Class.

    by , 01-28-2012 at 09:21 PM
    Java Code:
    public class ClassCasts 
    {
    	
    	public static void main(String args[])
    	{
    		Building b = new House();
    		Class<House> houseType = House.class;
    		House h = houseType.cast(b);
    		System.out.println(b.getClass());
    	}
    
    }
    Categories
    Uncategorized
  5. Using of Class Class. An Example:

    by , 01-28-2012 at 11:30 AM
    Java Code:
    package myPackage1;
    
    interface HasBatteries{};
    interface Waterproof{};
    interface Shoots{};
    
    class Toy
    {
    	Toy(){};
    	Toy(int i){};
    }
    
    class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots
    {
    	FancyToy()
    	{
    		super(1);
    	}
    }
    
    public class ToyTest 
    {
    	static void printInfo(Class cc)
    	{
    		System.out.println("Class name:
    ...
    Categories
    Uncategorized
  6. Reading a Files first line with a simple gui application

    by , 01-16-2012 at 12:12 AM
    Java Code:
    package myPackage;
    
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class FileOrganizer extends JFrame implements ActionListener 
    {
    	public
    ...
    Categories
    Uncategorized
  7. Reading Byte From a Text File and Printing To Console Character And Byte Value

    by , 01-14-2012 at 12:12 PM
    Java Code:
    public static void main(String[] args) 
    	{
    		File myText = new File("c:/myDir/myText.txt");
    		
    		try {
    			InputStream myByteReader = new FileInputStream(myText);
    			System.out.println("Available total bytes:" + myByteReader.available());
    			int firstByte = myByteReader.read();
    			System.out.println("First byte:" + firstByte);
    			System.out.println("Char of first byte:" + (char) firstByte);
    ...
    Categories
    Uncategorized
  8. Reading Bytes from a Text File

    by , 01-14-2012 at 12:48 AM
    Java Code:
    package myPackage;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilterOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.io.Serializable;
    ...
    Categories
    Uncategorized
  9. Writing an Array Object to a Binary File and Reading Back

    by , 01-14-2012 at 12:03 AM
    Java Code:
    package myPackage;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilterOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.io.Serializable;
    ...
    Categories
    Uncategorized
  10. Minimize memory leaks in Java

    by , 01-08-2012 at 01:46 PM
    The main causes for memory leaks in java are poor design with invalid references. Garbage collector is not able to claim those objects.

    Following steps help you to detect memory leaks in your application.

    • Use tools like JProbe, OptimizeIt.
    • Use operating system process monitors.
    • Override totalMemory() and freeMemory() methods in the Java Runtime class.

    Following steps help you to minimize memory leaks in your application.
    ...
    Categories
    Memory
  11. Performance improvement of a Java application

    by , 01-08-2012 at 01:45 PM
    Follow these steps to improve performance of your Java application.

    • Always Pool valuable system resources like threads, database connections, socket connections.
    • Optimize your I/O operations by using buffering when writing to and reading from files and/or streams.
    • Minimize network overheads by retrieving several related items simultaneously in one remote invocation if possible.
    • Apply the following performance tips in your code

    • Use ArrayLists,
    ...
  12. Signed Applet

    by , 01-08-2012 at 01:42 PM
    Java applets are contained within a “sandbox” by default and due to security reason. A signed Applet is a trusted Applet. This signed Applet can perform any action within sandbox and outsides it boundaries. For example, reading, writing and deleting file on local drives or accessing different system parameters.

    Earlier versions of browsers have no support for signed Applet and do not allow operations outside sandbox. To sign an Applet, applet’s code must be digitally singed with digital ...
    Categories
    Java Applet , Tutorial
  13. Java Applet

    by , 01-08-2012 at 01:41 PM
    Initialization code of the Java applet is written in init() method. It is just like a constructor for an applet.
    Java applet contains the following life cycle methods.

    init() : This method is used to initialize an applet and only once called in applet life cyle by the browser.

    start() : This method is called after init() and contains code to start processing. Start() method is called again, if user leaves current page without killing session.
    ...
    Tags: applet Add / Edit Tags
    Categories
    Java Applet , Tutorial
  14. Swing delegation event model

    by , 01-08-2012 at 01:38 PM
    Registered event listeners are notified for the event received by objects in swing delegation event model. Event receiver is a component in most cases.

    Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc.
    Event Processors: JButton, JList etc.
    EventListeners: ActionListener, ComponentListener, KeyListener etc.

    Name:  62.JPG
Views: 3086
Size:  15.8 KB

    Swing delegation event model
    Categories
    Events
  15. Layout Managers

    by , 01-08-2012 at 01:35 PM
    FlowLayout: Applet and Panel has FlowLayout as their default layout. It lays components from left to right.

    BorderLayout: Frame and Dialog has BorderLayout as their default layout. It lays components in north, south, east, west and center.

    CardLayout: stack of same size components arranged inside each other.

    GridLayout: Makes a bunch of components equal in size and displays them in the requested number of rows and columns.

    GridBagLayout: ...
    Categories
    Layout
  16. JTable MVC

    by , 01-08-2012 at 01:34 PM
    Swing uses a modified version of Model View Controller design pattern. Model, view and controller layers are separated by MVC design pattern.

    Name:  62.JPG
Views: 1791
Size:  19.7 KB

    Swing MVC Architecture

    • Component (JTable, JTree, and JList): These components coordinate.
    • Model (TableModel): charged with storing the data.
    • UIDelegate: It is responsible for getting the data from model and rendering it to screen.
    Tags: jtable, mvc Add / Edit Tags
    Categories
    Architecture , JTable
  17. Swing Hierarchy

    by , 01-08-2012 at 01:31 PM
    All the Swing components start with ‘J’. Following diagram shows the swing hierarchy. JComponent is the base class for all components.

    Name:  62.JPG
Views: 5089
Size:  42.4 KB

    Swing Hierarchy
    Categories
    Architecture
  18. Swing Action architecture

    by , 01-08-2012 at 01:28 PM
    Shared behavior between different user interfaces components are implemented by “Swing Action Architecture”. Like tool bar buttons and menu items perform same actions. All the components are disabled when an action is disabled.

    Java Code: Swing Action Architecture
    class FileAction extends AbstractAction {
    //Constructor
    FileAction(String name) {
    super(name);
    }
    public void actionPerformed(ActionEvent ae){
    //add action logic here
    }
    }
    JMenu fileMenu
    ...

    Updated 01-14-2012 at 07:56 PM by Java Swing

    Categories
    Actions , Architecture
  19. AWT vs. Swing

    by , 01-08-2012 at 01:23 PM
    Swing contains a rich set of components as compared to AWT. They are purely build in Java and has following benefits as compared to AWT.

    • Swing contains extra components like JTree, JTable etc.
    • They can change skin based on current look and feel.
    • They follow the MVC design pattern and are more flexible.
    • Swing provided light weight components.
    • Swing provides double buffering.
    • Swing provides paint debugging.

    Swing has following ...
    Tags: awt, swing Add / Edit Tags
    Categories
    Architecture
  20. Java Sockets

    by , 01-08-2012 at 01:21 PM
    Sockets provided communication channeled and support inter process communication in Java. For example to communicate between two JVMs, sockets are used. Socket is an end point for communication. There are two types of sockets.

    • Connection less sockets
    • Connection oriented sockets

    UDP protocol is used in the connection less sockets communication. Also they are referred as datagram communication protocol.

    Each socket is indentified with following ...
    Tags: java sockets Add / Edit Tags
    Categories
    Tutorial
  21. Thread communication

    by , 01-08-2012 at 01:18 PM
    Following methods are used to perform communication between threads.

    • Wait()
    • Notify()
    • notifyAll()

    In the following code, count is a shared variable between different threads. It’s a consumer producer process in which consumer waits in consume() method and waits for producer to perform its action.

    Name:  62.JPG
Views: 527
Size:  26.1 KB

    Thread communication
    Categories
    Tutorial
  22. High-level thread states

    by , 01-08-2012 at 01:16 PM
    Following diagrams display different thread states.

    Name:  62.JPG
Views: 832
Size:  29.7 KB

    Thread States

    • Runnable: Thread in this state is waiting for its turn to start execution.
    • Running: Thread is executing and performing its functionality.
    • Waiting: Thread is in blocked state and waiting for some operation to finish.
    • Sleeping: Thread is suspended forcefully.
    • Blocked on I/O: Thread will start execution after completion of ...
    Tags: states, thread Add / Edit Tags
    Categories
    Tutorial
  23. Thread Creation

    by , 01-08-2012 at 01:12 PM
    Threads can be created using one of the following methods.

    • By Extending Thread class.
    • By Implementing Runnable interface.

    Java Code: Thread creation
    class Counter extends Thread {
    //method where the thread execution will start
    public void run(){
    //logic to execute in a thread
    }
    //let’s see how to start the threads
    public static void main(String[] args){
    Thread t1 = new Counter();
    Thread t2 = new Counter();
    ...

    Updated 01-14-2012 at 07:52 PM by Thread

    Categories
    Tutorial
  24. Processes vs Threads

    by , 01-08-2012 at 01:10 PM
    Thread is a single execution in a process as compared to process which is an execution of a program. A process may have multiple threads within it. Also a thread is sometime referred as lightweight process. All the threads in JVM share heap that belongs to the process. In this case, different threads can access same object. Threads always share the heap and contain their own stack to store local variables. In this way threads are kept thread-safe and are synchronized.

    Name:  62.JPG
Views: 4308
Size:  26.7 KB
    ...
    Categories
    Tutorial
  25. User defined exception

    by , 01-08-2012 at 01:01 PM
    You can customize and write your own user defined exceptions. Following class extends the Exception class and writes a new “MyException” which is thrown at some point in your code.

    Java Code: This is an example of user defined exception
    public class MyException extends Exception {
    /* class definition of constructors goes here */
    public MyException() {
    super();
    }
    public MyException (String errorMessage) {
    super (errorMessage);
    }
    }

    Updated 01-14-2012 at 07:48 PM by Java Exception

    Tags: exception Add / Edit Tags
    Categories
    Tutorial
  26. Exception handling best practices

    by , 01-08-2012 at 12:58 PM
    Following are the best practices when catching and handling exceptions in your java code.

    • Do not catch “Exception” directly in your code. Java exception handling is not polymorphic. If you catch Exception, it can catch IOException or any other descendent type.

    Name:  62.JPG
Views: 1077
Size:  35.3 KB

    Catching Exceptions

    • Exception handling supports the stack trace of any error in your code. Always throw an exception early in your code to make this ...
  27. Java error handling

    by , 01-08-2012 at 12:56 PM
    When a hard failure or link failure occurs in virtual machine, JVM throws an Error. These Errors are not caught by the typical java programs.

    When a problem occurs in the code, an Exception is thrown and is caught by the program explicitly. Base class of all Exception classes is Exception class. All other are derived classes. Below image shows a complete hierarchy of the exceptions in java.

    Name:  62.JPG
Views: 1216
Size:  16.3 KB

    Throwable and subclasses

    ...
  28. Java garbage collector

    by , 01-08-2012 at 12:53 PM
    A new object is stored into heap when it is created by the Java Program. Heap is also call garbage collectable heap and it can be released by the garbage collector. Garbage collector is a JVM low priority thread and its execution cannot be forced. It is executed to release memory allocated to unused object. Also you can ask garbage collector to release memory by using System.gc()

    Object is unreachable when it has no other object that is referencing to this object. In such cases this ...
  29. Type casting

    by , 01-08-2012 at 12:50 PM
    When you treat a variable of one type to another type, it is called type casting. Up casting supports automatic conversion but in case of low casting you need to explicitly specify it.

    Java Code: Type Casting
    byte -> short -> int -> long -> float -> double
    int i = 5;
    long j = i; //Right. Up casting or implicit casting
    byte b1 = i; //Wrong. Compile time error “Type Mismatch”.
    byte b2 = (byte) i ; //Right. Down casting or explicit casting is required.
    ...

    Updated 01-14-2012 at 07:46 PM by Core Java

    Categories
    Casting
  30. Outer and Inner classes

    by , 01-08-2012 at 12:47 PM
    All the classes in Java are not defined separately from other classes. You can also write one class into another class. The inside class is referred as inner class and the class containing this inner class is called outer class. Inner class is a member of outer class similar to other attributes and methods.

    Avoid using inner classes when they are not required. It is hard to maintain and reuse code with inner classes. Only use it when it is required and relevant. They are used usually ...
    Categories
    Classes
Page 26 of 48 FirstFirst ... 16242526272836 ... LastLast