Lect 7(String)

Embed Size (px)

Citation preview

  • 7/28/2019 Lect 7(String)

    1/21

  • 7/28/2019 Lect 7(String)

    2/21

    String s1=LD; - one object

    String s1=new String(LD) two objects

    Immutability Cannot change string object

  • 7/28/2019 Lect 7(String)

    3/21

    P1

    String b=yash;

    b.toUppercase();

    System.out.println(b);

    P2

    b=b.toUppercase(); System.out.println(b);

  • 7/28/2019 Lect 7(String)

    4/21

    b

    yash

    YASH

  • 7/28/2019 Lect 7(String)

    5/21

    String s1=Yash + 3+2;

    4 objects are created

    (i) Yash (ii) 3

    (iii)2

    (iv)Yash32

    S1=Yash32

    Yash

    3 2

    Yash32

  • 7/28/2019 Lect 7(String)

    6/21

    String s1=3+2+LD

    Ans : s1=5LD

    String s1=3+2+LD+4+5;

    Ans : s1=5LD45

  • 7/28/2019 Lect 7(String)

    7/21

    When u want string from other datatype u can

    use this method

    For eg:- String s1=String.valueOf(12); s1=12;

    String s2=String.valueOf(12.2); s2=12.2

    String s3=String.valueOf(true); s3=true

  • 7/28/2019 Lect 7(String)

    8/21

    Equals takes into consideration the case

    While equalIgnoreCase does not take into

    consideration the case String s1=yash;

    String s2=Yash;

    If(s1.equals(s2)){

    }

  • 7/28/2019 Lect 7(String)

    9/21

    Objects cannot be compared with ==

    Can be compared with equals method if that

    object overrides equals method of Object class Only two String literals can be compared with

    == but not String objects

    Program

  • 7/28/2019 Lect 7(String)

    10/21

    String s1=Hello;

    String s2=Hello

    Is s1==s2 true ?

    String s1=Hello;

    String s2=new String(s1);

    Is s1==s2 true ?

  • 7/28/2019 Lect 7(String)

    11/21

    For Sorting Purpose

    Returns < 0 if invoking string is less

    Returns > 0 if invoking string is moreReturns == 0 if invoking string is equal to other

    String

    Apple.compareTo(Banana) 0

    S1.compareTo(s2);

  • 7/28/2019 Lect 7(String)

    12/21

    Boolean StartsWith(String str)

    Eg. FooBar.StartsWith(Foo) // returns true

    Boolean endsWith(Bar)

    Eg . FooBar.EndsWith(Bar) // returns true

  • 7/28/2019 Lect 7(String)

    13/21

    CharAt(int location)

    For eg. S1=Hello;

    S1.CharAt(0)H

    S1.CharAt(4)o

    int Length() // for array obj it is attrib

    byte[ ] getBytes( )

    indexOf(String str) subString(int startIndex,int EndIndex)

  • 7/28/2019 Lect 7(String)

    14/21

    Trim()removes white space at the beginning

    and end of String

    toUpperCase()-converts all characters ofString to uppercase

  • 7/28/2019 Lect 7(String)

    15/21

    Can be changed .represents growable and

    rewritable string

    Important Methods:-

    Int Length( )

    Int Capacity()

    ensureCapacity(int)

  • 7/28/2019 Lect 7(String)

    16/21

    StringBuilder vs StringBuffer

    Methods inside StringBuilder are not

    synchronized Everything else is same .

    Better to use StringBuilder as it works faster

  • 7/28/2019 Lect 7(String)

    17/21

    Encapsulates runtime environment

    instance/obj cannot be directly created

    Use static(factory) method getRuntime() to get

    instance of RunTime class

    Control state and behaviour of JVM

    Applets cannot use it without raising security

    Exception

  • 7/28/2019 Lect 7(String)

    18/21

    Process Exec(String path)- runs specifiedprogram

    Void gc()calls Garbage Collector Long freeMemory()returns free memory in

    JVM Long TotalMemory()-returns total memory in the

    system Exit (int status)-shuts down currently running

    JVM LoadLibrary(String Libname)

  • 7/28/2019 Lect 7(String)

    19/21

    Instance variables

    PrintStream in , out , err

  • 7/28/2019 Lect 7(String)

    20/21

    Methods:-

    gc()

    SetSecurityManager(SecurityManager sm) SecurityManager getSecurityManager()

    Exit(int status)

    Properties getProperties()

    setErr(PrintStream p)

    setOut(PrintStream p)

    setIn(PrintStream p)

  • 7/28/2019 Lect 7(String)

    21/21

    Program