SlideShare a Scribd company logo
1 of 19
Programming Language
Foundation
BY Dr. BABAOUSMAIL HASSEN
LECTURER AT BINJIANG COLLEGE OF NUIST
Chapter 4
Mathematical Functions,
Characters, and Strings
Continue…
Case Study:
Computing Angles of a Triangle
Write a program that prompts the user to enter the x- and y-
coordinates of the three corner points in a triangle and then
displays the triangle’s angles.
3
A
B
C
a
b
c
A = acos((a * a - b * b - c * c) / (-2 * b * c))
B = acos((b * b - a * a - c * c) / (-2 * a * c))
C = acos((c * c - b * b - a * a) / (-2 * a * b))
x1, y1
x2, y2
x3, y3
Character Data Type
In addition to processing numeric values, you can process
characters in Java.
The character data type, char, is used to represent a single
character.
A character literal is enclosed in single quotation marks.
Consider the following code:
char letter = 'A';
char numChar = '4';
◦ The first statement assigns character A to the char variable letter.
◦ The second assigns digit character 4 to the char variable numChar.
4
Character Data Type,
Computers use binary numbers internally. A character is stored in a computer as a
sequence of 0s and 1s.
Mapping a character to its binary representation is called encoding. There are
different ways to encode a character.
◦ Unicode
◦ ASCII code
Java supports Unicode, which was originally designed as a 16-bit character
encoding. A 16-bit Unicode takes two bytes, preceded by u, expressed in four
hexadecimal digits that run from u0000 to uFFFF.
Most computers use ASCII (American Standard Code for Information Interchange),
an 8-bit encoding.
Unicode includes ASCII code, with u0000 to u007F corresponding to the 128
ASCII characters.
5
Character Data Type,
NOTE: The increment and decrement operators can also be
used on char variables to get the next or preceding Unicode
character.
For example, the following statements display character b.
◦ char ch = 'a‘;
◦ System.out.println(++ch);
6
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
char letter = 'u0041'; (Unicode)
char numChar = 'u0034'; (Unicode)
Four
hexadecimal
digits.
Unicode Format
Unicode takes two bytes, preceded by u, expressed in four
hexadecimal numbers that run from 'u0000' to 'uFFFF'.
Unicode can represent 65534 characters.
7
Unicode u03b1 u03b2 u03b3 for three Greek
letters
ASCII Character Set,
ASCII Character Set is a subset of the Unicode from
u0000 to u007F
8
ASCII Code for Commonly Used Characters
9
Characters Code Value in Decimal Unicode Value
'0' to '9' 48 to 57 u0030 to u0039
'A' to 'Z' 65 to 90 u0041 to u005A
'a' to 'z' 97 to 122 u0061 to u007A
Escape Sequences
for Special Characters
10
A char can be cast into any numeric type, and vice versa.
◦ When an integer is cast into a char, only its lower 16 bits of data are used; the other
part is ignored.
◦ When a floating-point value is cast into a char, the floating-point value is first cast
into an int, which is then cast into a char.
◦ When a char is cast into a numeric type, the character’s Unicode is cast into the
specified numeric type.
11
Casting between char and
Numeric Types
Casting between char and Numeric
Types, cont.
Implicit casting can be used if the result of a casting fits into
the target variable. Otherwise, explicit casting must be used.
For example,
12
√
√
×
√
Comparing and Testing Characters
Two characters can be compared using the relational
operators just like comparing two numbers. This is done by
comparing the Unicodes of the two characters.
For example,
if (ch >= 'A'&& ch <= 'Z')
System.out.println(ch + " is an uppercase letter");
else if (ch >= 'a' && ch <= 'z')
System.out.println(ch + " is a lowercase letter");
else if (ch >= '0' && ch <= '9')
System.out.println(ch + " is a numeric character");
13
Methods in the Character Class
For convenience, Java provides the following methods in the
Character class for testing characters.
14
Method Description
isDigit(ch) Returns true if the specified character is a digit.
isLetter(ch) Returns true if the specified character is a letter.
isLetterOfDigit(ch) Returns true if the specified character is a letter or digit.
isLowerCase(ch) Returns true if the specified character is a lowercase letter.
isUpperCase(ch) Returns true if the specified character is an uppercase letter.
toLowerCase(ch) Returns the lowercase of the specified character.
toUpperCase(ch) Returns the uppercase of the specified character.
The String Type
The char type only represents one character. To represent a string of
characters, use the data type called String.
For example,
String message = "Welcome to Java ";
The String type is not a primitive type. It is known as a reference
type. Any Java class can be used as a reference type for a variable.
Reference data types will be thoroughly discussed in Chapter 9.
For the time being, you just need to know
◦ how to declare a String variable,
◦ how to assign a string to the variable,
◦ how to concatenate strings,
◦ to perform simple operations for strings.
15
Simple Methods for String Objects
16
Method Description
Returns the number of characters in this string.
Returns the character at the specified index from this string.
Returns a new string that concatenates this string with string s1.
Returns a new string with all letters in uppercase.
Returns a new string with all letters in lowercase.
Returns a new string with whitespace characters trimmed on both sides.
length()
charAt(index)
concat(s1)
toUpperCase()
toLowerCase()
trim()
Simple Methods for String Objects
Strings are objects in Java. The methods in the preceding table can only be invoked
from a specific string instance. For this reason, these methods are called instance
methods.
The syntax to invoke an instance method is
referenceVariable.methodName(arguments)
 A non-instance method is called a static method. A static method can be invoked
