View RSS Feed

Java SE

Java Standard Edition

  1. 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.

    ...
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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 ...
  9. 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
    ...
  10. 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?");
    ...
  11. 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 ...
  12. 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
  13. 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) ...
  14. 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. ...
  15. 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 ...
  16. 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
  17. 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
  18. 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
  19. 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 ...
  20. 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. ...
  21. 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
  22. Object Pointer In Java

    by , 11-08-2011 at 05:43 PM (My Java Tips)
    Pointer in C/C++ denotes to the variables saving the reference, In Java there is only call by reference. But here the pointer refers to an object which itself has another reference within it.

    Java classes can have instance variables. If an instance variable is of class type, then it can hold instance of the class in it. This is called pointer to the object.

    In some cases, we need an object that contains an instance variable that can refer to another object of the same ...
    Categories
    Java SE
  23. How to remove an element from a collection

    by , 11-08-2011 at 05:40 PM (My Java Tips)
    Collections in Java include so many interesting features. As the number of features increase there arise the possibility of incorrect usage. Here we discuss such an issue while removing an object from a Collection. Exception throw is "Concurrentmodificationexception" .

    Code:




    Java Code:
    package test;import java.util.ArrayList;
    
    import java.util.Iterator;
    
    import java.util.List;
    
    public class ListExample
    ...
    Categories
    Java SE
  24. Using == or equals() for string comparison

    by , 11-08-2011 at 05:34 PM (My Java Tips)
    Mostly new Java programmers get confused when comparing strings. You can check the equality of strings either by using "==" or "equals()". But one should know when to use each of these in order to get the required results.

    First, lets talk about "equals() " method. Method "equals()" is included in the class java.lang.Object which every Java class inherit. "equals()" method actually compares the characters that make up String object. ...
    Categories
    Java SE
  25. Implementing Clone Method For HashMap in Java

    by , 11-08-2011 at 04:25 PM (My Java Tips)
    Method "clone"creates and returns a copy of the object. It makes another object of the class in memory. Suppose, We are creating a clone of the object x then x.clone() != x will return true but it is not necessary that x.clone().equals(x) will return true. It depends upon the implementation of class.
    Class should implement "Cloneable" interface in order to make clone of its objects. Otherwise "CloneNotSupportedException" will be thrown. Array,HashMap are considered ...
    Categories
    Java SE
  26. Using Instanceof operator in java

    by , 11-08-2011 at 04:16 PM (My Java Tips)
    Good Java programmers use "instanceof" operator since it is a must to escape from "Class Cast Exceptions".

    Following are the key points to be remembered while using instanceof operator

    Instanceof operator is used to restore full functionality of an object.
    While programming many times heterogeneous collection are used and then to invoke full functionality of an object casting is required then to check the type of instance, "instanceof" ...
    Categories
    Java SE
  27. Correct way to override equals method in Java

    by , 11-08-2011 at 04:01 PM (My Java Tips)
    Though it is very simple to override "equals" method for a class. But people usually make mistakes in overriding this method. It is very important for a Java developer to know about this method.

    Some Important points to remember

    Always remember to override "hashCode" method if you are overriding "equals" method otherwise you may face problem while using your class with HashTable, HashMap like classes as they uses "hashCode" ...
    Categories
    Java SE
  28. Defining annotations

    by , 11-07-2011 at 06:54 PM (My Java Tips)
    It is right to say that a typical application programmer won't need to define annotations in normal routine. But still, one should know how to define annotations.


    Annotation type declarations are very much like normal interface declarations. An at-sign (@) comes before the interface keyword and each method declaration defines an element of the annotation type. Method declarations should not have any parameters or a throws clause. Also note that the return type should be amoung the ...
    Categories
    Java SE
  29. Path vs ClassPath

    by , 11-07-2011 at 06:47 PM (My Java Tips)
    I have found may people asking the difference between ClassPath and path. They tend to mix these and find this confusing. I will address this issue in this post.


    Path is simply a system wide variable that tells where to find your commands. Like you can run directory command (dir) from an path. This is so because Windows path is set in the path variable. To see the contents of path variable, use following command on command prompt:


    echo %PATH%

    ...
    Categories
    Java SE
  30. Creating MessageDigest

    by , 11-07-2011 at 06:42 PM (My Java Tips)
    Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. Thing to remember is that it I s not any encryption technique. It’s simply hashing. Same message will always generate the same hash code and a slight change in the message will generate completely different hash code. In the next few posts, I will talk about how we can generate message digests in our Java programs.


    The popular message digest algorithms are MD5 and ...
    Categories
    Java SE
Page 3 of 5 FirstFirst 12345 LastLast