Oop’s Using java


Download 0.74 Mb.
bet10/12
Sana15.11.2023
Hajmi0.74 Mb.
#1775298
1   ...   4   5   6   7   8   9   10   11   12
Bog'liq
OOP’s Using JAVA Module 2

String Comparison

  • We can compare String in Java on the basis of content a
  • It is used in 
    • authentication (by equals() method), 
    • sorting (by compareTo() method), 
    • reference matching (by == operator).
  • There are three ways to compare String in Java:
  • == :operator always ment for reference comparision
    • String : CompareTo() vs .equals()
    • equals () ------ Boolean

      Ratan Ratan=True

      Rattan Anu=false

      CompareTo() ------ int R=82 A=65

      Ratan Ratan=0

      Ratan Anu=17

      Anu Ratan=-17

Example of “String Comparision” Program 12

class compare{

 public static void main(String args[]){

String s1=“anu";

String s2=“anu";

System.out.println(s1.equals(s2)); System.out.println(s1==s2);

String s3=new String(“bharath");

String s4=new String(“bharath");

System.out.println(s3.equals(s4)); 

System.out.println(s3==s4);

}

}

  • class compare{
  •  public static void main(String args[]){
  • String s1=“Anu";
  • String s2=“Ratan";
  • System.out.println(s1.compareTo(s2));//17
  • System.out.println(s2.compareTo(s1));///-17

Modifying a String

  •  Methods for modifying a String objects.
    • substring()
      • susubstring(int beginIndex)
      • bstring(int beginIndex, int endIndex) 
    • concat() -- We can concatenate two strings using concat( )
    • replace()
      • String replace(char original, char replacement)
    • trim()
    • IndexOf()
    • lastIndexOf()
    • toUpperCase()
    • toLowerCase()

class Methods{

class Methods{


Download 0.74 Mb.

Do'stlaringiz bilan baham:
1   ...   4   5   6   7   8   9   10   11   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling