View RSS Feed

All Blog Entries

  1. Working with Java Servlets in Eclipse

    by , 02-04-2012 at 05: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:
    ...
  2. Initializing

    by , 02-01-2012 at 06: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
  3. Casting with Class: Class.

    by , 01-28-2012 at 08: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
  4. Using of Class Class. An Example:

    by , 01-28-2012 at 10: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
  5. Reading a Files first line with a simple gui application

    by , 01-15-2012 at 11:12 PM
    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
  6. Reading Byte From a Text File and Printing To Console Character And Byte Value

    by , 01-14-2012 at 11:12 AM
    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
  7. Reading Bytes from a Text File

    by , 01-13-2012 at 11:48 PM
    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
  8. Writing an Array Object to a Binary File and Reading Back

    by , 01-13-2012 at 11:03 PM
    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. Minimize memory leaks in Java

    by , 01-08-2012 at 12: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
  10. Performance improvement of a Java application

    by , 01-08-2012 at 12: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,
    ...
  11. Signed Applet

    by , 01-08-2012 at 12: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
  12. Java Applet

    by , 01-08-2012 at 12: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
  13. Swing delegation event model

    by , 01-08-2012 at 12: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: 2761
Size:  15.8 KB

    Swing delegation event model
    Categories
    Events
  14. Layout Managers

    by , 01-08-2012 at 12: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
  15. JTable MVC

    by , 01-08-2012 at 12: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: 1604
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
  16. Swing Hierarchy

    by , 01-08-2012 at 12: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: 4341
Size:  42.4 KB

    Swing Hierarchy
    Categories
    Architecture
  17. Swing Action architecture

    by , 01-08-2012 at 12: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 06:56 PM by Java Swing

    Categories
    Actions , Architecture
  18. AWT vs. Swing

    by , 01-08-2012 at 12: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
  19. Java Sockets

    by , 01-08-2012 at 12: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
  20. Thread communication

    by , 01-08-2012 at 12: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: 503
Size:  26.1 KB

    Thread communication
    Categories
    Tutorial
  21. High-level thread states

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

    Name:  62.JPG
Views: 788
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
  22. Thread Creation

    by , 01-08-2012 at 12: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 06:52 PM by Thread

    Categories
    Tutorial
  23. Processes vs Threads

    by , 01-08-2012 at 12: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: 3799
Size:  26.7 KB
    ...
    Categories
    Tutorial
  24. User defined exception

    by , 01-08-2012 at 12: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 06:48 PM by Java Exception

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

    by , 01-08-2012 at 11:58 AM
    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: 977
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 ...
  26. Java error handling

    by , 01-08-2012 at 11:56 AM
    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: 1092
Size:  16.3 KB

    Throwable and subclasses

    ...
  27. Java garbage collector

    by , 01-08-2012 at 11:53 AM
    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 ...
  28. Type casting

    by , 01-08-2012 at 11:50 AM
    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 06:46 PM by Core Java

    Categories
    Casting
  29. Outer and Inner classes

    by , 01-08-2012 at 11:47 AM
    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
  30. Java stack and heap memory

    by , 01-08-2012 at 11:43 AM
    Heap is used to store new objects created by Java. Stack is used to store primitive data types like int and double when they are declared locally. These primitive data types are stored on heap when they are declared globally. Whenever a Java method is called, all the local variables are pushed to stack and its pointer is decremented after completion of that method call. In a multithreaded application only one heap is used and each thread contains its own stack. So do not declare your data globally ...
    Categories
    Stack
Page 26 of 48 FirstFirst ... 16242526272836 ... LastLast