0 index
1 OOP concepts
2 Three Characteristics of a truly OOP environment
3 Encapsulation
4 Inheritance
5 Polymorphism
6 Examples: the Button classes
7 Variables in java
8 Boolean
9 Boolean example
10 Boolean comments
11 Characters (char)
12 Char Examples
13 Char Notes
14 byte
15 int
16 long
17 Examples of integer types
18 Notes on integer types
19 More on integers
20 Float
21 double
22 Casting primitives
23 postfix identifiers
24 Strings
25 Wrapper objects
26 Wrapper examples
27 Converting primitives to String
28 primitive to string examples
29 Strings to primitives
30 More on String to Primitive conversion
31 Warning about Wrapper objects!!!
32 Pass by reference, pass by value

outline
created using slideshow.cgi by Andy Harris















IUPUI Computer Science: java2/OOP in Java
1. OOP concepts
  • Objects
  • Classes - instances
  • Properties
  • Methods
  • Events
  • Ball demos



































IUPUI Computer Science: java2/OOP in Java
2. Three Characteristics of a truly OOP environment
  • Inheritence
  • Encapsulation
  • Polymorphism



































IUPUI Computer Science: java2/OOP in Java
3. Encapsulation
  • Starts with functions and procedures
  • Hide data, methods when possible
  • abstraction
  • Make each unit stand-alone
  • well defined interface



































IUPUI Computer Science: java2/OOP in Java
4. Inheritance
  • Each object is subclassed from another
  • Can have a long string of relatives
  • Inherits properties, methods, events of all parents



































IUPUI Computer Science: java2/OOP in Java
5. Polymorphism
  • Object can act in multiple ways
  • Cars, Motorcycles, Chainsaws all have start methods
  • One method, multiple parameter signatures
  • Label myLabel = new Label();
  • Label myLabel = new Label("Hi there");
  • Label myLabel = new Label("Hi there",30);



































IUPUI Computer Science: java2/OOP in Java
6. Examples: the Button classes



































IUPUI Computer Science: java2/OOP in Java
7. Variables in java
  • primitive types
  • Object types



































IUPUI Computer Science: java2/OOP in Java
8. Boolean
  • name boolean
    length 1 byte
    legal values true or false (no caps)
    default value false
    operations Can be used ONLY in boolean operations
    (=, ==, !=, !, &, &&, |, ||, ^, ?:)



































IUPUI Computer Science: java2/OOP in Java
9. Boolean example

  • boolean continue = true;
    while (continue){
    //code goes here...
    if(continue){
    //code goes here...
    continue = false
    }// end if
    } // end while



































IUPUI Computer Science: java2/OOP in Java
10. Boolean comments
  • booleans are NOT integers, they only take up one byte
  • You cannot combine integer and boolean operations
  • EG if (intVar = 10){} is NOT legal in java (thank goodness)



































IUPUI Computer Science: java2/OOP in Java
11. Characters (char)
  • name char
    length 16 bit (only ONE character)
    legal values unicode (ASCII + extensions)
    default value \u0000 (null character)
    operations any of the typical operations
    assignment
    boolean including if , while, for, switch
    it can be cast into other primative types (although it might lose some information)



































IUPUI Computer Science: java2/OOP in Java
12. Char Examples
  • char myChar = 'C'
  • myChar = 'a'



































IUPUI Computer Science: java2/OOP in Java
13. Char Notes
  • notice that you must refer to a char literal inside single quotes
  • Uppercase and lowercase letters are different
  • If you cast a char to a numeric type, the new value will reflect the unicode value of the character



































IUPUI Computer Science: java2/OOP in Java
14. byte
  • name byte
    length 8 bits
    legal values -128 to 127
    default value 0
    operations Any operations, but boolean operations ALWAYS produce a boolean result



































IUPUI Computer Science: java2/OOP in Java
15. int
  • name int
    length 32 bits
    legal values -2,147,483,648 to 2,147,483,647
    default value 0
    operations Any operations, but boolean operations ALWAYS produce a boolean result



































IUPUI Computer Science: java2/OOP in Java
16. long
  • name long
    length 64 bits
    legal values -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    default value 0
    operations Any operations, but boolean operations ALWAYS produce a boolean result



































IUPUI Computer Science: java2/OOP in Java
17. Examples of integer types
  • int num1;
  • int num2 = 3;
  • int ans;
  • long myLong = 3L;
  • long myLong = (long)num2;
  • ans = num1 + num2;



































