2.9. Character Data Type and Operations .ps



5031740-3810000Islamic University of Gaza (IUG)Faculty of Engineering Computer Engineering DepartmentJava Programing (1) Lab.ECOM 2114Eng. Asma Obeid Lab 3Data Types and OperationsContinueShorthand Operators:Java allows you to combine assignment and addition operators using a shorthand operator. For example,i = i + 8; can be written as: i += 8;The += is called the addition assignment operator. Other shorthand operators are shown in following tables.Shorthand OperatorsOperatorNameExampleEquivalent+=Addition assignmenti += 8i = i + 8-=Subtraction assignmentf -= 8.0f = f - 8.0*=Multiplication assignmenti*= 8i = i * 8/=Division assignmenti /= 8i = i / 8%=Remainder assignmenti %= 8i = i % 8 Increment and Decrement OperatorsOperatorNameDescription++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment.var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1.--varpredecrementThe expression (––var) decrements var by 1 and evaluates to the new value in var after the decrement.var--postdecrementThe expression (var––) evaluates to the original value in var and decrements var by 1.If the operator is before (prefixed to) the variable, the variable is incremented or decremented by 1, then the new value of the variable is returned. If the operator is after (suffixed to) the variable, the original old value of the variable is returned.In this case, i is incremented by 1, then the old value of i is returned and used in the multiplication. So newNum becomes 100. If i++ is replaced by ++i as follows,i is incremented by 1, and the new value of i is returned and used in the multiplication. Thus newNum becomes 110.Casting:You can always assign a value to a numeric variable whose type supports a larger range of values; thus, for instance, you can assign a long value to a float variable. You cannot, however, assign a value to a variable of a type with smaller range unless you use type casting. Casting is an operation that converts a value of one data type into a value of another data type. Widening a type: Casting a variable of a type with a small range to a variable of a type with a larger range. Narrowing a type: Casting a variable of a type with a large range to a variable of a type with a smaller range . Widening a type can be performed automatically without explicit casting(e.g. casting int to double) . Narrowing a type must be performed explicitly(e.g double to int).float f = (float)10.1; // narrowing the double 10.1 to the float fint i = (int)f; // narrowing the float f to the int i2.9. Character Data Type and Operationschar, 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 statement assigns the digit character 4 to the char variable numChar. Note: "A" is a string'A' is a character NoteThe 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);Escape Sequences for Special CharactersAn escape sequence begins with the backslash character (\) followed by a character that has a special meaning to the compiler.Java Escape SequencesCharacter Escape SequenceNameUnicode Code\bBackspace\u0008\tTab\u0009\nLinefeed\u000A\fFormfeed\u000C\rCarriage Return\u000D\\Backslash\u005C\'Single Quote\u0027\"Double Quote\u0022Suppose you want to print the quoted message shown below:He said "Java is fun"The statement to print it should beSystem.out.println("He said \"Java is fun\"");Casting Between char and Numeric TypesA char can be cast into any numeric type, and vice versa. When an integer is cast into a char, only its lower sixteen bits of data are used; the other part is ignored. For example, see the following code:char c = (char)0XAB0041; // the lower 16 bits hex code 0041 is // assigned to cSystem.out.println(c); // c is character AWhen a floating-point value is cast into a char, the integral part of the floating-point value is cast into a char.char c = (char)65.25; // decimal 65 is assigned to tSystem.out.println(c); // c is character AWhen a char is cast into a numeric type, the character's Unicode is cast into the specified numeric type.int i = (int)'A'; // the Unicode of character A is assigned to iSystem.out.println(i); // i is 65byte b = 'a'; // implicit casting cause the Unicode of 'a' = 97 //in the rangeint i = 'a';But the following casting is incorrect, because the Unicode \uFFF4 cannot fit into a byte:byte b = '\uFFF4';To force assignment, use explicit casting, as follows:byte b = (byte)'\uFFF4';Any positive integer between 0 and FFFF in hexadecimal can be cast into a character implicitly. Any number not in this range must be cast into a char explicitly.The String TypeThe char type only represents one character. To represent a string of characters, use the data type called String. For example, the following code declares the message to be a string that has an initial value of "Welcome to Java".String message = "Welcome to Java"; // String is not primitive, it's a referential type The plus sign (+) is the concatenation operator if one of the operands is a string. // Three strings are concatenatedString message = "Welcome" + "to" + "Java";// String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2// String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s becomes SupplementBExample 1:Example 2:Getting Input from Input DialogsYou can use the showInputDialog method in the JOptionPane class to get input at runtime. When this method is executed, a dialog is displayed to enable you to enter an input value, as shown in the following figure:After entering a string, click OK to accept the input and dismiss the dialog box. The input is returned from the method as a string. You can invoke the method with four arguments, as follows:The first argument can always be null. The second argument is a string that prompts the user. The third argument is the title of the input box. The fourth argument can be JOptionPane.QUESTION_MESSAGE, which causes the icon () to be displayed in the input box.How to use Input Dialogs:One is to use a statement as shown in the example:String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE);where x is a string for the prompting message, and y is a string for the title of the input dialog box.The other is to use a statement like this one:JOptionPane.showInputDialog(x);where x is a string for the prompting message.Converting Strings to NumbersThe input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns "123". You have to convert a string into a number to obtain the input as a number.To convert a string into an int value, use the parseInt method in the Integer class, as follows:int intValue = Integer.parseInt(intString);where intString is a numeric string such as "123".To convert a string into a double value, use the parseDouble method in the Double class, as follows:double doubleValue = Double.parseDouble(doubleString);where doubleString is a numeric string such as "123.45".The Integer and Double classes are both included in the java.lang package, and thus are automatically imported.HW: Repeat the same work and Refering to text book. 2.7 & 2.9 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download