Introduction to Java Part 4 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Introduction to Java Part 4

Description:

Instance variables are defined in the class and are available for all ... (also called a static variable) is flagged by the static modifier in its declaration: ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 25
Provided by: D416
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java Part 4


1
Introduction to Java Part 4
  • Informatik III

2
Variables
3
Instance Variables
  • Instance variables are defined in the class and
    are available for all methods
  • this. allows to access instance variables
  • Example
  • public class Name
  • public String
    firstName
  • public String
    lastName

4
Local variables
  • Formal parameters of methods, and variables
    declared inside the bodies of methods, are local
    variables.
  • These are a third kind of variable in Java they
    are neither instance variables or class
    variables.
  • Only available in the defining block which is
    indicated via

5
Class Variables
  • Besides instance variables, a class may contain
    global variables that are not associated with
    any instance. These variables are the same for
    all classes / instances.
  • A class variable (also called a static variable)
    is flagged by the static modifier in its
    declaration
  • class Potato
  • public String name
  • static public int num 0
  • //
    Class variablenumber of potatoes.

6
Modifiers
7
Access Modifiers
8
Access Modifiers (for methods, fields / classes)
  • For this training all instance variables should
    be private and all classes and methods should be
    public

public class Math private int number
public int getNumber() return number

9
Addional Modifiers for fields
  • Additional possible field Modifiers are
  • staticthis is a class variable see earlier.
  • finalthis field cannot be modified after it is
    initialized.
  • transientthe value of this field will not be
    included in a serialized representation of an
    instance.
  • volatileany cached copy of the field maintained
    by an individual thread will be reconciled with
    the master copy every time the field is accessed

10
Additional modifiers of Classes
  • Additional class modifiers are
  • defaultthe class may be used by code in current
    package
  • abstractthe class contains abstract methods
    without implementation (abstract classes will
    have subclasses that define implementation of
    methods).
  • finalthis class cannot have a subclass
  • strictfpall intermediate results in all float or
    double expressions appearing in the class have
    strict IEEE 754 exponents.
  • staticdefines that the class is static, e.g.
    class can be accessed without an instance

11
Additional modifiers of Methods
  • Additional method modifiers are
  • abstractthe method has no implementation
    heredeclaration has a semicolon in place of a
    body.
  • staticthis is a class method see earlier.
  • finalthis method cannot be overriden see later.
  • synchronizedother synchronized methods are
    locked out while this method is executing see
    later.
  • nativethe implementation of this method is given
    in a platform-dependent language. Declaration
    has a semicolon in place of a body.
  • strictfpintermediate results in all float or
    double expressions appearing in the body have
    strict IEEE 754 exponents.

12
Java Language Basics
13
Java Keywords
  • null, true, and false are literals with special
    meaning

14
Java Expression
15
Expressions
  • For assignment statements you use , ,
  • int i 5 // i has the value of 5
  • i 1 // equals i i 1
  • Arithmetic uses the familiar - /
  • Use and to add or substract one value
  • i // adds one to i
  • i-- // substract one from i
  • Java has boolean operators !
  • Java has comparisons lt lt ! gt gt

16
Boolean expression
  • A boolean expression is a logical expression
    which is either true or false
  • (5 gt 4) -gt false
  • (5 5) -gt true
  • means and and means or
  • (5gt4) (4gt5) -gt true
  • (5gt4) (4gt5) -gt false
  • Primitives Variables can use while Reference
    Types usually uses equals
  • String s hello s.equals(hello) -gt true
    s.equals(Hello) -gt false

17
Java Control Structure
18
Control statements - Overview
  • if (boolean expression) statement
  • if (boolean expression) statementelse
    statement(s)
  • while (boolean expression) statement(s)
  • do statement(s) while (boolean expression)
  • for (int i 0 i lt max i) statements

19
If Statements I
  • If allows the conditional execution of
    statements, e.g. if (person.getAgegt 18)
    drinkABeer
  • Conditional execution of statements
  • if (some Boolean expression)
  • statements to be executed if true

20
If Statements - II
  • If allows an else statements which is executed if
    the if statement is not truee.
  • if (some Boolean expression)
  • statements to be executed if true
  • else
  • statements to be executed if false
  • You can also nest several if statements
  • if (some Boolean expression) . . .
  • else if (another Boolean expression) .
    . .
  • else . . .

21
while Loop Constructs
  • while loop
  • while (boolean expression)
  • Stuff to do
  • Example

public void doStuff() int i 0
int j 0 while(i lt 10)
j j 1 i

22
The for loop
  • for (declaration1 booleanExpression
    expressionList2)
  • Statements to do
  • The declaration declaration1 is executed at start
    of the loop, expressionList2 is evaluated after
    every iteration, and the loop terminates when
    booleanExpression is false.
  • Typical example
  • for (int i 0 i lt 10 i)
  • j j 1

23
Switch
  • Java also introduces the try statement
  • Will evaluate n and go to the corresponding case
    statement

switch (n) case 0 m n - 1 break case
1 m n 1 case 3 m m n break
default m -n break
24
break and continue
  • Unlabeled break statement immediately exits the
    enclosing switch, while, do or for construct
  • while (true)
  • if (booleanExpression) break
  • The continue statement skips to the next
    iteration of the enclosing while, do or for. The
    statements after continue are not executed anymore
Write a Comment
User Comments (0)
About PowerShow.com