Java Notes by Pradeep Goud
Java Notes by Pradeep Goud
Declaring an Array
Arrays are declared by stating the type of element the array will
hold followed by square brackets to the left or right of the
identifier.
Example:
int[] key; // brackets before name (recommended)
int key []; // brackets after name (legal but less readable)
// spaces between the name and [] legal, but bad
It is never legal to include the size of the array in your declaration.
int[5] scores;
Remember, the JVM doesn't allocate space until you actually
instantiate the array object.
Constructing an Array
int[] testScores;
testScores = new int[4];
----values-------
0
1
The Heap
Initializing an Array
Initializing an array means putting elements into it.
Note
if an array has three elements, trying to access the [3] element
will raise an ArrayIndexOutOfBoundsException, because in
an array of three elements, the legal index values are 0, 1, and
2.
int[] x = new int[5];
x[4] = 2; // OK, the last element is at index 4
x[5] = 3;
Multidimensional Arrays
In Java, multidimensional arrays are actually arrays of arrays.
To declare a multidimensional array variable, specify each
additional index using another set of square brackets.
For example, the following declares a two dimensional array
variable called twoD.
int twoD[][] = new int[4][5];
Multidimensional Arrays
Strings
Creation of Strings
1)We can create a string just by assigning a group of characters to
a string type variable
String s;// declare a string type variable
S=Hello;//assign a group of characters to it
Note: String s=Hello;
2) We can create an object to String class by allocating memory
using new operator
String s=new String(Hello);
3)Creating string by converting character array into strings
char[] arr={H,e,l,l,o};
String s=new String(arr);
String s=new String(arr,1,3); //ell
String s=abc;
String s1=new String(abc);
String s=abc;
s
abc
String s2=s;
abc
s2
s = s.concat (def);
abc
s2
s
abcdef
s
String x = "Java";
x.concat(" Rules!");
System.out.println("x = " + x);
String x = "Java";
x = x.concat(" Rules!");
System.out.println("x = " + x);
getChars()
To extract more than one character at a time
public void getChars(int start, int end,char target[], int targetStart)
Example:
String s=This is a demo of the getChars method;
int start=10;
int end=14;
char[] buf=new char[end-start];
s.getChars(start,end,buf,0);
System.out.println(buf); //demo
getBytes()
public byte[] getBytes()
toCharArray()
Convert all the characters in a string object into a
character array
public char[] toCharArray()
String Comparison
equals()
equalsIgnoreCase()
regionMatches()
startsWith()
endsWith()
compareTo()
equals()
1) To compare two strings for equality, use equals()
2) It returns true if the strings contains same characters in the
same order, and false otherwise
3) The comparison is case-sensitive
public boolean equals(Object str)
String s1=exit;
String s2=exit;
Strrong s3=Exit;
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
equalsIgnoreCase(String s)
This method returns a boolean value (true or false) depending
on whether the value of the String in the argument is the same
as the value of the String used to invoke the method.
This method will return true even when characters in the
String objects being compared have differing cases
public boolean equalsIgnoreCase(String s)
String x = "Exit";
System.out.println( x.equalsIgnoreCase("EXIT")); // is "true"
System.out.println( x.equalsIgnoreCase("tixe")); // is "false"
regionMatches()
This method compares a specific region inside a string with
another specific region in other string
boolean regionMatches(int startindex, String s2, int s2Startindex,
int numChars)
String s1="helloworld";
String s2="world";
System.out.println(s1.regionMatches(5,s2,0,3));
boolean regionMatches(boolean ignoreCase,int startindex, String
s2, int s2Startindex, int numChars)
startsWith()
This method determines whether a given string begins with a
specified string
public boolean startsWith(String str)
String s1=hello world;
System.out.println(s1.startsWith(hello);
endsWith()
This method determines the string ends with a specified string
public boolean endsWith(String str)
compareTo()
This method is useful to compare two strings and to know which
string is bigger or smaller
This should be used as s1.compareTo(s2)
If s1 and s2 strings are equal, then this method gives 0
If s1 is greater than s2, then this method a +ve number
If s1 is less than s2, then it returns a ve number
When we arrange the strings in dictionary order, which ever
string comes first is lesser than the string that comes next.
Example: Box is lesser than Boy
This method case sensitive. It means BOX and box are not
same for this method
public int compareTo(String s)
public int compareToIgnoreCase(String s)
Modifying a Strings
1) substring()
2) concat()
3) replace()
4) trim()
substring()
This method is useful to extract sub string from a main string
public String substring(int x)
It return a new string consisting all characters starting from the
position x until the end of the string
Example:
String x = "0123456789";
System.out.println( x.substring(5) ); // 56789
public String substring(int stratindex, int endindex)
The string returned contains all the characters from the beginning
index, up to, but not including the ending index
System.out.println( x.substring(5, 8));
String concat()
This method concatenates or joins two strings (s1 and s2) and
return a third string (s3) as result.
public String concat(String s)
Example:
String x = "taxi";
System.out.println( x.concat(" cab") );
String trim()
public String trim()
This method removes spaces from the beginning and ending of a
string
String x = Rishvad Mohan ";
System.out.println( x.trim() );
Searching String
indexOf()
This method is called in the form s1.indexOf(s2), and it
returns an integer value.
If s1 contains s2 as sub string, then the first occurrence of s2 in
the string s1 will be returned by this method
public int indexOf(String s)
String s1=This is a book
String s2=is
int n=s1.indexOf(s2);
lastIndexOf(String s)
This method returns the last occurrence of the sub string s in the
main string. If s is not found, then it returns negative value
public int lastIndexOf(String s)
String s1=This is a book
String s2=is
int n=s1.lastIndexOf(s2);
length()
public int length()
This method returns the length of the String used to invoke the
method
String x = "01234567";
System.out.println( x.length() ); // returns "8"
StringBuffer
StringBuffer is a class which represents strings in
such a way that their data can be modified
It means StringBuffer class objects are mutable