Posts

Creating a Thread

Java defines two ways in which this can be accomplished: You can implement the Runnable interface. You can extend the Thread class, itself. Create Thread by Implementing Runnable: The easiest way to create a thread is to create a class that implements the  Runnable  interface. To implement Runnable, a class need only implement a single method called  run( ) , which is declared like this: public void run( ) You will define the code that constitutes the new thread inside run() method. It is important to understand that run() can call other methods, use other classes, and declare variables, just like the main thread can. After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors. The one that we will use is shown here: Thread(Runnable threadOb, String threadName); Here  threadOb  is an instance of a class that implements the Runnable interface and the name of t...

Threads in Java

Java provides built-in support for  multithreaded programming . A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. A multithreading is a specialized form of multitasking. Multitasking threads require less overhead than multitasking processes. I need to define another term related to threads:  process:  A process consists of the memory space allocated by the operating system that can contain one or more threads. A thread cannot exist on its own; it must be a part of a process. A process remains running until all of the non-daemon threads are done executing. Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.

The Arrays Class

The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are overloaded for all primitive types. SN Methods with Description 1 public static int binarySearch(Object[] a, Object key) Searches the specified array of Object ( Byte, Int , double etc) for the specified value using the binary search algorithm. The array must be sorted prior to making this call. This returns index of the search key, if it is contained in the list; otherwise, (-(insertion point + 1). 2 public static boolean equals(long[] a, long[] a2) Returns true if the two specified arrays of longs are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. This returns true if the two arrays are equal. Same method could be used by all other premitive data types ( Byte, short, Int etc.) 3 public static voi...

Java String valueOf example.

/* Java String valueOf example. This Java String valueOf example describes how various java primitives and Object are converted to Java String object using String valueOf method. */   public class JavaStringValueOfExample {   public static void main ( String args [ ] ) {   /*   Java String class defines following methods to convert various Java primitives to   Java String object.   1) static String valueOf(int i)   Converts argument int to String and returns new String object representing   argument int.   2) static String valueOf(float f)   Converts argument float to String and returns new String object representing   argument float.   3) static String valueOf(long l)   Converts argument long to String and returns new String object representing   argument long.   4) static String valueOf(double i)   Converts argument double to String and returns new String object representi...

Java String split example.

/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */   public class JavaStringSplitExample {   public static void main ( String args [ ] ) { /*   Java String class defines following methods to split Java String object.   String[] split( String regularExpression )   Splits the string according to given regular expression.   String[] split( String reularExpression, int limit )   Splits the string according to given regular expression. The number of resultant   substrings by splitting the string is controlled by limit argument.   */   /* String to split. */ String str = "one-two-three" ; String [ ] temp ;   /* delimiter */ String delimiter = "-" ; /* given string will be split by the argument delimiter provided. */ temp = str. split ( delimiter ) ; /* print substrings */ for ( int i = 0 ; i ...

Java String compare example.

/* Java String compare example. This Java String compare example describes how Java String is compared with another Java String object or Java Object. */   public class JavaStringCompareExample {   public static void main ( String args [ ] ) {   /*   Java String class defines following methods to compare Java String object.   1) int compareTo( String anotherString )   compare two string based upon the unicode value of each character in the String.   Returns negative int if first string is less than another   Returns positive int if first string is grater than another   Returns 0 if both strings are same.   2) int compareTo( Object obj )   Behaves exactly like compareTo ( String anotherString) if the argument object   is of type String, otherwise throws ClassCastException.   3) int compareToIgnoreCase( String anotherString )   Compares two strings ignoring the character case of the given...