Java notes By Pradeep Goud
-
Upload
pradeepgoud -
Category
Documents
-
view
225 -
download
0
Transcript of Java notes By Pradeep Goud
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 1/45
Arrays
• Arrays are objects in Java that store multiplevariables of the same type.
• Arrays can hold either primitives or object references
• For this arrays, you need to know three things:
How to make an array reference variable declare!
How to make an array object construct!How to populate the array with elements initiali"e!
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 2/45
#eclaring an Array
• Arrays are declared by stating the type of element the array will
hold followed by s$uare 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
• %t is never legal to include the si"e of the array in your declaration.
int[5] scores;
• &emember, the J'( doesn)t allocate space until you actually
instantiate the array object.
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 3/45
Constructing an Array
•*onstructing an array means creating the array object on theheap.
• +o create an array object, Java must know how much space to
allocate on the heap, so you must specify the si"e of the array
at creation time.• +he si"e of the array is the number of elements the array will
hold.
• +he most straightforward way to construct an array is to use
the keyword new followed by the array type, with a bracketspecifying how many elements of that type the array will hold.
int[] testScores; // eclares the array of ints
testScores ! new int["];
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 4/45
int[] testScores;
testScores = new int[4]; ----values-------
The Heap
0 1 23
0 0 0 0
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 5/45
• ou can also declare and construct an array in onestatement as follows:
int[] testScores ! new int["];
int[] car#ist ! new int[];// $ompilation %ails
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 6/45
Initializing an Array
•
%nitiali"ing an array means putting elements into it.
&ote
• if an array has three elements, trying to access the ['] element
will raise an rraynde*+ut+founds-*ception, because inan array of three elements, the legal inde* .alues are , 0, and
12
int[] * ! new int[5];
*["] ! 1; // +3, the last element is at inde* " *[5] ! ';
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 7/45
int[] 4 ! new int[1];
int y ! ';
4[y] ! ";
Declaring, Constructing, and Initializing on One Line
int[] dots ! 67,8,9:;
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 8/45
*onstructing and %nitiali"ing an Anonymous Array
• +he second shortcut is called -anonymous array creation- and
can be used to construct and initiali"e an array, and then assignthe array to a previously declared array reference variable:
int[] testScores;
testScores ! new int[] 6",,1:;
• <emember that you do not specify a si4e when using
anonymous array creation synta*2
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 9/45
Multidimensional Arrays
• %n Java, multidimensional arrays are actually arrays of arrays2
• +o declare a multidimensional array variable, specify each
additional inde using another set of s$uare brackets.
• For eample, the following declares a two dimensional array
variable called twoD.
int two[][] ! new int["][5];
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 10/45
Multidimensional Arrays
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 11/45
/trings
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 12/45
• 0roup of characters are called 1strings2
• %n java, a string is an object of /tring class
• %n Java, each character in a string is a 345bit 6nicode
character
• /tring class is declared as final, which means that /tring class
not subclassed
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 13/45
*reation of /trings3!7e can create a string just by assigning a group of characters to
a string type variableString s;// declare a string type .ariable
S!=>ello?;//assign a group of characters to it
&ote@ String s!=>ello?;
8! 7e can create an object to /tring class by allocating memoryusing new operator
String s!new String(=>ello?);
9!*reating string by converting character array into strings
char[] arr!6A>B,BeB,BlB,BlB,BoB:;
String s!new String(arr);
String s!new String(arr,0,'); //ell
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 14/45
• /tring s;abc<=
• /tring s3new /tring;abc<!=
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 15/45
trings Are Immuta!le O!"ects
• >nce a /tring object is created, it can never be changed it)s
immutable
• +he good news is that while the /tring object is immutable, itsreference variable is not.
String s!=abcdef?; // create a new String obCect, with .alue DabcdefD,
refer s to it
String s1 ! s; // create a 1nd reference .ariable referring to the sameString
s ! s2concat(D more stuffD); //create a new String obCect, with .alue
Dabcdef more stuffD, refer s to it2
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 16/45
/tring objects and their reference variables
• /tring s;abc<=
abc
s
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 17/45
• /tring s8s=
abc
s
s2
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 18/45
• s = s.concat !"e#!$;
abc
abc"e#
s2
s
s
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 19/45
String * ! DEa.aD;
*2concat(D <ulesFD);
System2out2println(D* ! D G *);
String * ! DEa.aD;
* ! *2concat(D <ulesFD);
System2out2println(D* ! D G *);
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 20/45
String s0 ! Dspring D;
String s1 ! s0 G Dsummer D;
s02concat(Dfall D);
s12concat(s0);
s0 G! Dwinter D;
System2out2println(s0 G D D G s1);
Answer: spring winter spring summer.• +here are two reference variables, s3 and s8. +here were
a total of eight /tring objects created as follows: -spring-,
-summer - lost!, -spring summer-, -fall- lost!, -spring
fall- lost!, -spring summer spring- lost!, -winter- lost!,-spring winter- at this point -spring- is lost!. >nly two
of the eight /tring objects are not lost in this process.
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 21/45
/tring class methodsCharacter Extraction
charAt!
get*hars!
get?ytes!
to*harArray!
chart()
+o etract a single character from a /ting
public char chart(int inde*)
Example:
String * ! DairplaneD;
System2out2println( *2chart(1) ); // output is HrA
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 22/45
get$hars()
+o etract more than one character at a time
public .oid get$hars(int start, int end,char target[], int targetStart)
Example:
String s!=Ihis is a demo of the get$hars method?;
int start!0;
int end!0";
char[] buf!new char[endstart];
s2get$hars(start,end,buf,);
System2out2println(buf); //demo
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 23/45
getytes()
public byte[] getytes()
to$harrray()
*onvert all the characters in a string object into a
character array
public char[] to$harrray()
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 24/45
String Comparison
e$uals!
e$uals%gnore*ase!
region(atches!
starts7ith!
ends7ith!
compare+o!
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 25/45
eJuals()
3! +o compare two strings for e$uality, use e$uals!
8! %t returns true if the strings contains same characters in the
same order, and false otherwise9! +he comparison is case5sensitive
public boolean eJuals(+bCect str)
String s0!=e*it?;
String s1!=e*it?;
Strrong s'!=-*it?;
System2out2println(s02eJuals(s1));
System2out2println(s02eJuals(s'));
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 26/45
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 27/45
regionLatches()
+his method compares a specific region inside a string with
another specific region in other string
boolean regionLatches(int startinde*, String s1, int s1Startinde*,int num$hars)
String s0!DhelloworldD;
String s1!DworldD;
System2out2println(s02regionLatches(5,s1,,'));
boolean regionLatches(boolean ignore$ase,int startinde*, String
s1, int s1Startinde*, int num$hars)
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 28/45
startsMith()
+his method determines whether a given string begins with a
specified string
public boolean startsMith(String str) String s0!=hello world?;
System2out2println(s02startsMith(=hello?);
public boolean startsMith(String str, int startinde*)
System2out2println(s02startsMith(DworldD,7));
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 29/45
endsMith()
+his method determines the string ends with a specified string
public boolean endsMith(String str)
String s0!Dhello worldD;
System2out2println(s02endsMith(DrldD));
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 30/45
compareIo()
+his method is useful to compare two strings and to know which
string is bigger or smaller
+his should be used as s3.compare+os8! %f s3 and s8 strings are e$ual, then this method gives @
%f s3 is greater than s8, then this method a ve number
%f s3 is less than s8, then it returns a Bve number
7hen we arrange the strings in dictionary order, which ever
string comes first is lesser than the string that comes net.
Cample: ;?o< is lesser than ;?oy<
+his method case sensitive. %t means ;?>D< and ;bo< are not
same for this method
public int compareIo(String s)
public int compareIognore$ase(String s)
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 31/45
Modifying a Strings
3! substring!
8! concat!
9! replace!
E! trim!
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 32/45
substring()
+his method is useful to etract sub string from a main string
public String substring(int *)
%t return a new string consisting all characters starting from the
position 12 until the end of the string
-*ample@
String * ! D01'"5798D;
System2out2println( *2substring(5) ); // 5798
public String substring(int stratinde*, int endinde*)
+he string returned contains all the characters from the beginning
inde, up to, but not including the ending inde
System2out2println( *2substring(5, 9));
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 33/45
String concat()
+his method concatenates or joins two strings s3 and s8! and
return a third string s9! as result.
public String concat(String s)
-*ample@
String * ! Dta*iD;
System2out2println( *2concat(D cabD) );
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 34/45
String replace(char old, char new)
public String replace(char old, char new)
+his method replaces all the occurrences of character by a new
character =String * ! Do*o*o*o*=
System2out2println( *2replace(H*H, HKH) ); // output is
DoKoKoKoK=
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 35/45
String trim()
public String trim()
+his method removes spaces from the beginning and ending of a
string
String * ! = <ish.ad Lohan D;
System2out2println( *2trim() );
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 36/45
Searching String
inde*+f()
• +his method is called in the form s3.inde>fs8!, and it
returns an integer value.
• %f s3 contains s8 as sub string, then the first occurrence of s8 in
the string s3 will be returned by this method
public int inde*+f(String s)
String s0!=Ihis is a book?
String s1!=is?
int n!s02inde*+f(s1);
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 37/45
lastnde*+f(String s)
+his method returns the last occurrence of the sub string 1s2 in the
main string. %f 1s2 is not found, then it returns negative value
public int lastnde*+f(String s)
String s0!=Ihis is a book?
String s1!=is?
int n!s02lastnde*+f(s1);
l h()
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 38/45
length()
public int length()
+his method returns the length of the /tring used to invoke the
methodString * ! D01'"57D;
System2out2println( *2length() ); // returns D9D
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 39/45
public String to#ower$ase()
+his method returns a /tring whose value is the /tring used to
invoke the method, but with any uppercase characters
converted to lowercaseString * ! D &ew LoonD;
System2out2println( *2to#ower$ase() );
public String toNpper$ase()
+his method returns a /tring whose value is the /tring used to
invoke the method, but with any lowercase characters
converted to uppercase
String * ! D &ew LoonD;
System2out2println( *2toNpper$ase() );
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 40/45
/tring?uffer
• /tring?uffer is a class which represents strings in
such a way that their data can be modified
• %t means /tring?uffer class objects are mutable
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 41/45
*reating /tring?uffer class objects
Stringuffer sb!new Stringuffer(=>ello?);
Stringuffer sb!new Stringuffer();
Here, we are creating a /tring?uffer object as an empty object.
%n this case, a /tring?uffer object will be created a default capacity of 34
characters
Stringuffer sb!new Stringuffer(5);
%n this case, a /tring?uffer object will be created a default capacity of @
charactersCven if we declare the capacity as @, it is possible to store more than @
characters into this /tring?uffer.
+he reason is /tring?uffer is mutable and can epand dynamically in
memory
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 42/45
(ethods of /tring?uffer class
public Stringuffer append(*)
12 may be boolean, byte, int, long, float, double, char, character
array,/tring or other /tring?uffer
%t will be append to the /tring?uffer object
Cample:
Stringuffer sb!new Stringuffer(=uni?);
sb2append(=.ersityB);
System2out2println(sb);
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 43/45
public Stringuffer insert(int i , *)
%t will be inserted into /tring?uffer at the position represented by
i
Cample:Stringuffer sb!new Stringuffer(=intelligent person?);
sb2insert(00,?young?);
System2out2println(sb);
bli St i ff d l t (i t i t C)
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 44/45
public Stringuffer delete(int , int C)
+his method removes characters from ith position till j53th
position in the /tring?uffer
sb2delete(,');
public Stringuffer re.erse()
+his method reverse the characters in the /tring?uffer
Stringuffer sb!new Stringuffer(=abc?);
sb2re.erse();
System2out2println(sb);
8/9/2019 Java notes By Pradeep Goud
http://slidepdf.com/reader/full/java-notes-by-pradeep-goud 45/45
public int length()
public int inde*+f(String str)
public int lastnde*+f(String str)
public Stringuffer replace(int , int C,String str)
+his replace characters from % to j53, by the string 1str2 in the
/tring?uffer object
Stringuffer sb!new Stringuffer(=>igh $ost?);
sb2replace(,",?low?);
System2out2println(sb);