Friday 23 June 2017

Characters, Strings & Arrays

Characters:

Most of the time, if you are using a single character value, you will use the primitive char type. For example:

//Single char
char ch = 'a';
// an array of chars 
char[] charArray = { 'a', 'b', 'c', 'd', 'e' }; 

There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" the char in a Character object for this purpose. An object of type Character contains a single field, whose type is char. This Character class also offers a number of useful class (i.e., static) methods for manipulating characters.

You can create a Character object with the Character constructor:

Character ch = new Character('a');

Useful Methods in the Character class

Escape Sequences:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:


Escape SequenceDescription
\tInsert a tab in the text at this point.
\bInsert a backspace in the text at this point.
\nInsert a newline in the text at this point.
\rInsert a carriage return in the text at this point.
\fInsert a formfeed in the text at this point.
\'Insert a single quote character in the text at this point.
\"Insert a double quote character in the text at this point.
\\Insert a backslash character in the text at this point.

Strings:

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Creating Strings:


The most direct way to create a string is to write:

String greeting = "Hello world!";

In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

As with any other object, you can create String objects by using the new keyword and a constructor. The String class has thirteen constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:

char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' }; 
String helloString = new String(helloArray); 
System.out.println(helloString);

Output:
hello

String Length:

Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. Example below will illustrate this.


public class StringsDemo {

  public static void main(final String[] args) {
    String palindrome = "How are you";
    int len = palindrome.length();
    System.out.println(len);
  }
}

Output:
11



Arrays:

Which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Arrays in Java are also objects. They need to be declared and then created. In order to declare a variable that will hold an array of integers, we use the following syntax:

int[] arr = new int[10];

This will create a new array with the size of 10. We can check the size by printing the array's length:

public class ArraysDemo {

  public static void main(final String[] args) {
    final int[] arr = new int[10];
    System.out.println(arr.length);
    }
}

Output:
10

We can access the array and set values and print them,

public class ArraysDemo {

  public static void main(final String[] args) {
    final int[] arr = new int[5];
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    arr[3] = 4;
    arr[4] = 5;
    for (int i = 0; i < arr.length; i++) {
      System.out.println(arr[i]);
    }
    }
}

Output:
1
2
3
4
5

Arrays index starts with 0 but not 1, which means the first element in an array is accessed at index 0 (e.g: arr[0], which accesses the first element). Also, as an example, an array of size 5 will only go up to index 4 due to it being 0 based. What happens if you try to access

We can also create an array with values in the same line:

public class ArraysDemo {

  int[] arr = {1, 2, 3, 4, 5};
    for (int i = 0; i < arr.length; i++) {
      System.out.println(arr[i]);
    }
}

Output:
1
2
3
4
5

Don't try to print the array without a loop, it will print the object code but not the values.

Passing Arrays to Methods:

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array:

public void processArray(int[] array) {
   ....
}



No comments:

Post a Comment