IUPUI Computer Science: java2/OOP in Java
18. Notes on integer types
  • You cannot always be sure what type your answer will be when you compare different types. You may need to cast an answer
  • to be sure
  • Remember that integer values can be positive or negative, but cannot contain decimal values



































IUPUI Computer Science: java2/OOP in Java
19. More on integers
  • when dealing with long values, either cast explicitly to a long or use the long postfix (L)
  • java interprets this value as an int : 5
  • java interprets this value as a long: 5L
  • (note that the L is capitalized, unlike other postfixes. Lowercase 'l' looks too much like '1' (The one character)



































IUPUI Computer Science: java2/OOP in Java
20. Float
  • name float
    length 4 bytes
    legal values @ 2 to +/- 38
    default value 0.0f
    operations Can be used for assignment, comparison.
    Can be cast to other primitive types, but may lose value



































IUPUI Computer Science: java2/OOP in Java
21. double
  • name double
    length 8 bytes
    legal values @ 2 to +/- 300
    default value 0.0d
    operations Can be used for assignment, comparison.
    Can be cast to other primitive types, but may lose value



































IUPUI Computer Science: java2/OOP in Java
22. Casting primitives
  • Conversion between primitive types
  • The next line will cause an error!!
  • int answer = 54.2 + 21.7;
  • The next line will be OK
  • int answer = (int)(54.2 + 21.7);
  • of course, some detail will have been lost
  • This is different than
  • int answer = (int)54.2 + (int)21.7;



































IUPUI Computer Science: java2/OOP in Java
23. postfix identifiers
  • used for literal values
  • 2i, 3d, 7L
  • Casting will work just as well, might be more clear



































IUPUI Computer Science: java2/OOP in Java
24. Strings
  • ...are OBJECTS, not primitive types!!
  • cannot be cast
  • cannot be compared using math operators
  • (..at least and get the result you might expect)



































IUPUI Computer Science: java2/OOP in Java
25. Wrapper objects
  • classes defined to encapsulate primitives
  • start with a capital letter
  • Float is an object, float is a primitive
  • Wrappers exist for all primitive types



































IUPUI Computer Science: java2/OOP in Java
26. Wrapper examples
  • float fltPrim = 3.6f;
  • float result;
  • Float fltClass = new Float(fltPrim);
  • result = fltClass.toFloat();



































IUPUI Computer Science: java2/OOP in Java
27. Converting primitives to String
  • String.valueOf(anything);
  • Concatenation to a string will automatically call this method
  • Wrappers also contain a toString method



































IUPUI Computer Science: java2/OOP in Java
28. primitive to string examples
  • String myString = String.valueOf(5);
  • Integer myInteger = new Integer(4);
  • myString = myInteger.toString();
  • myString = Integer.toString(4);



































IUPUI Computer Science: java2/OOP in Java
29. Strings to primitives
  • Most of the wrapper objects can take a String value as their initial value, and will automatically attempt to create the proper
  • value of the appropriate type.
  • EG
  • Float myVal = new Float (txtInput.getText());
  • lblOut.setText myVal.toString();



































IUPUI Computer Science: java2/OOP in Java
30. More on String to Primitive conversion
  • There is also a .valueOf method in the wrappers to do this:
  • EG
  • Float myVal = Float.valueOf (txtInput.getText());
  • In these cases, however, there has not been a primitive defined (!)
  • The value has NEVER stood alone as a primitive. It has always been part of some kind of object.



































IUPUI Computer Science: java2/OOP in Java
31. Warning about Wrapper objects!!!
  • They cannot be used in mathematical operations
  • convert to a primitive first (floatValue(), doubleValue(), etc) first



































IUPUI Computer Science: java2/OOP in Java
32. Pass by reference, pass by value
  • All objects are passed by reference
  • All primitives are passed by value
  • To pass a primitive by value, encase it in a wrapper first



































outline

OOP concepts

Three Characteristics of a truly OOP environment

Encapsulation

Inheritance

Polymorphism

Examples: the Button classes

Variables in java

Boolean

Boolean example

Boolean comments

Characters (char)

Char Examples

Char Notes

byte

int

long

Examples of integer types

Notes on integer types

More on integers

Float

double

Casting primitives

postfix identifiers

Strings

Wrapper objects

Wrapper examples

Converting primitives to String

primitive to string examples

Strings to primitives

More on String to Primitive conversion

Warning about Wrapper objects!!!

Pass by reference, pass by value