View RSS Feed

Java SE

Java Standard Edition

  1. java.util.Locale

    by , 10-30-2011 at 06:27 PM (My Java Tips)
    Locale object deals with the geographical details. Different parts of the world have different languages, currencies and time zones. There are some operations that are different for different geographical areas. These are referred as locale-sensitive operations. These operations uses Locale to tailor the information according to the needs.

    Representing numbers is a locale-sensitive operation as formatting of number is different in different parts of the world. The example below prints ...
    Java SE
  2. Logical Operators

    by , 10-30-2011 at 06:08 PM (My Java Tips)
    Following are logical operators that one can use in Java programming.

    ! Is not…
    ^ Exclusive Or…
    | Or… (bitwise)
    & And… (bitwise)
    || Or… (short cut, see &&)
    && And

    In this Java programming tutorial, I will code And, Or and Not operators as they are commonly used.

    Java Code:
    int a= 15;
    int b = 25;
    if(a<10 || b>10)
    System.out.println("Entered: Java
    Java SE
  3. ThreadLocal

    by , 10-29-2011 at 08:36 PM (My Java Tips)
    I will present an example that shows how to start, stop, suspend, and resume threads.

    It uses the Runnable interface because it extends from Applet class and multiple inheritance is not allowed. This example uses a thread that counts and prints a string every second. The thread starts when the applet is initialized. It continues to run until the user leaves the page. If the user returns to the page (and all is well), the thread continues from where it left off. This allows applets ...
    Glassfish , Java SE
  4. The Thread Class

    by , 10-29-2011 at 08:29 PM (My Java Tips)
    java.lang.Thread is used to create and control threads. A new instance of this class must be created to use a new thread. Once a thread is created, it does not start running right away. Thread.start() must be called to actually make the thread run. On calling Thread.start(), the thread begins executing in the run() method of the target class.

    Remember that a new Thread class always starts running the public void run() method of a class. There are two ways to create a thread:

    Updated 10-29-2011 at 08:32 PM by Java Tip

    Java SE , Java ME
  5. Java Threads - synchronized

    by , 10-28-2011 at 08:27 PM (My Java Tips)
    In multithreaded environment, data may be corrupted. A was to prevent this is to prevent the interruption of critical regions.

    You have to fidn the critical region in your code which you think should be accessed by one thread at a time. Java's synchronized is used to ensure that only one thread is in a critical region at once. When the thread enters a synchronized code block, it locks on that region and no other thread can enter the critical region as long as the lock is released. ...
    Java SE
  6. Finally Block

    by , 10-25-2011 at 08:17 PM (My Java Tips)
    You must be using try catch block in your programming practices. The finally block is used to ensure resources are recovered regardless of any problems that may occur.

    Remember that there are several different ways of using the finally block and it depends on how exceptions are handled. Let me introduce the first style. If a method throws all exceptions, then it may use a finally with no catch :

    Java Code:
    public final class SimpleFinally

    Updated 10-25-2011 at 08:36 PM by Java Tip

    Java SE
  7. Static/Non static methods - Example

    by , 06-02-2011 at 08:12 PM (My Java Tips)
    I will present an example to show the difference between static and non-static methods.

    Go through the example below:

    Java Code:
    public abstract class AbstractExample {
       public static void foo() {
          System.out.println("Static foo called.");
       public void bar() {
          System.out.println("Non-static bar called.");
    public class AbstractCaller extends AbstractExample {
    Java SE
  8. Using interfaces in APIs

    by , 05-25-2011 at 10:59 AM (My Java Tips)
    When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

    Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

    Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

    Interfaces ...
    Tags: api, interface Add / Edit Tags
    Java SE
  9. ArrayStoreException

    by , 05-24-2011 at 02:48 PM (My Java Tips)
    java.lang.ArrayStoreException is a runtimeException and is thrown when we try to store wrong type of object into an array of objects.


    Java Code:
    ArrayStoreException(String s)
    For example: Storing an Integer object in a Strign array will cause this exception.

    Java Code:
    Object x[] = new String[3];
    x[0] = new Integer(0);
    Since its a runtime exception, compiler won't ...
    Java SE
  10. Finalize

    by , 05-24-2011 at 02:46 PM (My Java Tips)
    All Java classes inherits the finalize() method from java.lang.Object and this method is called by the garbage collector when it determines no more references to the object exist.

    The finalize method performs no actions but it may be overridden by any class. It is overridden to clean-up non-Java resources ie closing a file, closing a connection etc. In overridding finalize() it is a good programming practice to use a try-catch-finally statement and to always call super.finalize(). ...
    Tags: finalize Add / Edit Tags
    Java SE
  11. Reflection example - accessing private data

    by , 05-21-2011 at 06:28 PM (My Java Tips)
    If you think that you cannot access private data members of a class from some other class, then think again. With Reflection, this is possible.

    Using reflection, we can see / view / access a private member, private variable, private method. Some people will not like this with argument that this actually means messing up the laws of encapsulation. I use it for unit testing private methods.

    Example follows:

    Java Code:
    import java.lang.reflect.Field;
    Tags: reflection Add / Edit Tags
    Java SE
Page 5 of 5 FirstFirst ... 345