5-Arrays, 1 D and Multi-Dimensional, Enhanced For Loop, Strings, Wrapper Classes-14!08!2023
5-Arrays, 1 D and Multi-Dimensional, Enhanced For Loop, Strings, Wrapper Classes-14!08!2023
• java.util.Arrays class
• contains static methods for sorting and searching arrays, comparing
arrays, and filling array elements.
Copying Arrays using System.arraycopy
o Class java.lang.String
o Class java.lang.StringBuffer
o Class java.lang.Character
o Class java.util.StringTokenizer
All classes has its own constructors and
methods
Declaration and Definition of Strings in Java
• Normally Objects in Java is created using new.
• So string is created using object of String class.
String first= new String(“ Suvilin”);
Or
String first;
first = new String(“Suvilin”);
Or
String first=“Suvilin”;
Strings are immutable. For mutable we use StringBuffer
class.
Reading String Input
• Method 1
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
String my= br.readLine();
• Method 2
Scanner sc=new Scanner(System.in);
String my=sc.nextLine();
String you=sc.next();
Java String Pool
• Class String
o Provides nine constructors
o Null constructor String() has no characters and a
length of zero
o String (array, offset, number of
characters)
String Constructor
public class StringConstructors {
public static void main( String args[] )
{
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
byte byteArray[] = { ( byte ) 'n', ( byte ) 'e', ( byte ) 'w', ( byte ) ' ', (
byte ) 'y', ( byte ) 'e', ( byte ) 'a', ( byte ) 'r' };
String s = new String( "hello" );
// use String constructors
String s1 = new String( ); //String default constructor instantiates empty string
String Constructor
String s2 = new String( s );
String s3 = new String( charArray );
String s4 = new String( charArray, 6, 3 );
String s5 = new String( byteArray, 4, 4 );
String s6 = new String( byteArray );
// append Strings to output
String output = "s1 = " + s1 + "\ns2 = " + s2 + "\ns3 = " + s3 + "\ns4 =
" + s4 + "\ns5 = " + s5 + "\ns6 = " + s6;
}
}
String Methods length, charAt and getChars
• Method length
o Determine String length
• Like arrays, Strings always “know” their size by using length
method(not instance variable).
• s1.length()
• Method charAt
o Get character at specific location in String
o s1.charAt( offset )
• Method getChars
o Get entire set of characters in String
o s1.getChars( start, first after, charArray, start );
String Methods length, charAt and getChars
2 int compareTo(Object o)
Compares this String to another Object.
25 int length()
Returns the length of this string.
String Methods
42 String toString()
This object (which is already a string!) is itself returned.
43 String toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
45 String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Need of Wrapper Classes
class Main {
if(aObj instanceof Integer) {
public static void main(String[]
args) {
System.out.println("An
// create primitive types object of Integer is created.");
int a = 5; }
double b = 5.65;
//converts into wrapper objects
if(bObj instanceof Double) {
Integer aObj = Integer.valueOf(a);
Double bObj = Double.valueOf(b);
System.out.println("An
object of Double is created.");
}
}
Wrapper Objects into Primitive Types-Unboxing
class Main {
public static void main(String[] args) {
// creates objects of wrapper class
Integer aObj = Integer.valueOf(23);
Double bObj = Double.valueOf(5.55);
// converts into primitive types
int a = aObj.intValue();
double b = bObj.doubleValue();
System.out.println("The value of a: " + a);
System.out.println("The value of b: " + b); }}