without using an object.
 The syntax to invoke a static method can be
ClassName.methodName(arguments)
 All the methods defined in the Math class are static methods. They are not tied to
a specific object instance. For example, the pow method in the Math class can be
invoked using.
Math.pow(2, 2.5)
17
Getting String Length
String message = "Welcome to Java";
System.out.println("The length of " + message +
" is “ + message.length( ));
18
Getting Characters from a String
String message = "Welcome to Java";
System.out.println("The first character in
message is “ + message.charAt(0));
19

More Related Content

Similar to Java Object Orientend Programming 1.pptx

© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx
© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx
© Copyright 2013 by Pearson Education, Inc. All Rights Res.docxLynellBull52
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsJamesChristianGadian
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxrohinitalekar1
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
pointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in cpointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in cssuser2d043c
 
LiangChapter4 Unicode , ASCII Code .ppt
LiangChapter4 Unicode , ASCII Code  .pptLiangChapter4 Unicode , ASCII Code  .ppt
LiangChapter4 Unicode , ASCII Code .pptzainiiqbal761
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 

Similar to Java Object Orientend Programming 1.pptx (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
 
M C6java7
M C6java7M C6java7
M C6java7
 
© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx
© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx
© Copyright 2013 by Pearson Education, Inc. All Rights Res.docx
 
Strings
StringsStrings
Strings
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Strings
StringsStrings
Strings
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Strings in c
Strings in cStrings in c
Strings in c
 
Computer programming 2 Lesson 11
Computer programming 2  Lesson 11Computer programming 2  Lesson 11
Computer programming 2 Lesson 11
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
pointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in cpointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in c
 
LiangChapter4 Unicode , ASCII Code .ppt
LiangChapter4 Unicode , ASCII Code  .pptLiangChapter4 Unicode , ASCII Code  .ppt
LiangChapter4 Unicode , ASCII Code .ppt
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
C string
C stringC string
C string
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Java Object Orientend Programming 1.pptx

  • 1. Programming Language Foundation BY Dr. BABAOUSMAIL HASSEN LECTURER AT BINJIANG COLLEGE OF NUIST
  • 3. Case Study: Computing Angles of a Triangle Write a program that prompts the user to enter the x- and y- coordinates of the three corner points in a triangle and then displays the triangle’s angles. 3 A B C a b c A = acos((a * a - b * b - c * c) / (-2 * b * c)) B = acos((b * b - a * a - c * c) / (-2 * a * c)) C = acos((c * c - b * b - a * a) / (-2 * a * b)) x1, y1 x2, y2 x3, y3
  • 4. Character Data Type In addition to processing numeric values, you can process characters in Java. The character data type, char, is used to represent a single character. A character literal is enclosed in single quotation marks. Consider the following code: char letter = 'A'; char numChar = '4'; ◦ The first statement assigns character A to the char variable letter. ◦ The second assigns digit character 4 to the char variable numChar. 4
  • 5. Character Data Type, Computers use binary numbers internally. A character is stored in a computer as a sequence of 0s and 1s. Mapping a character to its binary representation is called encoding. There are different ways to encode a character. ◦ Unicode ◦ ASCII code Java supports Unicode, which was originally designed as a 16-bit character encoding. A 16-bit Unicode takes two bytes, preceded by u, expressed in four hexadecimal digits that run from u0000 to uFFFF. Most computers use ASCII (American Standard Code for Information Interchange), an 8-bit encoding. Unicode includes ASCII code, with u0000 to u007F corresponding to the 128 ASCII characters. 5
  • 6. Character Data Type, NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. ◦ char ch = 'a‘; ◦ System.out.println(++ch); 6 char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = 'u0041'; (Unicode) char numChar = 'u0034'; (Unicode) Four hexadecimal digits.
  • 7. Unicode Format Unicode takes two bytes, preceded by u, expressed in four hexadecimal numbers that run from 'u0000' to 'uFFFF'. Unicode can represent 65534 characters. 7 Unicode u03b1 u03b2 u03b3 for three Greek letters
  • 8. ASCII Character Set, ASCII Character Set is a subset of the Unicode from u0000 to u007F 8
  • 9. ASCII Code for Commonly Used Characters 9 Characters Code Value in Decimal Unicode Value '0' to '9' 48 to 57 u0030 to u0039 'A' to 'Z' 65 to 90 u0041 to u005A 'a' to 'z' 97 to 122 u0061 to u007A
  • 11. A char can be cast into any numeric type, and vice versa. ◦ When an integer is cast into a char, only its lower 16 bits of data are used; the other part is ignored. ◦ When a floating-point value is cast into a char, the floating-point value is first cast into an int, which is then cast into a char. ◦ When a char is cast into a numeric type, the character’s Unicode is cast into the specified numeric type. 11 Casting between char and Numeric Types
  • 12. Casting between char and Numeric Types, cont. Implicit casting can be used if the result of a casting fits into the target variable. Otherwise, explicit casting must be used. For example, 12 √ √ × √
  • 13. Comparing and Testing Characters Two characters can be compared using the relational operators just like comparing two numbers. This is done by comparing the Unicodes of the two characters. For example, if (ch >= 'A'&& ch <= 'Z') System.out.println(ch + " is an uppercase letter"); else if (ch >= 'a' && ch <= 'z') System.out.println(ch + " is a lowercase letter"); else if (ch >= '0' && ch <= '9') System.out.println(ch + " is a numeric character"); 13
  • 14. Methods in the Character Class For convenience, Java provides the following methods in the Character class for testing characters. 14 Method Description isDigit(ch) Returns true if the specified character is a digit. isLetter(ch) Returns true if the specified character is a letter. isLetterOfDigit(ch) Returns true if the specified character is a letter or digit. isLowerCase(ch) Returns true if the specified character is a lowercase letter. isUpperCase(ch) Returns true if the specified character is an uppercase letter. toLowerCase(ch) Returns the lowercase of the specified character. toUpperCase(ch) Returns the uppercase of the specified character.
  • 15. The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java "; The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. Reference data types will be thoroughly discussed in Chapter 9. For the time being, you just need to know ◦ how to declare a String variable, ◦ how to assign a string to the variable, ◦ how to concatenate strings, ◦ to perform simple operations for strings. 15
  • 16. Simple Methods for String Objects 16 Method Description Returns the number of characters in this string. Returns the character at the specified index from this string. Returns a new string that concatenates this string with string s1. Returns a new string with all letters in uppercase. Returns a new string with all letters in lowercase. Returns a new string with whitespace characters trimmed on both sides. length() charAt(index) concat(s1) toUpperCase() toLowerCase() trim()
  • 17. Simple Methods for String Objects Strings are objects in Java. The methods in the preceding table can only be invoked from a specific string instance. For this reason, these methods are called instance methods. The syntax to invoke an instance method is referenceVariable.methodName(arguments)  A non-instance method is called a static method. A static method can be invoked without using an object.  The syntax to invoke a static method can be ClassName.methodName(arguments)  All the methods defined in the Math class are static methods. They are not tied to a specific object instance. For example, the pow method in the Math class can be invoked using. Math.pow(2, 2.5) 17
  • 18. Getting String Length String message = "Welcome to Java"; System.out.println("The length of " + message + " is “ + message.length( )); 18
  • 19. Getting Characters from a String String message = "Welcome to Java"; System.out.println("The first character in message is “ + message.charAt(0)); 19