Sunday, 20 October 2013

Class StringBuffer ( java.lang.StringBuffer )

StringBuffer

The StringBuffer class is an alternative to the String class. You can add, insert, or append new contents into a string buffer, whereas the value of a String object is fixed once the string is created.

StringBuffer is mutable.StringBuffer is a synchronized and allows us to mutate the string. StringBuffer has many utility methods to manipulate the string. It can change in terms of length and content.
StringBuffers are thread-safe,meaning that they have synchronized methods to control access,so that only one thread can access a StringBuffer object's synchronized code at a time.

Thus, StringBuffer objects are generally safe to use in a multi-threaded environment where multiple threads may be trying to access the same StringBuffer object at the same time.

StringBuffer declaration :

public final class StringBuffer 
extends Object implements Serializable, CharSequence


Lets check list of method of StringBuffer,


For more details :
http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

Saturday, 19 October 2013

Class String ( java.lang.String )

Strings

The String class provided by java allows use of strings, which are objects not primitive type.The string literal refers to any alphanumeric enclosed in double quotation mark. Like, "Value"

Strings in java are immutable. Once created they cannot be altered and hence any alterations will lead to creation of new string object.

A String, once created, cannot be changed. None of the preceding methods modify the String, although several create a new String Statements like this create new Strings:

    varString = varString + anotherChar;

Creating a few extra Strings in a program is no big deal but creating a lot of Strings can be very costly.

 Lets see an example,

String s1 = "Sample"
String s2 = new String("Sample")
String s3 = "Sample"

 
The difference between the three statements is that, s1 and s3 are pointing to the same memory location i.e. the string pool.  s2 is pointing to a memory location on the heap. Using a new operator creates a memory location on the heap. Concatenating  s1 and s3 leads to creation of a new string in the pool.


Strings can contain any character, but some of them are "escaped" in order to write them in a literal. Like,
\" stands for the double-quote (") character
\n stands for the newline character
\\ stands for the backslash (\)character
Each of these is written as a two-character sequence, but represents a single character in the string.

Here List of methods of String Class :

Sunday, 13 October 2013

What is Java Bytecode?

Bytecode is compiled java code that the Java Virtual Machine executes.

Lets see it with creating source file , compile source file into .class file & run the .class file.

See , you have a helloworld.java file.










now compile it with java compiler javac. This will create a helloworld.class file. By opening this .class file in any text editor will shown as below.


















Now run the code with java ,

java helloworld










The way of Other languages works :
  1. write source code
  2. port source code in different platform
  3. compile source code in platform  specific binaries or machine code
  4. binaries executes on a single platform















 On other hand java works:
  1. write source code
  2. compile source code 
  3. run any platform














The "javap -c" command disassembles a class file. Disassembling done by Disassembler which converts machine language to assembly language.


Sunday, 6 October 2013

Regular Expressions with java.util.regex.*

A regular expression defines a patter for a String. Regular expressions are extremely useful in extracting information from text such as code, log files, spreadsheets, or even documents.The first thing to recognize when using regular expressions is that everything is essentially a character, and we are writing patterns to match a specific sequence of characters.

"A regular expression is a kind of pattern that can be applied to Strings."

"A regular expression either matches the text or part of the text, or it fails to match." 
"Regular expressions are extremely useful for manipulating text"


Java has a regular expression package, java.util.regex.

Now Do it,
  1. create Object of Pattern Class.

    Pattern p = Pattern.compile("[a-z]+");
  2. create a matcher for a specific piece of text by passing a String to a Pattern Object.

    Matcher m = p.matcher("Now is the time");
  3. Now that we have a matcher m,
    •  m.matches() returns true  if the pattern matches the entire text string, and false otherwise
    • m.lookingAt() returns true if the pattern matches at the beginning of the text string, and false otherwise
    • m.find() returns true if the pattern matches any part of the text string, and false otherwise
    • If called again, m.find() will start searching from where the last match was found
    • m.find() will return true for as many matches as there are in the string; after that, it will return false
    •  When m.find()  returns false, matcher m will be reset to the beginning of the text string (and may be used again)
    • After a successful match, m.start() will return the index of the first character matched
    • After a successful match, m.end() will return the index of the last character matched, plus one
    • If no match was attempted, or if the match was unsuccessful, m.start() and m.end() will throw an IllegalStateException
    • m.replaceFirst(replacement) returns a new String where the first substring matched by the pattern has been replaced by replacement
    • m.replaceAll(replacement) returns a new String where every substring matched by the pattern has been replaced by replacement
    • m.find(startIndex) looks for the next pattern match, starting at the specified index
    • m.reset() resets this matcher
    • m.reset(newText) resets this matcher and gives it new text to examine (which may be a String, StringBuffer, or CharBuffer)
     
 Patterns Matches :

  








Breaking down:











 Regular Expression Cheat Sheet

Wednesday, 2 October 2013

How to Define and Use A Java Array

Java Arrays are data structures containing a fixed number of values objects of the same type. Each item in an array is called an element, and each element is accessed by its numerical index.

The following program creates an array of integer , putting values and prints each value of array in standard output.

public class techedifyOne {   
    public static void main(String[] args) {       
        int[] ary;// declares an array of integers
       
ary = new int[5];// allocates memory for 5 integers
       
ary[0] = 1;// initialize first element
       
ary[1] = 2;//and so on
       
ary[2] = 3;
       
ary[3] = 4;
       
ary[4] = 5;
        System.out.println("Element at index 0: "+
ary[0]);
        System.out.println("Element at index 1: "+
ary[1]);
        System.out.println("Element at index 2: "+
ary[2]);
        System.out.println("Element at index 3: "+
ary[3]);
        System.out.println("Element at index 4: "+
ary[4]);
    }


Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5


In above program declares a variable with following code :

int[] ary;

When you declare a Java array, you need to give it a data type. In above program , we declare an integer array containing five elements.see following code :

ary = new int[5];

An element of array will be access by index of that array. In above program , first element is initialize ny accessing element on 0 index. see following code :

ary[0] = 1;// initialize first element


we can also declare multidimensional  array by using two or more set of brackets.
e.g.
        String [][] val;
        val = new String[2][2];
        val[0][0]="value One";
        val[0][1]="value Two";
        val[1][0]="value Twenty One";
        val[1][1]="value Twenty Two";


we can also declare multidimensional  array with initialization of value.
e.g.
        String [][] val =
        {
            {"value One","Value Two"},
            {"value Twenty One","Value Twenty  Two"}
        };








Java Identifiers


The names of variables in the Java language are referred to as identifiers. An identifier is a programmer-defined name for something in Java such as a variable, a class or a method. There are several rules and conventions for names in Java. A rule means that it is a requirement of Java. A convention is a generally accepted way of doing something. It is not a requirement, but rather a good idea. It ensures that all programmers use the same style. That makes it easier for programmers to collaborate.

“Identifiers can begin with a letter, an underscore, or a currency character.”

 “After the first character, identifiers can also include digits.”

 “Identifiers can be of any length.”

 “Java Beans methods must be named using camel Case, and depending on the method's purpose, must start with set, get, is, add, or remove”


" Another important point to note is that identifiers are case-sensitive."