SlideShare uma empresa Scribd logo
1 de 23
TEST (MODULE:- 1 and 2)
 What are command line arguments? Write a program in JAVA to print

  Fibonacci series using command line arguments?      [10]

 Create a class employee with data members empid, empname, designation

  and salary. Write methods for:-                     [10]

  get_employee()- To take user input.

  show_grade()- To display grade of employee based on salary.

  show_employee()- To show employee details.

 Write a note on:-                                   [10]
   Java Virtual Machine.

   Nesting of Methods with example
STRINGS
   IN
 JAVA
          - Ankita Karia
STRINGS
 Strings represent a sequence of characters.
 The easiest way to represent a sequence of characters in
  JAVA is by using a character array.
 char Array[ ] = new char [5];             Character arrays are
                                              not good enough to
                                             support the range of
                                             operations we want to
                                              perform on strings.


    In JAVA strings are class objects and implemented using
    two classes:-
        String
        StringBuffer.
    In JAVA strings are not a character array and is not NULL
    terminated.
DECLARING & INITIALISING

•   Normally, objects in Java are created with the new keyword.

    String name;
     name = new String("Craig");

                                OR
     String name= new String("Craig");


•   However, String objects can be created "implicitly":
                 String name;
                 name = "Craig";
The String Class


String objects are handled specially by the compiler.
 String is the only class which has "implicit" instantiation.
The String class is defined in the java.lang package.
Strings are immutable.
 The value of a String object can never be changed.
 For mutable Strings, use the StringBuffer class.
DYNAMIC INITIALIZATION
           OF STRINGS
BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));

    String city = br.readLine();    Give throws
                                    IOException
                                       beside
                                      function
                                        name

Scanner sc=new Scanner(System.in);
    String state=sc.nextLine();
   String state1=sc.next();
STRING CONCATENATION
    JAVA string can be concatenated
     using + operator.
 String name="Ankita";
 String surname="Karia";
 System.out.println(name+" "+surname);

STRING Arrays
•An array of strings can also be created
        String cities [ ] = new String[5];
• Will create an array of CITIES of size 5 to
hold string constants
String Methods
   The String class contains many useful methods for string-
    processing applications.
    ◦ A String method is called by writing a String object, a dot, the name of
      the method, and a pair of parentheses to enclose any arguments

    ◦ If a String method returns a value, then it can be placed anywhere that a
      value of its type can be used

      String greeting = "Hello";         String method
      int count = greeting.length();
      System.out.println("Length is " + greeting.length());

    ◦ Always count from zero when referring to the position or index of a
      character in a string
String Indexes
Some Methods in the Class String (Part 1
of 8)




                        1-10
