1 / 22

Chapter 2 – Continued Basic Elements of Java

Chapter 2 – Continued Basic Elements of Java. Chapter Objectives. Variables – Cont. Type Conversion String Class Commonly Used String Methods Parsing Numeric Strings. Variables – Cont. Float The default type of floating point numbers is double . The declaration :

Download Presentation

Chapter 2 – Continued Basic Elements of Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 – Continued Basic Elements of Java

  2. Chapter Objectives • Variables – Cont. • Type Conversion • String Class • Commonly Used String Methods • Parsing Numeric Strings

  3. Variables – Cont. • Float • The default type of floating point numbers is double . • The declaration : float rate = 15.5f ; without the f , the compiler will generate an error

  4. Variables – Cont. • Char • When using the char data type, you enclose each character represented within single quotations marks. • Ex: char c = ‘A’ char x = ‘&’ char space = ‘ ‘ • Each character is represented by a value (‘A’ is represented by the value 65)

  5. Type Conversion (Casting) Used: to change one data type to another . to avoid implicit type coercion. Syntax: (dataTypeName) expression Expression evaluated first, then the value is converted to dataTypeName

  6. Type Conversion (Casting) Examples: (int)(7.9 + 6.7) = 14 (int)(7.9) + (int)(6.7) = 13 (double)(17) = 17.0 (double)(8+3) = (double)11 = 11.0 (double)(7) /2 = 7.0/2 = 3.5 (double)(7/2) = 3.0 (int)(7.8+(double)(15)/2) = (int)15.3 =15

  7. Type Conversion (Casting) (int)(‘A’) = 65 (int)(‘8’) = 56 (char)(65) = ‘A’ (char)(56) = ‘8’

  8. The classString Contains operations to manipulate strings. String: Sequence of zero or more characters. Enclosed in double quotation marks. Is processed as a single unit . Null or empty strings have no characters. “ “ Every character has a relative position , the first character is in position 0 .

  9. The classString Java system automatically makes the class String available (i.e no need to import this class ) Example : Consider the following declaration : String sentence ; sentence = “programming with java” Java Programming: From Problem Analysis to Program Design, Second Edition 10

  10. The classString Length of the string is the number of characters in it . When determining the length of a string , blanks count . Example : “ “  has length = 0 “abc”  has length = 3 , position of a = 0 ,b= 1 , c= 2 “a boy”  has length = 5

  11. Strings and the Operator + • Operator + can be used to concatenate two strings, or a string and a numeric value or character. Example String str; intnum1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226";

  12. Strings and the Operator + • Consider the following statement: str = "The sum = " + (num1 + num2); • In this statement, because of the parentheses, you first evaluate num1 + num2. Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38";

  13. Some Commonly Used String Methods Java Programming: From Problem Analysis to Program Design, Second Edition

  14. Some Commonly Used String Methods Java Programming: From Problem Analysis to Program Design, Second Edition

  15. Some Commonly Used String Methods Java Programming: From Problem Analysis to Program Design, Second Edition

  16. Some Commonly Used String Methods Java Programming: From Problem Analysis to Program Design, Second Edition

  17. Examples on string methods String s1 , s2 , s3 ; s1 = “abcdefeg” ; System.out.println( s1.length() ); // 8 System.out.println(s1.charAt(3)); //d System.out.println(s1.indexOf(‘e’)); //4 System.out.println(s1.indexOf(“cd”)); //2 System.out.println(s1.toUpperCase()); //ABCDEFEG Java Programming: From Problem Analysis to Program Design, Second Edition

  18. Examples on string methods System.out.println(s1.substring(1 , 4)); //bcd System.out.println(s1 + “xyz”); // abcdefegxyz System.out.println( s1.replace(‘d’ ,’D’)); // abcDefeg System.out.println(s1.charAt(4) ); // e System.out.println(s1.indexOf(‘b’)); // 1 System.out.println(s1.indexOf(‘e’,5)); // 6 Java Programming: From Problem Analysis to Program Design, Second Edition

  19. Parsing Numeric Strings • Integer, Float, and Double are classes designed to convert a numeric string into a number. • These classes are called wrapper classes. • parseInt is a method of the classInteger, which converts a numeric integer string into a value of the type int. • parseFloat is a method of the classFloat and is used to convert a numeric decimal string into an equivalent value of the type float. • parseDouble is a method of the classDouble, which is used to convert a numeric decimal string into an equivalent value of the type double. Java Programming: From Problem Analysis to Program Design, Second Edition

  20. Parsing Numeric Strings • A string consisting of only integers or decimal numbers is called a numeric string. • To convert a string consisting of an integer to a value of the type int, we use the following expression: • Integer.parseInt(strExpression) • Example: • Integer.parseInt("6723") = 6723 • Integer.parseInt("-823") = -823 Java Programming: From Problem Analysis to Program Design, Second Edition

  21. Parsing Numeric Strings • To convert a string consisting of a decimal number to a value of the type float, we use the following expression: • Float.parseFloat(strExpression) • Example: • Float.parseFloat("34.56") = 34.56 • Float.parseFloat("-542.97") = -542.97 • To convert a string consisting of a decimal number to a value of the type double, we use the following expression: • Double.parseDouble(strExpression) • Example: • Double.parseDouble("345.78") = 345.78 • Double.parseDouble("-782.873") = -782.873 Java Programming: From Problem Analysis to Program Design, Second Edition

More Related