Oop’s Using java


Download 0.74 Mb.
bet8/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 in JAVA

  • The java.lang.String class is used to create a string object.
  • It is possible to create string object in two ways
  • //creating a string obj without using new operator.
  • String str1=“Ratan”;

    String str2=“bharath”;

    String str3=“Ratan”;

    The objects are created in scp memory

    str1,str3 …….. Ratan

    str2 ……….Bharath

  • In scp memory just before object creation it is always checking previous objects.
  • If the previous object is not available it will create the new object.

    If the previous object is available with the same content then the reference variable pointing to existing object.

//creating a string obj by using new operator.

  • //creating a string obj by using new operator.
  • String str1=“Ratan”;

    String str2=“bharath”;

    String str3=“Ratan”;

    The objects are created in heap memory

    When we create object in heap area instead of checking previous objects it creates directly new object

    str1 …….. Ratan

    str2 ……….Bharath

    str3 ………Ratan

Example of “String” Program 10

  • class StringExample{
  • public static void main(String args[]){
  • String s1=“bharath";
  • System.out.println(s1);
  • String s2=new String(“bharath”);
  • System.out.println(s2);
  • }
  • }

String Handling

  • The Java String class length() method finds the length of a string. For example,the length of a string can be found with length() method.
  • public class Length{
  • public static void main(String args[]){
  • String s1=“Hello";
  • String s2=“Helloworld";
  • System.out.println(s1.length());
  • System.out.println(s2.length());
  • }
  • }

Example of “String Length” Program 11

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