View RSS Feed

All Blog Entries

  1. Socket Programming (Client)

    by , 11-09-2011 at 05:04 PM (My Java Tips)
    I will write a simple socket client, that will connect to the server, send data to the server and well get connection time stamp from the server.


    In the example below, you will learn how to connect to a socket server and how to send and receive data.

    First part is to declare client socket and input/output streams. Then we have to connect to the server using socket. While declaring socket object, we have to mention the host name and the port on which the server is listening. ...
    Categories
    Network Programming
  2. Abstract Classes/Methods

    by , 11-09-2011 at 04:58 PM (My Java Tips)
    The keyword abstract is used when a class is declared abstract. An abstract may or may not include abstract methods. These classes can be inherited (subclasses) but cannot be instantiated.


    Abstract classes comprise of concrete methods and abstract methods. Concrete methods are methods with body and abstract methods are without body methods. The class implementing the abstract class has to override abstract methods.

    Abstract methods are without implementation (without ...
    Categories
    Java SE
  3. Performance Issues (String Concatenation)

    by , 11-09-2011 at 04:55 PM (My Java Tips)
    String concatenation is a task that is overlooked by the programmers. In this post, I will write about the performance issues related to string concatenation.


    Strings are immutable objects and to concatenate strings, java has to perform a lot of operations in the background. Consider the following code segment:

    Java Code:
    String a=  "a";
    String b=  "b";
    String str = a + b;
    This is compiled to

    Java Code:
    String
    ...
    Categories
    Performance
  4. Performance Issues (StringTokenizer)

    by , 11-09-2011 at 04:46 PM (My Java Tips)
    StringTokenizer is a very useful class that is used by developers while parsing text. It works fine and produces the required results.

    Java Code:
    StringTokenizer st = new StringTokenizer(s,",");


    StringTokenizer is very powerful and can handle large sets of delimiters at once. This makes it powerful but a little slow. What if we have a single character delimiter? Should we still use StringTokenizer? If we want to make life easy, the answer is yes. But ...
    Categories
    Performance
  5. Performance Issues (adding element to a Vector)

    by , 11-09-2011 at 04:43 PM (My Java Tips)
    Adding elements to a Vector is a normal practice but there are few performance issues related to this which should be considered.


    Elements can be added to Vector using any of the following:

    - insertElementAt(e, index)
    - addElement(e)
    - add(e)
    - add(index,e)

    The addElement(e) and add(e) methods are used to add elements at the end of Vector whereas insertElementAt(e, index) and add(index, e) methods are used to insert element at any ...
    Categories
    Performance
  6. An Introduction To JUnit

    by , 11-08-2011 at 06:51 PM (My Java Tips)
    In a big project programmer needs to test his code as a unit, Separately from other part of project. So there was a need of some framework which can provide testing for implementing testing in Java.

    JUnit provides a simple way to explicitly test specific areas of a Java program. It can be used to test a hierarchy of program code either as individual or as multiple units. Using JUnit framework is beneficial because it forces us to explicitly declare the expected results of specific ...
    Tags: junit Add / Edit Tags
    Categories
    Java SE
  7. Execute An External Program From Java

    by , 11-08-2011 at 06:49 PM (My Java Tips)
    A Java programmer may need to access the environment under which the application is running. It is supported by “ getRuntime ” method in Runtime Class. Programmer need not create an instance of Runtime class, Instead the Java application has a single instance of class Runtime environment in which the application is running.

    Runtime class has several methods that are useful to execute an external program, getting Information about processors, Memory Information etc.

    ...
    Categories
    Java SE
  8. File Operations In Java

    by , 11-08-2011 at 06:48 PM (My Java Tips)
    Through file handling, we can read data from and write data to files besides doing all sorts of other operations. Java provides a number of methods for file handling through different classes which are a part of the “java.io” package.

    The question can arise in the mind of a new programmer as to why file-handling is required. The answer of this question would be in two parts, why do we need to read data from the files and why do we need to save it (write it) to a file.

    ...
  9. TCP IP Using Socket Programming

    by , 11-08-2011 at 06:46 PM (My Java Tips)
    TCP is a connection-oriented protocol that addresses numerous reliability issues to provide a reliable byte stream. It ensures that the data arrives in-order, duplicate data is discarded and lost/discarded packets are re-sent. It includes traffic congestion control meaning that if there is too much traffic on the network, appropriate measures are taken which include telling the transmitting hosts to slow down the rate at which the data is being sent out and telling the receiving hosts to increase ...
  10. Threads In Java

    by , 11-08-2011 at 06:43 PM (My Java Tips)
    Threads are small processes that are part of a larger process. A single process may have many threads and even if one of these threads is waiting for input from the user or is blocked or is carrying out a lengthy process, the rest of the threads can continue. An example of this can be a word processor which is a process with many threads such as the spell checker, the grammar checker, the user typing area etc.In Java, thread is just the path of execution.
    In Java, threads are created by two ...
    Tags: sleep, thread Add / Edit Tags
    Categories
    Java SE
  11. Tracking Class Loading and Garbage Collection

    by , 11-08-2011 at 06:41 PM (My Java Tips)
    When any Java program runs, In background some libraries are loaded by JVM (Java Virtual Machine) in order to execute the Java program. Java command line tool(”java”) has various options that might be helpful for a Java programmer to know more about the class loading mechanisms.

    For example “ –cp “ option that instructs JVM to find necessary classes other than system classes. An usage is shown below


    java -cp ExampleDir.ExampleTipIf ExampleTip.class is not found in ExampleDir ...
    Tags: command line, jvm Add / Edit Tags
    Categories
    Java SE
  12. How To Modify A JAR File

    by , 11-08-2011 at 06:40 PM (My Java Tips)
    This assumes a basic understanding about the JAR API's provided by Sun, To know more click here. Now this is a step ahead of that, by which you will be able to add or delete a particular file to the JAR archive.

    Listed below is an example that adds a file to an existing JAR or ZIP file. In the program an intermediate file is created first that will be later replaced with an existing one. To be more precise the intermediate file will be renamed to original one after deleting the old ...
    Tags: jar file Add / Edit Tags
    Categories
    Java SE
  13. KeyBoard Input Class

    by , 11-08-2011 at 06:39 PM (My Java Tips)
    Taking input from keyboard involves various steps. Lot of people ask for some ready made class which will serve the purpose. Following class is a good choice. You can use "readInt()", "readDouble()", "readChar()" and "readString()" methods according to your needs.




    Java Code:
    import java.io.*;
    
    public class KeyboardInput {
    
    	static BufferedReader stdin =
    		new BufferedReader( new InputStreamReader(System.in));
    ...
    Categories
    Java SE
  14. Connectiong To MS Access And Fetching Records

    by , 11-08-2011 at 06:37 PM (My Java Tips)
    JDBC (Java Database Connectivity) is designed to allow user to use SQL(Structured Query Language) to query databases. JDBC makes our tasks easy as it handles all low-level concerns about particular database types. It allows you to query an Access database with exactly the same source code as with an Oracle database.

    JDBC is similar to Microsoft's ODBC with the plus point "Platform Independence". To use JDBC, you need to have database driver to communicate with the database. ...
    Tags: jdbc, sql Add / Edit Tags
    Categories
    JDBC
  15. How To List Contents Of A JAR File In A Java Program

    by , 11-08-2011 at 06:35 PM (My Java Tips)
    "jar" command line tool is used to create or list contents of a jar file. In a Java Program it is also possible to edit contents of a jar file. In "java.util.jar" package there are five ways to create a JAR file using JarFile.
    Java Code:
        JarFile(File file) .
        JarFile(File file, boolean verify) .
        JarFile(File file, boolean verify, int mode) .
        JarFile(String name) .
        JarFile(String name, boolean verify) .
    ...
    Tags: jatfile Add / Edit Tags
    Categories
    Java SE
  16. Working With Interfaces

    by , 11-08-2011 at 06:33 PM (My Java Tips)
    Interface is very much similar to the Abstract class in Java but the difference is that in interfaces, members (methods) cannot be implemented, member(fields) defined will be treated as constants.
    A class becomes more formal about its behavior after implementing an interface. Interfaces are actually a contract between the class and the outside world. While implementing an interface in a concrete class, you have to implement all the methods defined by that interface. If you miss any method, ...
    Categories
    Java SE
  17. Enumerations and Iterators

    by , 11-08-2011 at 06:31 PM (My Java Tips)
    An Enumeration is used for iterating over a given collection, Usually of unknown size. Iterator also has the same purpose but Enumeration does not allow modification of the collection, Which can be achieved using Iterator.

    Iterator’s "remove" method removes from the underlying collection the last element returned by the iterator. This method can be called only once per call to the Iterator's "next" method.

    Following code sample will help you understand ...
  18. Difference Between Comparable And Comparator Interface

    by , 11-08-2011 at 06:29 PM (My Java Tips)
    Programmers are usually confused between "Comparable" and "Comparator" interface. Comparable interface has "compareTo" method which is normally used for natural ordering but Comparator interface has "compare" method which takes two arguments. It can be used where you want to sort objects based of more then one parameter. Following example will make it more clear.




    Java Code:
    package test;/*
     ** Use the Collections.sort to
    ...
  19. Taking inputs from users

    by , 11-08-2011 at 06:28 PM (My Java Tips)
    There are a number of ways to take input from user in Java.
    Following code sample helps you in taking input as a character, as a stream and from GUI.
    Required Packages: import java.io and javax.swing packages.



    Java Code:
    // reading character from user
    char a = (char)System.in.read();
    System.out.println("Character read is: " + a);
    
    // taking input from GUI
    String input = JOptionPane.showInputDialog("What is your name?");
    ...
  20. Using Vectors

    by , 11-08-2011 at 06:26 PM (My Java Tips)
    When using arrays, you have to give their size at declaration. So arrays do not expand dynamically. Vectors expand automatically when data is added to them. Vector class is included in java.util package. Vectors are used to store Objects.

    Vector is thread safe. It means Vectors are synchronized, means you can use Vectors even if your object might be shared by two or more threads at a same time. This makes Vector slower than ArrayList which is not thread safe.

    Following ...
  21. Using Arrays in Java

    by , 11-08-2011 at 06:24 PM (My Java Tips)
    Arrays are simply a sequence of memory locations for storing data. The index of an array starts from zero. We have to mention the size of an array while declaring it. Once declared, we cannot change the size at any stage.

    While working with arrays, you will encounter ArrayIndexOutOfBoundsException. This exception is raised if you try to access an element from the array with negative index, or with index greater than the length of index.

    Sorting an array is easy in Java ...
    Tags: array, size fixed Add / Edit Tags
    Categories
    Java SE
  22. Rotating Elements In a List using Collections.rotate Method

    by , 11-08-2011 at 06:22 PM (My Java Tips)
    Collections have very useful methods. Let us see how to rotate objects in a given list. The method "rotate" has two inputs, a list and a distance parameter which defines rotation distance. The distance can be positive, negative or zero. Objects in the list will be rotated by the specified parameter. Let us take an example to rotate objects of Car class.

    For example, suppose list comprises [t, a, n, k, s]. After invoking



    Collections.rotate(list, 1) ...
  23. How to Sort Objects Using Collections.sort method

    by , 11-08-2011 at 06:19 PM (My Java Tips)
    A programmer usually needs to sort a given set of inputs. Java has inbuilt set of API's for sorting. There is a Comparable interface in java that needs to be implemented by class whose object needs to be compared. Java classes like "Integer", "Date" implements this interface, so object of these classes can be sorted easily by Collection's sort method. We can make a class that implements Comparable interface so that it's objects can be sorted by "Collections.sort" method. ...
  24. Java Reflection API - Overview

    by , 11-08-2011 at 06:08 PM (My Java Tips)
    Concept of Java reflection is very powerful. It gives you access to internal information about the classes (sub classes, methods, attributes) loaded into the JVM. This concept is not available in other programming languages like C and C++.

    Consider the following example. I have displayed the complete signatures of “ MyClass " methods using reflection. First of all, you should obtain a java.lang.Class object for the class that you want to manipulate. “ java.lang.Class " is ...
  25. Java Serialization Overview

    by , 11-08-2011 at 06:04 PM (My Java Tips)
    Serialization is the process of saving the state of an object onto any storage medium. Storage medium can be a file or a memory buffer. Then you can transmit it across a network, either in binary form, or in some human-readable text format such as XML. The object in the form of bytes or any other format can be recreated to its internal state easily. The process of serializing an object is also called “ Marshalling ” an object. Extracting a data structure from a series of bytes, is “ Deserialization ...
    Categories
    Java SE
  26. Autoboxing And Unboxing in Java 5

    by , 11-08-2011 at 06:01 PM (My Java Tips)
    Java 5 supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments, method and constructor invocations. This conversion is know as autoboxing.

    Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations.For example:


    Java Code:
    	int inative = 0;
    ...
    Categories
    Java SE
  27. Exception Handling In Java

    by , 11-08-2011 at 05:58 PM (My Java Tips)
    Exception handling is an integral part of programming. In Java exceptions are itself defined as objects in other words throwing an exception is throwing an object. “ Throwable ”, declared in “java.lang package” is the parent class for all the classes that a program can instantiate and throw.


    Basically Throwable has two direct subclasses.

    Exception.
    Error.

    Exceptions (means members of the Exception family) are thrown to indicate abnormal ...
    Categories
    Java SE
  28. Introduction To Collections In Java

    by , 11-08-2011 at 05:56 PM (My Java Tips)
    Framework is a set of classes that acts as a base for building some advanced functionality. It contains super classes having useful functionality and mechanisms which can either be used directly or can be extended in form of subclasses so as to extend the functionality without having to reinvent the basic mechanisms. For example, AWT is a framework for user interfaces.



    The Java collections library is a framework for collection classes. It includes a number of interfaces and ...
  29. More on Collection FrameWork

    by , 11-08-2011 at 05:54 PM (My Java Tips)
    Here we assume that programmer is well aware about the basic concepts of Collections Framework. For more information about basics on Collection Framework click here.
    Now have a look on the classes that implement the " Collection " and " Map " interface.

    As the collection interfaces have a few methods that can be easily implemented from fundamental methods where as abstract classes in the collection framework supply some of these routine implementations. ...
  30. Singleton Pattern

    by , 11-08-2011 at 05:51 PM (My Java Tips)
    A design pattern describes a proven solution to a recurring problem or in other words it is a template for solving problems. Singleton pattern is a creation pattern.

    As the name denotes there will be only an instance of an object, which is created by this pattern. For example: central configuration object should have only one instance that should be accessible from other parts of application. If we have more than one instances of a central configuration object, then different parts ...
    Categories
    Design Patterns , Java SE
Page 40 of 48 FirstFirst ... 303839404142 ... LastLast