Arrays and Strings

            Arrays and Strings

  • Java provides a data structure, the array, 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.

  • Strings, which are widely used in Java programming, are a sequence of
    characters. In Java programming language, strings are treated as objects.
    The Java platform provides the String class to create and manipulate
    strings.


Creating an array

You can create an array by using the new operator with the following syntax −
arrayRefVar = new dataType[arraySize];
The above statement does two things −
  •  It creates an array using new dataType[arraySize].
  • It assigns the reference of the newly created array to the variable arrayRefVar.

One and two dimensional array-

A list of items group in a single variable name with only one index is called 1-D array

2-D Array-

  • The Two Dimensional Array in Java programming language is nothing but an Array of Arrays. If the data is linear we can use the One Dimensional Array but to work with multi-level data we have to use Multi-Dimensional Array.

  • Two Dimensional Array in Java is the simplest form of Multi-Dimensional Array. In Two Dimensional Array, data is stored in row and columns and we can access the record using both the row index and column index (like an Excel File).


String array and methods-

  • A Java String Array is an object that holds a fixed numberof String values. Arrays in general is a very useful and important data structure that can help solve many types of problems.

String Array Declaration-

Square brackets is used to declare a String array. There are two ways of using it. The first one is to put square brackets after the String reserved word. For example:

String[] thisIsAStringArray;
Another way of declaring a String Array is to put the square brackers after the  name of the variable. For example:
 
Another way of declaring a String Array is to put the square brackers after the name of the variable. For example:

String[]thisIsAStringArray[];
Both statements will declare the variable "thisIsAStringArray" to be a String Array. Note that this is just a declaration, the variable "thisIsAStringArray" will have the value null. And since there is only one square brackets, this means that the variable is only a one-dimensional String Array. Examples will be shown later on how to declare multi-dimensional String Arrays.

String and string buffer classes-

  • String and StringBuffer both are the classes which operate on strings.
  •  StringBuffer class is the peer class of the class String.
  • The object of String class is of fixed length.
  •  The object of the StringBuffer class is growable.
  •  The basic difference between String and StringBuffer is that the object of the “String” class is immutable. The object of the class “StringBuffer” mutable.

Wrapper classes-

  • A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other wrap a primitive value into a wrapper class object.

                   Inheritance


  • Inheritance can be defined as the process where one class acquires the properties (methods and fields) of anot information is made manageable in a hierarchical order.
  •  The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).  

Basic types-

There are various types of inheritance as demonstrated below.


A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class.
Therefore following is illegal −

Example
public class extends Animal, Mammal{}
However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance.

Using super-

The super keyword
The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.
  • It is used to differentiate the members of superclass from the members of subclass, if they have same names.
  • It is used to invoke the superclass constructor from subclass.

Differentiating the Members
If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below.

super.variable 
super.method();

Multilevel hierarchy abstract and final
classes-

  • The classes in the lower hierarchy inherit all the variables (static attributes) and methods (dynamic behaviors) from the higher hierarchies. A class in the lower hierarchy is called a subclass (or derived, child, extended class). A class in the upper hierarchy is called a superclass (or base, parent class).
  •  In Multilevel hierarchy type of Inheritance , each subclass inherits all of the traits found in all of its super classes. It is perfectly acceptable to use a subclass as superclass of another.

Object class-

  • Every class in Java is directly or indirectly derived from the Object class. If a Class does not extend any other class then it is direct child class of Object and if extends other class then it is an indirectly derived. Therefore the Object class methods are available to all Java classes.

Packages and interfaces-

  • The content in packages and interfaces can be used by the classes by importing and implementing it correspondingly. The basic difference between packages and interfaces is that a package contains a group of classes and interfaces whereas; an interface contains methods and fields.

Comparison Chart

Access protection-

  • Protected Access Modifier - Protected. Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.


Extending interfaces-

An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. The following Sports interface is extended by Hockey and Football interfaces.

Example
The Hockey interface has four methods, but it inherits two from Sports; thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Football needs to define the three methods from Football and the two methods from Sports.

Packages-

A package as the name suggests is a pack(group) of classes, interfaces and other  packages. In java we use packages to organize our classes and interfaces. We have  two types of packages in Java: built-in packages and the packages we can create  (also known as user defined package). In this guide we will learn what are  packages, what are user-defined packages in java and how to use them.

In java we have several built-in packages, for example when we need user input, we import a package like this:

import java.util.Scanner
Here: 
java is a top level package 
util is a sub package 
→ and Scanner is a class which is present in the sub package util. 
Types of packages in Java

  As mentioned in the beginning of this guide that we have two types of packages in  java
1) User defined package: The package we create is called user-defined package. 
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages

Next Unit- Exception Handling in Java



1 Comments

Previous Post Next Post