Beruflich Dokumente
Kultur Dokumente
ARRAYS-INTRODUCTION
Salary[10]
1) One dimensional
2) Two dimennsional
To create an array, you first must create an
array variable of the desired type.
The general form of a onedimensional array
declaration is
type var-name[ ];
1. Declaring an array
2. Creating memory location
2. type [] arrayname;
Creation of array can be done by the following
line :
Arrayname = new type[size];
Eg : number = new int [5];
we can also combine the two steps like:
Int number [] = new int [5];
Finally initiallization can be done as:
Arrayname[subscript] = value;
Note : Unlike C, java protects array from overrun
and underruns .ie trying to access an array
bound its boundaries will generate an error.
ARRAY LENGTH
separately.
twoD[1]=new int[5];
You can Manually allocate differing size second
dimensions.
int twoD[][] = new int[4][];
System.out.println(s1);
System.out.println(s2);
SPECIAL STRING OPERATIONS
String Concatenation:
In general, Java does not allow operators to be
applied to String objects.
The one exception to this rule is the + operator,
which concatenates two strings, producing a
String object as the result.
charAt(int index)
Returns the character at the specified
index. An index ranges from 0 to length() - 1.
Eg:char ch;
ch = "abc".charAt(1); //assigns the value “b” to ch
getChars:
If you need to extract more than one character
at a time, you can use the getChars( ) method.
It has this general form:
void getChars(int sourceStart, int sourceEnd,
char target[ ], int targetStart)
Care must be taken to assure that the target
array is large enough to hold the number of
characters in the specified substring.
Eg: getCharsDemo program
toCharArray( ):
If you want to convert all the characters in a
String object into a character array, the easiest
way is to call toCharArray( ).
char[ ] toCharArray( )
Method Task
S1.setCharAt(n,’x’) Modifies the nth character to x
S1.append(s2) Appends the string s2 to s1 at
the end
S1.insert(n,s2) Insert the string s2 at the
position n of the string s1
S1.SetLength(n) Sets the length of the string s1
to n.
If n<s1.length() s1 is truncated.
If n>s1.length() zeros are added
to s1.
public class StringBufferFunctionsDemo {
Long n1 = Long.valueOf(42000000L);
Long n1 = Long.valueOf(“42000000L”);
WRAPPER CLASS METHODS
The wrapper class have a number of methods for
handling primitive datatypes and objects.
They are as follows:
Converting primitive numbers to Objects numbers using
Constructor Methods
Integer IntVal = new Integer(i); // converts integer to
Integer object
Float FloatVal = new Float(f); // primitive float to Float
object
Same for Double and Long.
Here i and f are denoting primitive datavalues denoting
int, float.They may be constants or variable
Converting Object numbers to primitive
numbers using typeValue() Methods
Method calling