Some Methods in the Class String (Part 2
of 8)
Some Methods in the Class String (Part 3
of 8)
Some Methods in the Class String (Part 4 o
8)
Some Methods in the Class String (Part 5 o
8)
Some Methods in the Class String (Part 6
of 8)
Some Methods in the Class String (Part 7 o
STRING BUFFER

               CLASS
    STRINGBUFFER class creates strings flexible length that
    can be modified in terms of both length and content.

   STRINGBUFFER may have characters and substrings
    inserted in the middle or appended to the end.

   STRINGBUFFER automatically grows to make room for
    such additions

                      Actually STRINGBUFFER has more
                     characters pre allocated than are actually
                        needed, to allow room for growth
STRING BUFFER
              CONSTRUCTORS
   String Buffer():- Reserves room fro 16 characters
    without reallocation


   StringBuffer(int size):- Accepts an integer argunent
    that explicilty sets the size of the buffer


   StringBuffer(String str):- Accepts STRING argument
    that sets the initial contents of the STRINGBUFFER and
    allocated room for 16 additional characters.
STRING BUFFER FUNCTIONS
   length():-Returns the current length of the string.

   capacity():- Returns the total allocated capacity.

   void ensureCapacity():- Preallocates room for a certain

                    number of characters.
   void setLength(int len):- Sets the length of the string s1
                              to len.
               If len<s1.length(), s1 is truncated.
               If len>s1.length(), zeros are added to s1.
   charAt(int where):- Extracts value of a single character.

   setCharAt(int where, char ch):- Sets the value of character

                     at specified position.
STRING BUFFER FUNCTIONS
   append(s2):- Appends string s2 to s1 at the end.

   insert(n,s2 ):- Inserts the string s2 at the position n of the
    string s1

   reverse():- Returns the reversed object on when it is called.

   delete(int n1,int n2):- Deletes a sequence of characters
    from the invoking object.

                 n1      Specifies index of first character to remove

                          Specifies index one past the lastcharacter to
                  n2      remove

   deleteCharAt(int loc):- Deletes the character at the index
    specified by loc.
STRING BUFFER FUNCTIONS
   replace(int n1,int n2,String s1):- Replaces one set of
    characters with another set.

   substring(int startIndex):- Returns the substring that starts
    at starts at startIndex and runs to the end.

   substring(int startIndex, int endIndex):- Returns the
    substring that starts at starts at startIndex and runs to the
    endIndex-1




VECTOR

Mais conteúdo relacionado

Mais procurados

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 

Mais procurados (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java threads
Java threadsJava threads
Java threads
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Java program structure
Java program structureJava program structure
Java program structure
 
Java Strings
Java StringsJava Strings
Java Strings
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java package
Java packageJava package
Java package
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 

Destaque

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi_Kant_Sahu
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
Fahim Adil
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
myrajendra
 

Destaque (20)

Java String
Java String Java String
Java String
 
String Handling
String HandlingString Handling
String Handling
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Java strings
Java   stringsJava   strings
Java strings
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Strings
StringsStrings
Strings
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
String java
String javaString java
String java
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Chapter 3:Programming with Java Operators and Strings
Chapter 3:Programming with Java Operators and  StringsChapter 3:Programming with Java Operators and  Strings
Chapter 3:Programming with Java Operators and Strings
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
StringTokenizer in java
StringTokenizer in javaStringTokenizer in java
StringTokenizer in java
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
 
Java: strings e arrays
Java: strings e arraysJava: strings e arrays
Java: strings e arrays
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Save Time, Money and Bloodshed with Soft System Discovery
Save Time, Money and Bloodshed with Soft System DiscoverySave Time, Money and Bloodshed with Soft System Discovery
Save Time, Money and Bloodshed with Soft System Discovery
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 

Semelhante a Strings in Java

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
fedcoordinator
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 

Semelhante a Strings in Java (20)

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
07slide
07slide07slide
07slide
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
StringBuffer.pptx
StringBuffer.pptxStringBuffer.pptx
StringBuffer.pptx
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
package
packagepackage
package
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
String handling
String handlingString handling
String handling
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
Java
JavaJava
Java
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Java R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdfJava R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdf
 
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.docxJava R20 - UNIT-5.docx
Java R20 - UNIT-5.docx
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings in c
Strings in cStrings in c
Strings in c
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
 

Mais de Abhilash Nair (20)

Sequential Circuits - Flip Flops
Sequential Circuits - Flip FlopsSequential Circuits - Flip Flops
Sequential Circuits - Flip Flops
 
VHDL Part 4
VHDL Part 4VHDL Part 4
VHDL Part 4
 
Designing Clocked Synchronous State Machine
Designing Clocked Synchronous State MachineDesigning Clocked Synchronous State Machine
Designing Clocked Synchronous State Machine
 
MSI Shift Registers
MSI Shift RegistersMSI Shift Registers
MSI Shift Registers
 
VHDL - Enumerated Types (Part 3)
VHDL - Enumerated Types (Part 3)VHDL - Enumerated Types (Part 3)
VHDL - Enumerated Types (Part 3)
 
VHDL - Part 2
VHDL - Part 2VHDL - Part 2
VHDL - Part 2
 
Introduction to VHDL - Part 1
Introduction to VHDL - Part 1Introduction to VHDL - Part 1
Introduction to VHDL - Part 1
 
Feedback Sequential Circuits
Feedback Sequential CircuitsFeedback Sequential Circuits
Feedback Sequential Circuits
 
Designing State Machine
Designing State MachineDesigning State Machine
Designing State Machine
 
State Machine Design and Synthesis
State Machine Design and SynthesisState Machine Design and Synthesis
State Machine Design and Synthesis
 
Synchronous design process
Synchronous design processSynchronous design process
Synchronous design process
 
Analysis of state machines & Conversion of models
Analysis of state machines & Conversion of modelsAnalysis of state machines & Conversion of models
Analysis of state machines & Conversion of models
 
Analysis of state machines
Analysis of state machinesAnalysis of state machines
Analysis of state machines
 
Sequential Circuits - Flip Flops (Part 2)
Sequential Circuits - Flip Flops (Part 2)Sequential Circuits - Flip Flops (Part 2)
Sequential Circuits - Flip Flops (Part 2)
 
Sequential Circuits - Flip Flops (Part 1)
Sequential Circuits - Flip Flops (Part 1)Sequential Circuits - Flip Flops (Part 1)
Sequential Circuits - Flip Flops (Part 1)
 
FPGA
FPGAFPGA
FPGA
 
FPLDs
FPLDsFPLDs
FPLDs
 
CPLDs
CPLDsCPLDs
CPLDs
 
CPLD & FPLD
CPLD & FPLDCPLD & FPLD
CPLD & FPLD
 
CPLDs
CPLDsCPLDs
CPLDs
 

Último

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Strings in Java

  • 1. TEST (MODULE:- 1 and 2)  What are command line arguments? Write a program in JAVA to print Fibonacci series using command line arguments? [10]  Create a class employee with data members empid, empname, designation and salary. Write methods for:- [10] get_employee()- To take user input. show_grade()- To display grade of employee based on salary. show_employee()- To show employee details.  Write a note on:- [10]  Java Virtual Machine.  Nesting of Methods with example
  • 2. STRINGS IN JAVA - Ankita Karia
  • 3. STRINGS  Strings represent a sequence of characters.  The easiest way to represent a sequence of characters in JAVA is by using a character array. char Array[ ] = new char [5]; Character arrays are not good enough to support the range of operations we want to perform on strings. In JAVA strings are class objects and implemented using two classes:- String StringBuffer. In JAVA strings are not a character array and is not NULL terminated.
  • 4. DECLARING & INITIALISING • Normally, objects in Java are created with the new keyword. String name; name = new String("Craig"); OR String name= new String("Craig"); • However, String objects can be created "implicitly": String name; name = "Craig";
  • 5. The String Class String objects are handled specially by the compiler. String is the only class which has "implicit" instantiation. The String class is defined in the java.lang package. Strings are immutable.  The value of a String object can never be changed. For mutable Strings, use the StringBuffer class.
  • 6. DYNAMIC INITIALIZATION OF STRINGS BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String city = br.readLine(); Give throws IOException beside function name Scanner sc=new Scanner(System.in); String state=sc.nextLine(); String state1=sc.next();
  • 7. STRING CONCATENATION  JAVA string can be concatenated using + operator. String name="Ankita"; String surname="Karia"; System.out.println(name+" "+surname); STRING Arrays •An array of strings can also be created String cities [ ] = new String[5]; • Will create an array of CITIES of size 5 to hold string constants
  • 8. String Methods  The String class contains many useful methods for string- processing applications. ◦ A String method is called by writing a String object, a dot, the name of the method, and a pair of parentheses to enclose any arguments ◦ If a String method returns a value, then it can be placed anywhere that a value of its type can be used String greeting = "Hello"; String method int count = greeting.length(); System.out.println("Length is " + greeting.length()); ◦ Always count from zero when referring to the position or index of a character in a string
  • 10. Some Methods in the Class String (Part 1 of 8) 1-10
  • 11. Some Methods in the Class String (Part 2 of 8)
  • 12. Some Methods in the Class String (Part 3 of 8)
  • 13. Some Methods in the Class String (Part 4 o 8)
  • 14. Some Methods in the Class String (Part 5 o 8)
  • 15. Some Methods in the Class String (Part 6 of 8)
  • 16. Some Methods in the Class String (Part 7 o
  • 17.
  • 18. STRING BUFFER  CLASS STRINGBUFFER class creates strings flexible length that can be modified in terms of both length and content.  STRINGBUFFER may have characters and substrings inserted in the middle or appended to the end.  STRINGBUFFER automatically grows to make room for such additions Actually STRINGBUFFER has more characters pre allocated than are actually needed, to allow room for growth
  • 19. STRING BUFFER CONSTRUCTORS  String Buffer():- Reserves room fro 16 characters without reallocation  StringBuffer(int size):- Accepts an integer argunent that explicilty sets the size of the buffer  StringBuffer(String str):- Accepts STRING argument that sets the initial contents of the STRINGBUFFER and allocated room for 16 additional characters.
  • 20. STRING BUFFER FUNCTIONS  length():-Returns the current length of the string.  capacity():- Returns the total allocated capacity.  void ensureCapacity():- Preallocates room for a certain number of characters.  void setLength(int len):- Sets the length of the string s1 to len. If len<s1.length(), s1 is truncated. If len>s1.length(), zeros are added to s1.  charAt(int where):- Extracts value of a single character.  setCharAt(int where, char ch):- Sets the value of character at specified position.
  • 21.
  • 22. STRING BUFFER FUNCTIONS  append(s2):- Appends string s2 to s1 at the end.  insert(n,s2 ):- Inserts the string s2 at the position n of the string s1  reverse():- Returns the reversed object on when it is called.  delete(int n1,int n2):- Deletes a sequence of characters from the invoking object. n1 Specifies index of first character to remove Specifies index one past the lastcharacter to n2 remove  deleteCharAt(int loc):- Deletes the character at the index specified by loc.
  • 23. STRING BUFFER FUNCTIONS  replace(int n1,int n2,String s1):- Replaces one set of characters with another set.  substring(int startIndex):- Returns the substring that starts at starts at startIndex and runs to the end.  substring(int startIndex, int endIndex):- Returns the substring that starts at starts at startIndex and runs to the endIndex-1 VECTOR