Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 8 Mixed type operations • Automatic conversions – (float and double) double – (byte, short, int) int – (byte, short, int, long) long • A computer can only operate on data of the same data type • The conversion rule is applied at the moment that the arithmetic operation is performed Today • • • • Expressions with mixed type variables Increment operators Characters (new type) Strings (new type) Numeric literals (constants) • A numeric literal is a constant value that appears in a Java program. – 3.14159265358979 1776 – 2012 • Has type • Demo Exercise3.java Constant synax • final datatype CONSTANT_NAME = value ; Short hand operators Assignment operator • • • • • = // assignment operator var = expr; // syntax Has lowest priority c = b = a = 4 + 2; // nested assignment ? • Demo AssignExpr04.java Increment / Decrement operators • Difference between a++ and ++a • The ++ and -- operators have higher priority than any binary arithmetic operators in Java • Demo Increment01.java Character type (char) • built-in (primitive) data type of Java • is used to represent alpha-numerical information (characters) inside the computer • uses the Unicode to encode characters from many different kinds of languages in the world • uses 2 bytes of memory to store the Unicode value • represented as an integer • The Unicode includes the ASCII code which is used to encode English characters (recall HW1) ASCII and Unicode Why ‘quotes’? • Character literals – ‘a’ – ‘b’ – ‘c’ // // // Unicode code value 97 Unicode code value 98 Unicode code value 99 • Without quotes will be confused with identifier ‘a’ + 1 = ? • Demo: Char01B.java Syntax • Define char variable – char a; // define variable a • Assign a value – a = ‘c’; // store ASCII code // of c in variable a Printing difference int vs. char Converting int char Converting lowercase to uppercase Converting lowercase to uppercase • Demo: Char05a.java String • A string is a sequence of characters enclosed between the double quotes "...“ • Each character in a string is of the type char and uses the Unicode as encoding method • *not* a built in data type String Class / Data type • String Class ( online doc ) – Information – Operations // sequence of characters // concatenation, sub-string, .. String literals • “Hello World” • May contain escape character – \n – \t // new line // tab character • The escape character is usually used to express "unprintable" characters Escape characters Syntax • String NameOfVariable ; // define string variable • The class String announces the variable definition clause • The NameOfVariable is an identifier which is the name of the variable. • A String typed variable can store the location (address) of a sequence characters (string) • Demo: String01.java How strings are stored? • “Hello” • “Good bye” // // 5 characters 8 characters • Can not fixed size variable Location + length Location + length • Sentinel = a special character (symbol) that denotes the end of a string • The sentinel used in programming languages to denote the end of a string is usually the NULL character with Unicode value 0. Location + sentinel String storage • Which method is better? – Location + length – Location + sentinel