Posts

Showing posts with the label java

Exceptions in java

Exceptions in java are any abnormal, unexpected events or extraordinary conditions that may occur at runtime. They could be file not found exception, unable to get connection exception and so on. On such conditions java throws an exception object. Java Exceptions are basically Java objects. No Project can never escape a java error exception. Java exception handling is used to handle error conditions in a program systematically by taking the necessary action. Exception handlers can be written to catch a specific exception such as Number Format exception, or an entire group of exceptions by using a generic exception handlers. Any exceptions not specifically handled within a Java program are caught by the Java run time environment An exception is a subclass of the Exception/Error class, both of which are subclasses of the Throwable class. Java exceptions are raised with the throw keyword and handled within a catch block.

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

Java Search String using indexOf Example

/*   Java Search String using indexOf Example   This example shows how we can search a word within a String object using   indexOf method. */   public class SearchStringExample {   public static void main ( String [ ] args ) { //declare a String object String strOrig = "Hello world Hello World" ;   /*   To search a particular word in a given string use indexOf method.   indexOf method. It returns a position index of a word within the string   if found. Otherwise it returns -1.   */   int intIndex = strOrig. indexOf ( "Hello" ) ;   if ( intIndex == - 1 ) { System . out . println ( "Hello not found" ) ; } else { System . out . println ( "Found Hello at index " + intIndex ) ; }   /*   we can also search a word after particular position using   indexOf(String word, int position) method.   */ ...