270 likes | 354 Views
Fundamental Data Types Lesson - 2. CSM-Java Programming-I Spring,2005. Objectives Review of last class. Identifiers and primitive data types. Operators. CSM-Java Programming-I Lesson-1. Identifiers.
E N D
Fundamental Data Types Lesson - 2 CSM-Java Programming-I Spring,2005
Objectives • Review of last class. • Identifiers and primitive data types. • Operators. CSM-Java Programming-I Lesson-1
Identifiers • Java identifiers are used for names of variables, constants and labels. • They must start with a letter, a currency symbol or underscore (_) followed by letters, digits or both. • Java language keywords cannot be used as identifiers. Eg: int, private, void, new, for… CSM-Java Programming-I Lesson-1
Primitive Types • boolen either true or false • char 16-bits • byte 8-bits • short 16-bits • int 32-bits • long 64-bits • float 32-bits • double 64-bits CSM-Java Programming-I Lesson-1
Literals A literal is a constant value that may be assigned to a primitive type. • Boolean – true and false • Integer – Integer constants are strings of octal, decimal or hexadecimal digits. E.g.: 29-decimal 035-octal 0x1D, 0X1d-hexadecimal Suffix a L to indicate a long (64 bit) literal) CSM-Java Programming-I Lesson-1
Literals • Floating-Point – Floating point literals are expressed as decimal numbers. E.g.: 18., 1.8e1, .18E2 all denote the same number. A floating-point literal with no F or D suffix defaults to double type. • Character – Character literals appear between single quotes. E.g.: ‘Q’. CSM-Java Programming-I Lesson-1
Literals • Character – Certain special characters can be represented by an escape sequence. E.g.: \n for newline, \t for tab, \b for backspace. • Strings – String literals appear between double quotes. E.g.: “Hi”. A string literal references an object of type String. CSM-Java Programming-I Lesson-1
Strings Strings – String is a sequence of characters. Strings are objects of the String class. The number of characters in a string is called the length of the string. Eg: int n = message.length(); Strings can be concatenated, by using the + operator. String name = “dave”; String message = “Hello, “ + name; Whenever one of the arguments of the + operator is a string, the other argument is converted to a string. CSM-Java Programming-I Lesson-1
Strings • If a string contains the digits of a number, use the Integer.parseInt or Double.parseDouble method to obtain the number value. • Use the substring() method to extract a part of a string. Eg: String greeting = “Hello World!”; String sub = greeting.substring(0,4); //sub = “Hell” • String positions are counted starting with 0. CSM-Java Programming-I Lesson-1
final Keyword • Any number that is not completely self-explanatory should be declared as a named constant. • A final variable is a constant. Once its value has been set, it cannot be changed. • Use named constants to make your programs easier to read and maintain. • Eg: final int DAYS_PER_YEAR = 365; CSM-Java Programming-I Lesson-1
static Keyword • Java numbers are not objects, so you cannot invoke a method on a number. Eg: To calculate sqrt or power. • A static method does not operate on an object. • Eg: Math.sqrt(x); Math.round(y); CSM-Java Programming-I Lesson-1
Type Conversion • When an assignment is made to a variable, the types of either side of the assignment must be compatible. Eg 1: double total = “a lot”; //Error Eg 2: int dollars = 2; double total = dollars; // Correct – This is called implicit conversion. Eg 3: double dollars = 2; int total = dollars; // Error CSM-Java Programming-I Lesson-1
Type Conversion Eg 3: double dollars = 2; int total = dollars; // Error Casting: You must convert the floating-point value to integer with a cast. (typename) expression; int total = (int) dollars; CSM-Java Programming-I Lesson-1
Type Conversion Loss of Information Eg 1: double total = 13.75; int pennies = (int)(total *100); Use the Math.round() method to round a floating-point number to the nearest integer to avoid the loss of information. CSM-Java Programming-I Lesson-1
Operators • Arithmetic Operators +, -, *, /, % (remainder) If both the arguments of the / operator are integers, the result is an integer and the remainder is discarded. Eg: 7/4 = 1; 3 is discarded. CSM-Java Programming-I Lesson-1
Operators • Arithmetic Operators +, -, *, /, % (remainder) • Eg: 7.0/4.0, 7/4.0, 7.0/4 all yield 1.75 • The % operator computes the remainder of a division. Eg: 7 % 4 = 3 CSM-Java Programming-I Lesson-1
Common Errors • Integer Division Eg: int s1 = 5; int s2 = 6; double avg = (s1 + s2) / 3; // Error double avg = (s1 + s2) / 3.0; //Correct • Unbalanced Parentheses 1.5 * ((- (b – Math.sqrt(b*b – 4*a*c)) / (2*a)) CSM-Java Programming-I Lesson-1
Operators • Increment and decrement Operators ++, -- i++ is equivalent to i= i+1 Increment and decrement operators can be either prefix of postfix operators (they can appear either before or after what they operate on). CSM-Java Programming-I Lesson-1
Increment and decrement Operators • If increment or decrement operators appear before (prefix), the operation is applied before the value of the expression is returned. • If increment or decrement operators appear after (postfix), the operation is applied after the value of the expression is used. CSM-Java Programming-I Lesson-1
Example: Increment and decrement Operators public class Ex1 { public static void main(String args[]) { int x = 10; System.out.println(++x); System.out.println(x++); System.out.println(x); } } CSM-Java Programming-I Lesson-1
Relational and Equality Operators • > greater than • >= greater than or equal to • < less than • <= less than or equal to • == equal to – equality op. • != not equal to – equality op. The unary operator ! Inverts a boolean; !true is same as false Equality operators opearte only on boolean values CSM-Java Programming-I Lesson-1
Logical Operators • && - conditional AND • || - conditional OR CSM-Java Programming-I Lesson-1
Example1: Logical Operators if (x && y) { if (y || z) { Do something } } The inner if is executed only if both x and y are true. If x is false, y is not evaluated. The body of inner if is excecuted if either y or z is true. If y is true, z is not evaluated. CSM-Java Programming-I Lesson-1
Ternary Operator x = (a ? b : c) is equivalent to if (a) x = b; else x = c; CSM-Java Programming-I Lesson-1
Assignment Operator a +=10; Same as a = a+10; Same for all arithmetic operators CSM-Java Programming-I Lesson-1
Operator Precedence The order of precedence of operators from highest to lowest is: • postfix [] ,., (params), expr++, expr— • Unary ++expr, –expr, +expr, –expr ! • Multiplicative * / % • Additive + - • Relational <, >, <=, >= • Equality ==, != • Logical AND && • Logical OR || • Ternary ?: • Assignment =, +=, -=, *=, /=, %= CSM-Java Programming-I Lesson-1
Operator Precedence • Operators with the same precedence appear on the same line in the table. • Precedence can be overridden using parentheses • Operands to the operators will be evaluated left-to-right, except for &&, ||, ?:. Eg: x+y+z. First evaluates x, then y, adds the values together, evaluates z and adds that to the previous result. CSM-Java Programming-I Lesson-1