Programming in Java Text Books :

Slides:



Advertisements
Similar presentations
Java
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Strings Reading for this Lecture, L&L, 3.2. Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Chapter 7: Characters, Strings, and the StringBuilder.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Chapter 8 String Manipulation
Introduction to programming in java
String and StringBuffer classes
Strings and Text Processing
C# - Strings.
Strings.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
String Handling in JAVA
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
String and StringBuilder
Chapter 9 Strings and Text I/O
String and StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
String and StringBuilder
String methods 26-Apr-19.
String Methods.
Strings in Java.
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

Programming in Java Text Books : Herbert Schildt, “JAVA 2: Complete Reference”, 5th Edition, TMH Reference Books : Jim Keogh, “J2EE Complete Reference”. Kenarnold and James gosling : “The Java Programming Language”.

Java Strings For Example : Java String provides a lot of functionalities that can be performed on a string such as compare, concat, equals, split, length, replace, compareTo, intern, substring etc. String is basically an object that represents sequence of charcter values. An array of characters works same as java string char[] ch={'j','a','v','a',' ','w','o','r','l','d''};   String s=new String(ch); Or String s="java world"; For Example : The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.

Java Strings String is a sequence of characters. But In Java, string is an object that represents a sequence of characters. String class is used to create a string object in Java. How to create String object? There are two ways to create String object: By string literal Java String literal is created by using double quotes. By new keyword (new)

Java Strings Literals Java String literal is created by using double quotes. For Example: String s ="welcome"; Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example: String s1="Welcome"; String s2="Welcome"; //will not create new instance Note: String objects are stored in a special memory area known as string constant pool.

Java Strings by new keyword Java String is created by using new keyword. For Example: String s =new String("Welcome"); Sample Program public class StringExample{   public static void main(String args[]){   String s1="java"; //creating string by java string literal      char ch[]={'s','t','r','i','n','g','s'};   String s2=new String(ch); //converting char array to string   String s3=new String("example"); System.out.println(s1);   System.out.println(s2);   System.out.println(s3);   } }   Output : java strings example

Java String class methods The java.lang.String class provides many useful methods to perform operations on sequence of char values No. Methods Description 1. char charAt(int index) returns char value for the particular index 2. int length() returns string length 3. static String format(String format, Object... args) returns formatted string 4. static String format(Locale l, String format, Object... args) returns formatted string with given locale 5. String substring(int beginIndex) returns substring for given begin index

Java String class methods The java.lang.String class provides many useful methods to perform operations on sequence of char values No. Methods Description 6. String substring(int beginIndex, int endIndex) returns substring for given begin index and end index 7. boolean contains(CharSequence s) returns true or false after matching the sequence of char value 8. static String join(CharSequence delimiter, CharSequence... elements) returns a joined string 9. static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) 10. boolean equals(Object another) checks the equality of string with object

Java String class methods No. Methods Description 11. boolean isEmpty() checks if string is empty 12. String concat(String str) concatinates specified string 13. String replace(char old, char new) replaces all occurrences of specified char value 14. String replace(CharSequence old, CharSequence new) replaces all occurrences of specified CharSequence 15. String trim() returns trimmed string omitting leading and trailing spaces 16. String split(String regex) returns splitted string matching regex 17. String split(String regex, int limit) returns splitted string matching regex and limit 18. String intern() returns interned string

Java String class methods No. Methods Description 19. int indexOf(int ch) returns specified char value index 20. int indexOf(int ch, int fromIndex) returns specified char value index starting with given index 21. int indexOf(String substring) returns specified substring index 22. int indexOf(String substring, int fromIndex) returns specified substring index starting with given index 23. String toLowerCase() returns string in lowercase. 24. String toLowerCase(Locale l) returns string in lowercase using specified locale. 25. String toUpperCase() returns string in uppercase. 26. String toUpperCase(Locale l) returns string in uppercase using specified locale.

Java “immutable” String In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created its data or state can't be changed but a new string object is created. Example class Testimmutablestring{   public static void main(String args[]){   String s="Sachin";   s.concat(" Tendulkar"); //concat() method appends the string at the end   System.out.println(s); //will print Sachin because strings are immutable objects    }   } Output:Sachin

Output:Sachin Tendulkar Java “immutable” String In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created its data or state can't be changed but a new string object is created. Example class Testimmutablestring1{ public static void main(String args[]){ String s="Sachin"; s=s.concat(" Tendulkar"); System.out.println(s); } Output:Sachin Tendulkar But if we explicitely assign it to the reference variable, it will refer to "Sachin Tendulkar" object