0 index
1 Variables in java
2 Boolean
3 Boolean example
4 Boolean comments
5 Characters (char)
6 Char Examples
7 Char Notes
8 byte
9 int
10 long
11 Examples of integer types
12 Notes on integer types
13 More on integers
14 Float
15 double
16 Casting primitives
17 postfix identifiers
18 Strings
19 Wrapper objects
20 Wrapper examples
21 Converting primitives to String
22 primitive to string examples
23 Strings to primitives
24 More on String to Primitive conversion
25 Warning about Wrapper objects!!!
26 Pass by reference, pass by value
27 Circle 1
28 Comments on circle1
29 Circle 2
30 Comments on circle 2
31 Circle 3
32 Comments on circle 3
33 Circle 4
34 Comments on circle 4
35 Circle 5
36 Comments on circle 5
37 Circle 6
38 Comments on Circle 6
39 Input and output
40 I/O examples

outline
created using slideshow.cgi by Andy Harris















IUPUI Computer Science: java2/Variables, Classes and Instances
1. Variables in java
  • primitive types
  • Object types



































IUPUI Computer Science: java2/Variables, Classes and Instances
2. 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/Variables, Classes and Instances
3. 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/Variables, Classes and Instances
4. 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/Variables, Classes and Instances
5. 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/Variables, Classes and Instances
6. Char Examples
  • char myChar = 'C'
  • myChar = 'a'



































IUPUI Computer Science: java2/Variables, Classes and Instances
7. 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/Variables, Classes and Instances
8. 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/Variables, Classes and Instances
9. 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/Variables, Classes and Instances
10. 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/Variables, Classes and Instances
11. 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/Variables, Classes and Instances
12. 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/Variables, Classes and Instances
13. 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/Variables, Classes and Instances
14. 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/Variables, Classes and Instances
15. 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/Variables, Classes and Instances
16. 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/Variables, Classes and Instances
17. postfix identifiers
  • used for literal values
  • 2i, 3d, 7L
  • Casting will work just as well, might be more clear



































IUPUI Computer Science: java2/Variables, Classes and Instances
18. 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/Variables, Classes and Instances
19. 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/Variables, Classes and Instances
20. Wrapper examples
  • float fltPrim = 3.6f;
  • float result;
  • Float fltClass = new Float(fltPrim);
  • result = fltClass.toFloat();



































IUPUI Computer Science: java2/Variables, Classes and Instances
21. 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/Variables, Classes and Instances
22. 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/Variables, Classes and Instances
23. 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/Variables, Classes and Instances
24. 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/Variables, Classes and Instances
25. Warning about Wrapper objects!!!
  • They cannot be used in mathematical operations
  • convert to a primitive first (floatValue(), doubleValue(), etc) first



































IUPUI Computer Science: java2/Variables, Classes and Instances
26. 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



































IUPUI Computer Science: java2/Variables, Classes and Instances
27. Circle 1



































IUPUI Computer Science: java2/Variables, Classes and Instances
28. Comments on circle1
  • One instance variable
  • No constructors
  • No methods



































IUPUI Computer Science: java2/Variables, Classes and Instances
29. Circle 2



































IUPUI Computer Science: java2/Variables, Classes and Instances
30. Comments on circle 2
  • Constructor
  • Constructor parameters
  • Use of a private instance variable (localRad)
  • The demo requires a float value to be sent



































IUPUI Computer Science: java2/Variables, Classes and Instances
31. Circle 3



































IUPUI Computer Science: java2/Variables, Classes and Instances
32. Comments on circle 3
  • Overloaded constructors and polymorphism
  • Null parameters must still do something
  • Invoking other constructors



































IUPUI Computer Science: java2/Variables, Classes and Instances
33. Circle 4



































IUPUI Computer Science: java2/Variables, Classes and Instances
34. Comments on circle 4
  • Making radius private
  • Getter and Setter methods
  • String output



































IUPUI Computer Science: java2/Variables, Classes and Instances
35. Circle 5



































IUPUI Computer Science: java2/Variables, Classes and Instances
36. Comments on circle 5
  • Private methods
  • area cannot be public!!
  • it should be automatically set



































IUPUI Computer Science: java2/Variables, Classes and Instances
37. Circle 6



































IUPUI Computer Science: java2/Variables, Classes and Instances
38. Comments on Circle 6
  • static variables
  • final variables
  • static methods



































IUPUI Computer Science: java2/Variables, Classes and Instances
39. Input and output
  • Streams in java
  • Input vs output
  • Binary vs Text
  • Location vs Filter
  • Linking them together



































IUPUI Computer Science: java2/Variables, Classes and Instances
40. I/O examples
  • Pigify turns text into pig latin
  • Text EditorDemonstrates file streams, advanced event handling
  • Window closer A utility class used in the text editor
  • DB practice A demonstration of JDBC database connections



































outline

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

Circle 1

Comments on circle1

Circle 2

Comments on circle 2

Circle 3

Comments on circle 3

Circle 4

Comments on circle 4

Circle 5

Comments on circle 5

Circle 6

Comments on Circle 6

Input and output

I/O examples