Unit 1-Chapter 5 - Arrays, Strings, Predefined Classes
Unit 1-Chapter 5 - Arrays, Strings, Predefined Classes
Java provides a data structure, the array, which stores a fixed sized sequential collection of
elements of the same type.
In java, arrays are treated as objects. while using arrays we create objects for arrays.
DEFINITION
It can hold primitive values or object references as its data elements.
Multidimensional Array
•The array declaration is usually the data type followed by a pair of square brackets
followed by the array name.
The actual array construction with a new keyword involves the memory allocation and
array object creation at runtime.
An array of object references type is created by specifying the object type and the size of the
array.
An array of 5 Date object references will be created with each element initialised to null.
The actual construction of the array object and initialization will be done only at run time.
MULTIDIMENSIONAL
ARRAYS
A multidimensional array is an array references.
Since an array object can store object references, it can store references to other
arrays. Thus, can declared as array of arrays.
A regular array’s elements are array references to the array of equal size.
Conceptually, it is a matrix like structure where number of rows and columns are
known.
DECLARATION, CREATING, INITIALIZATION
To use those methods, we have to add the below statement in the program.
import java.util.Arrays;
This is to tell the compiler where to find the code for these methods.
It returns true if the arrays are equal and false if they are not.
Two arrays are considered equal if they have the same number of elements and the
elements are arranged in the same order.
Example, int [ ] source = {12, 1, 5, -2, 16, 14, 18, 20, 25};
We can copy the contents of source array into a new array dest,
system.out.println(Arrays.toString(numbers));
sort( )
The sort( ) method allows to sort arrays.
The sort() method does not written a new array, it simply modifies the array that is
passed in as argument.
system.out.println(Arrays.toString(numbers));
STRINGS
STRING – An Array Of Characters
oOne of the most important java data type is string.
o in java strings are objects and string class is part of java.lang package.
There are only 8 primitive types in java [ byte, short, int, long, float, double, char and
Boolean] the rest are reference types.
Examples for reference types are string, arrays, classes and interfaces.
One of the main difference between a primitive type and a reference type is the data that is
stored.
WHY ARE STRINGS CALLED IMMUTABLE?
String objects are created by either using new operator or enclosing a sequence of
characters in double quotes.
The string object created by either ways is immutable, means once we create a string
object by specifying a sequence of characters, that object will always represent that same
sequence of characters throughout its life.
We can never change this sequence of characters in this object, no method in
java.lang.String class allow us to do.
Any method that may appear to be changing the string actually creates a new string object.
For example, if we try to modify the string “Hello” by concatenating “World” as,
str.concat(“World”);
str1=str2;
system.out.prinln(str1);
STRING METHODS
The StringBuffer class provides us with a better alternative, this class represents a string that
can be modified without creating too many objects.
Like the string class, StringBuffer class also represents Strings of characters in java.
The major difference between a string object and StringBuffer object is that the StringBuffer
object is mutable.
•The command line arguments represent the values passed to main() method.
•To receive and stores the values, main() method provides arguments called as String
args[].
For instance, if we want to store primitive like objects in collection, they must be objects, so
we want to represent them as objects.
The wrapper class contain a number of useful methods and it's defined in java.lang package,
The wrapper class can wrap [encapsulate] a single immutable primitive value.
For example, a wrapper class integer wraps an in value; a class named Boolean wraps
Boolean value and so on.
Primitive Types Corresponding Wrapper Class
boolean Boolean
byte Byte
short Short
char Character
int Integer
long Long
float Float
double double
CREATING WRAPPER CLASS OBJECTS
If we want a particular primitive value, say int 100 to be treated as object, we need to
wrap it into a wrapper object.
1. Find out what is the corresponding wrapper class for the primitive type; for int
primitive, it will be Integer class.
Another way of creating a wrapper object is invoking a static method valueOf() method
of a wrapper class, this method takes a string as an argument. Valueof(String s).
if we want to get a wrapper object for int value 100, we can do by following steps,
1. Find out what is the corresponding wrapper class for the primitive type; for int
primitive, it will be Integer class.
UNBOXING:
To convert from Integer to int,
int m = intObj.intValue();
We can simply write,
int m = intObj;
THANK YOU