Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Fundamentals 3 Type conversion ,String.

Similar presentations


Presentation on theme: "Java Fundamentals 3 Type conversion ,String."— Presentation transcript:

1 Java Fundamentals 3 Type conversion ,String

2 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

3 Variables – Cont. Char When using the char data type, you enclose each character represented within single quotations marks. Ex: char c = ‘A’ char space = ‘ ‘ Each character is represented by a value (‘A’ is represented by the value 65) char aCharecter='A'; char aAscii =65; System.out.println(aCharecter); System.out.println(aAscii); A

4

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 Java Programming: From Problem Analysis to Program Design, Second Edition

6 Type Conversion (Casting)
Examples: (int)( ) = 14 (int)(7.9) + (int)(6.7) = 13 (double)(17) = 17.0 (double)(7) /2 = 7.0/2 = 3.5 (double)(7/2) = 3.0 (int)(7.8+(double)(15)/2) =(int)15.3 =15 double x=7.9 ,y= 6.7; int result; result=(int)( ); Java Programming: From Problem Analysis to Program Design, Second Edition

7 Type Conversion (Casting)
(int)(‘A’) = 65 (int)(‘8’) = 56 (char)(65) = ‘A’ (char)(56) = ‘8’ Java Programming: From Problem Analysis to Program Design, Second Edition

8 The class String 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 . Java Programming: From Problem Analysis to Program Design, Second Edition

9 The class String 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 9 9

10 The class String 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 Java Programming: From Problem Analysis to Program Design, Second Edition

11 Some Commonly Used String Methods
String mystr=new String("programming with Java is fun"); System.out.println(mystr); System.out.println(mystr.charAt(3)); System.out.println(mystr.indexOf('J')); System.out.println(mystr.indexOf(‘j')); programming with Java is fun g 17 -1 Java Programming: From Problem Analysis to Program Design, Second Edition 11 11

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

13 Example String mystr=new String("programming with Java is fun");
System.out.println(mystr); System.out.println(mystr.indexOf('a',10)); System.out.println(mystr.indexOf("with")); System.out.println(mystr.indexOf("ing")); programming with Java is fun 18 12 8

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

15 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 15 15

16 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 16 16


Download ppt "Java Fundamentals 3 Type conversion ,String."

Similar presentations


Ads by Google