CSMJava ProgrammingI Spring,2005 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CSMJava ProgrammingI Spring,2005

Description:

Review of last class. Identifiers and primitive data types. Operators. CSM-Java ... Strings String literals appear between double quotes. E.g.: 'Hi' ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: smc9
Category:

less

Transcript and Presenter's Notes

Title: CSMJava ProgrammingI Spring,2005


1
Fundamental Data Types Lesson - 2
CSM-Java Programming-I
Spring,2005
2
  • Objectives
  • Review of last class.
  • Identifiers and primitive data types.
  • Operators.

CSM-Java Programming-I Lesson-1
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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(bb 4ac)) / (2a))

CSM-Java Programming-I Lesson-1
18
Operators
  • Increment and decrement Operators
  • , --
  • i is equivalent to i i1
  • 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
19
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
20
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
21
Relational and Equality Operators
  • gt greater than
  • gt greater than or equal to
  • lt less than
  • lt 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
22
Logical Operators
  • - conditional AND
  • - conditional OR

CSM-Java Programming-I Lesson-1
23
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
24
Ternary Operator
  • x (a ? b c) is equivalent to
  • if (a)
  • x b
  • else
  • x c

CSM-Java Programming-I Lesson-1
25
Assignment Operator
  • a 10
  • Same as
  • a a10
  • Same for all arithmetic operators

CSM-Java Programming-I Lesson-1
26
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 lt, gt, lt, gt
  • Equality , !
  • Logical AND
  • Logical OR
  • Ternary ?
  • Assignment , , -, , /,

CSM-Java Programming-I Lesson-1
27
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 xyz.
    First evaluates x, then y, adds the values
    together, evaluates z and adds that to the
    previous result.

CSM-Java Programming-I Lesson-1
Write a Comment
User Comments (0)
About PowerShow.com