View RSS Feed

All Blog Entries

  1. Convert Number to String

    by , 04-26-2012 at 07:56 PM
    This sections shows how numbers can be converted into String. Given program gives the functionality for conversion of numbers to String.

    Code Description:
    Different kind of numbers have been declared, for example, long, int and float etc given below. valueOf() method is used which is basically Stringclass’s method. For numeric conversions String.valueOf() is used & numeric classes are converted into String by using toString() method, as given below:
    Program’s code ...
  2. Regular Expressions, Literal Strings and Backslashes

    by , 04-26-2012 at 07:55 PM
    In literal strings, backlash is considered as escape character. It is basically a single backlash i.e. “\\”. Backlash is also considered an escape character in case of regular expressions. A single backlash is matched to the regular expression \\. As a java string, regular expression would become “\\\\”. Four backlashes are used for matching just one single.

    Word character is being matched with regex \w. It is abbreviated as "\\w".

    Similar backlash mess up ...
  3. Java Matcher Class

    by , 04-26-2012 at 07:54 PM
    Matcher class instances match the character sequences, against string pattern. Matchers are provided with input by CharSequence so that to provide support to matching, against characters from input sources.

    Pattern helps to create the matcher when pattern’s matcher method is invoked. Once it has been made, different match operations could be performed by matcher.
    • Whole input sequence is matched by the matcher method against the pattern.
    • Input sequence is matched by the
    ...
    Categories
    Regex
  4. Java Pattern Class

    by , 04-26-2012 at 07:54 PM
    Pattern class instance presents the regular expression which has been specified in string form, in syntax, same as used by Perl.

    Regular expression shall be compiling first to the Pattern class instance. Matcher object is created by ther resulting pattern, which matches with the sequence of the arbitrary character. Similar patterns are shared by various matchers as it is stateless.

    Given regular expression is compiled by the compile method into a pattern. After this, ...
    Categories
    Regex
  5. Pattern Method Equivalents in java.lang.String

    by , 04-26-2012 at 07:53 PM
    Support of the regular expression is also present in java.lang.String via different methods which mimic the java.util.regex.Pattern behavior. From APIs, their key excerpts are shown below for convenience.

    Public Boolean matches (String regex):
    Show whether string will be matching with the regular expression or not. This method’s invocation of the form str.matches produces similar output that was produced by expression Pattern.matches.

    Public string[] split (String ...
    Categories
    Regex
  6. Java String Boundary Matchers

    by , 04-26-2012 at 07:51 PM
    Till now we were just interested in this thing that if match is present at a location in some specified string. Care hs never been taken regarding the location of match in the string, where it actually took place.

    Pattern matches could be made much more precise just by making information specific along with boundary matches. Let us say, e.g, may be one of you are more keen to find out a specific word but only when it is present at start or end of line. May be you desire to be aware ...
    Categories
    Regex
  7. Differences Among Greedy, Reluctant, and Possessive Quantifiers

    by , 04-26-2012 at 07:50 PM
    Subtle difference is present b/w reluctant, greedy and possessive quantifiers.

    Greedy ones are known as greedy as they enforce matchers to eat or read in the whole input string before the 1st match attempt. When 1st attempt of match gets failed, input string is back offed by the matcher by 1 character and then tries once again in which the process is repeated till the match is being found or no more characters have been left for backing off. It totally depends upon quantifier that ...
  8. Java String Quantifiers

    by , 04-26-2012 at 07:49 PM
    Number of occurrences are specified by the quantifiers so that to match. For sake of convenience, Pattern API has been divided into 3 sections with specifications that describe possessive, greedy and reluctant quantifiers are represented below. In first look, this might happen that X?+, X? And X?? Be doing similar things as all of them promise to be match X i.e once, or not at all. Subtle differences of implementations are present that will be discussed at the end of this section.

    ...
    Categories
    Regex
  9. What are Metacharacters

    by , 04-26-2012 at 07:47 PM
    Special characters are also supported by this API which places an affect at the mode by which a pattern is matched. Regular expressions can be changed to cat. & input string to tocats. Output will be as following:
    Enter regex i.e. cat.

    Enter “input string” for searching i.e. cats
    Text cats are found that start at index zero & end at index four.
    Match succeeds even dot is absent in input string. Match succeeds as dots are metacharacters, a character that ...
    Categories
    Regex
  10. String Literals

    by , 04-26-2012 at 07:46 PM
    Basic pattern matching form that is being supported by API is string literal match. E.g, when regular expression and input string is foo, matching succeeds as strings are same. Test harness could be used to try it.

    Enter regex: foo
    Enter “input string” for search: foo
    Text foo is found that starts at index 0, & ends at index 3.

    Match was successful. It shall be noted that when input strings are three characters longer then starting index is zero and ...
    Categories
    Regex
  11. How Are Regular Expressions Represented?

    by , 04-26-2012 at 07:45 PM
    Package java.util.regex has three classes: PatternSyntaxException, Matcher and Pattern.

    • Pattern object is basically the compiled presentation of regular expression. For pattern creation first thing is invoking public static methods of compilation that will make pattern object to be returned. Regular expression is accepted by these methods being 1st argument; 1st trial lessons will let you know regarding the required syntax.
    • Matcher object is considered to be an engine which will
    ...
    Categories
    Regex
  12. Using Regular Expressions with String.matches()

    by , 04-26-2012 at 07:44 PM
    Built in support is provided by Strings present in Java for the regular expressions. There are 3 built in methods present in Strings for regular expressions. For example: replace(), split() and matches().

    There is no optimization of methods for performance. Classes will be used later on, which are performance optimized.

    s.matches("regex"): Evaluates if "regex" matches s. Returns only true if the WHOLE string can be matched
    s.split("regex") ...
    Categories
    StringBuffer
  13. Regular Expressions Overview

    by , 04-26-2012 at 07:42 PM
    For strings, search pattern is defined by the regular expression. Such kind of pattern might be matching one time or several times, or not at all. Regular expression has an abbreviation “regex”.
    String (literal) is a simpler example of regular expression. E.g. Hello World string will be matched with Hello World regex.

    Other regular expression example is (dot) “.” That match with just one single character; and it will be matching e.g. “z”or ”a” or ”1”.

    Usage ...
  14. Memory usage of StringBuilder and StringBuffer

    by , 04-26-2012 at 07:39 PM
    The implementation of these classes is essentially done in similar manner through the shared subclasses & usage of their memory is also same. Difference comes when the turn of those methods come that are present at StringBuffer get synchronized. Here they will be referred as the string buffers generically for sake of simplicity however; comment at the usage of memory will be applied to both classes.

    Object’s overhead is required by the string buffer, space for sake of int field ...
  15. StringBuffer Functions

    by , 04-26-2012 at 07:38 PM
    Few StringBuffer methods has many uses which is being explained by the following program:

    1. capacity()
    String buffer’s current capacity is returned.

    2. length()
    String buffer’s length is returned.

    3. charAt(int index)
    Specified sequence character which is presented by string buffer are returned.

    4. setCharAt(int index, char ch)
    At specified index, the character of this string buffer has been set to ch
    ...
  16. StringBuffer

    by , 04-26-2012 at 07:36 PM
    Thread safed & mutable character sequence. Basically it is just like String however modifications could be done with it. At any time, few specified character sequences are present in it but the content or length of that sequence might be changed via method calls.

    Mutilple threads may use string buffers safely. Synchronization of methods is done where it is considered to be an important thing hence all operations that take place at some specific instance would behave like they ...
    Tags: stringbuffer Add / Edit Tags
    Categories
    StringBuffer
  17. Understanding String memory usage

    by , 04-26-2012 at 07:36 PM
    Fields which are present at the String object shall be looked first of all so that for a better understanding of the above calculations:

    • a char array: Separate object which consist of the actual characters;
    • an integer offset where the string starts;
    • String Length
    • Another int, for hash code’s cached calculations.



    This shows that if no characters are present in a string, even then 4 bytes would be required for sake of the char array reference along ...
    Categories
    String
  18. Memory usage of Java Strings and string-related objects

    by , 04-26-2012 at 07:35 PM
    Everywhere strings are present. It is quite difficult thing to write down an application that is not used by them or others use it extensively. Various typical applications like server entities that are obtained from databases, or data present to be displayed at web, most of the objects that have been stored on heap might be Strings. Therefore, when usage of memory is crucial it means that understanding of the Strings memory usage or the related objects is also crucial.

    If your have ...
    Categories
    String
  19. Getting Characters and Substrings by Index

    by , 04-26-2012 at 07:34 PM
    Character can be obtained at one particular index, within a string, by charAt() accessor method’s invoking. First character index is 0 however last character index is length()-1. This given example shows the code which obtains the character, at index 9 present in string.
    String anotherPalindrome = "Niagara. O roar again!";

    char aChar = anotherPalindrome.charAt(9);

    Indices start with 0 therefore the characters which are present at the index 9 are 'O'. ...
    Categories
    String
  20. Converting Strings to Numbers

    by , 04-26-2012 at 07:32 PM
    In a string object, usually a program would be ending up with numeric data, a value which user will enter. For example:
    Primitive numeric types (Short, Integer, Byte, Double, Long and Float) are being wrapped by the Number subclasses. A class method known as valueOf is provided by each which makes a string to be converted into object of that type. For example, ValueOfDemo will be getting 2 strings to convert them into no. and then performing the arithmetic operations at values.
    ...
    Categories
    String
  21. The difference between == and equals()

    by , 04-26-2012 at 07:31 PM
    The == operator:
    References are compared by == operator, of the 2 objects which are present in memory. When these 2 objects are pointing similar memory location, then it returns true. It shall be remembered that in java strings are immutable therefore if string variables “str1” is present along with “abc” value and then another variable along with other value is created, instead of new string variable creation with same value, then Java simply would be pointing str2 to similar location of ...
    Tags: equals() Add / Edit Tags
    Categories
    String
  22. String Conversion and toString() in Java

    by , 04-26-2012 at 07:30 PM
    When data is converted into string representation while concatenation is done, versions that are overloaded string conversion methods valueOf() are called. They are defined by the help of String.valueOf(), which are overloaded for type Object and all simpler types. In case of simple types, a string is returned by the valueOf() in which human readable are present that are equal to value that has been called. On the object, toString() method is called by ValueOf(). Here toString() method shall be ...
    Categories
    String
  23. String Equality and Interning

    by , 04-26-2012 at 07:29 PM
    In Java Strings are the objects however they resemble primitives like charS or intS. In that java source, code may be having the String literals and + operator might be used to concatenate the Strings. These features are quite convenient, however similarity present b/w Strings & primitives may cause certain confusions, when comparison is done of the Strings.

    Two mechanisms are provided by Java to do the test for equality. Use “==” operator for testing of the primitive values equality. ...
    Categories
    String
  24. Differences between String and StringBuffer in Java

    by , 04-26-2012 at 07:29 PM
    Major difference that exist b/w StringBuffer and String is the immutable string, while mutability of the StringBuffer means that StringBuffer object could be modified once it has been created, without creation of new object. StringBuffer is made an ideal choice by this mutable property to deal with Java strings. StringBuffer can be converted into String by the help of toString() method. StrinBuffer Vs String, or difference b/w String & StringBuffer is most popular for the interview questions, ...
    Categories
    String
  25. Problem with String in Java

    by , 04-26-2012 at 07:28 PM
    In java string, biggest strength well known as immutability is actually the biggest problem encountered in case it is not used appropriately. String is created many times and lots of operations are being performed at them. For example, string conversion into lowercase, uppercase, concatenating with string or getting substring out. Though String is considered as immutable class, when a new string is created evertime and the old one gets discarded that makes temporary garbage a lot, in heap. If String ...
    Categories
    String
  26. Concatenating Strings

    by , 04-26-2012 at 07:27 PM
    A method is present in String class for concatenation of 2 strings:
    Java Code:
    string1.concat(string2);
    New string will be returned, that is, string1 and string2 will be added in the end. Concat() method could also be used along with string literals:
    Java Code:
    "My name is ".concat("Zara");
    Commonly, strings are concatenated along with +operator:
    Java Code:
    "Hello," + " world" + "!"
    Result ...
    Categories
    String
  27. String Length

    by , 04-26-2012 at 07:26 PM
    An object’s information is seeked by certain methods and those methods are named as accessor methods. Among them, one that could be used along with strings is known as length() method that would be returning no. of characters present in string object.
    Java Code:
    public class StringDemo{
       public static void main(String args[]){
          String palindrome = "Dot saw I was Tod";
          int len = palindrome.length();
          System.out.println( "String Length
    ...
    Categories
    String
  28. Creating Strings

    by , 04-26-2012 at 07:25 PM
    Another direct method for creation of string is, to write:
    Java Code:
    String greeting = "Hello world!";
    In your code, whenever string literal is encountered, a String object is created by the compiler along with value, “Hello World”
    Eleven constructors are present in String class which permits to give the string’s initial value by usage of different kind of resources, like character array.
    Java Code:
    public class StringDemo{
       public static void main(String
    ...
    Categories
    String
  29. String representation of Objects

    by , 04-26-2012 at 07:25 PM
    Tostring() method is implemented by all java objects that will be returning String object, which will be describing that object in a best manner. For instance, red colored object can be printed out by using:
    Java Code:
    Color color = new Color( 255, 0, 0 );
    String colorStr = color.toString();
    Console.println( colorStr );
    Printout will be:
    Java Code:
    java.awt.Color[r=255,g=0,b=0]
    In such a case, there is no need to call tostring() method explicitly. ...
    Categories
    String
  30. Strings in Java

    by , 04-26-2012 at 07:24 PM
    The most common use of the string class is manipulation and holding of the text strings, in Java programs. In case of automatically imported standard java.lang packages this is usually found, therefore nothing special is needed to use it.


    String class can be used in its simplest form by typing text that is usually double quotes surrounded. This is what is known as String literal.
    "This is a Java String"

    Anywhere a String object is needed, string ...
    Categories
    String
Page 10 of 48 FirstFirst ... 8910111220 ... LastLast