DevEX - reference for building teams, processes, and platforms
Java Course 2: Basics
1. Java course - IAG0040
Java Basics,
Program Flow
Anton Keks 2011
2. Java type system
●
static type checking ●
provides (benefits)
– by compiler, type – safety
declarations required ●
many bugs are type
●
strongly typed errors
– runtime checking – documentation
– automatic conversions – abstraction
allowed only when not ● high-level thinking
loosing information
– optimization
● memory-safe (performance)
– array bounds checking,
no pointer arithmetic
Java course – IAG0040 Lecture 2
Anton Keks Slide 2
3. Objects and Classes
●
Classes introduce new types into a program
●
First appeared in Simula-67 to define 'classes of
objects'
●
In OOP, Objects are instances of Classes
●
All objects of the same class share
– same properties:
● fields hold internal state
– same behavior, dependent on the state:
●
methods used for communication with objects
Java course – IAG0040 Lecture 2
Anton Keks Slide 3
4. References
● You manipulate objects with references
– Like remote control of a TV is a reference to the TV
●
Objects are stored on the heap, independently
– String s; // is a reference to nothing
– s = “hello”; // now s points to a newly created
// String object on the heap
●
You must create all the objects before using them
– s = new Date();
● There are many other built-in types (classes), like
Integer, Date, Currency, BigDecimal, etc, and making
your own types is the point of OOP
Java course – IAG0040 Lecture 2
Anton Keks Slide 4
5. Variables and Fields
●
Local variables
– inside of methods
– final variables can't change their values
●
Class members (fields)
– inside of classes, outside of methods
●
Global variables
– don't exist in Java
– can be simulated with static class members
●
Constants are static final fields
Java course – IAG0040 Lecture 2
Anton Keks Slide 5
6. Primitive types
●
Only primitives are not objects in Java
– But, there are wrapper classes. Unlike in C, formats and
sizes are standardized and are always the same.
Primitive Size Min Max Wrapper Default
boolean 1 bit? false true Boolean false
char 16 bit Unicode 0 216-1 (UTF-16) Character 'u0000'
byte 8 bit -128 127 Byte (byte)0
short 16 bit -215 215-1 Short (short)0
int 32 bit -231 231-1 Integer 0
long 64 bit -263 263-1 Long 0L
float 32 bit IEEE 754 IEEE 754 Float 0.0f
double 64 bit IEEE 754 IEEE 754 Double 0.0d
void - - - Void -
Java course – IAG0040 Lecture 2
Anton Keks Slide 6
7. High-precision numbers
●
BigInteger and BigDecimal
– these are classes, not primitives
– immutable
– cannot be used with operators, operations are
methods
– slower, but long and precise
● BigInteger – arbitrary long integers
● BigDecimal – arbitrary precision fixed point
numbers
Java course – IAG0040 Lecture 2
Anton Keks Slide 7
8. Fields (Members)
●
A field (or member) is a class variable, holds its state
●
Field names are in “lowerCamelCase”
●
Constant names are in UPPER_CASE
●
Initialized either automatically or manually
● class MyClass {
boolean isEmpty; // a field
int count; // a second field
static String foo = “bar”; // a static field
static final double E = Math.E; // a constant
static {
foo = “fooBar”; // static block
}
}
Java course – IAG0040 Lecture 2
Anton Keks Slide 8
9. Methods
●
A method is a class function, used to execute
an operation on a class (aka send a message)
● class MyClass {
int count; // a field
void doStuff(int count, short b) { // a method
this.count = count * b; // method body
}
int getCount() { // second method,
return count; // returning a value
}
}
● Methods can be overloaded
● Parameters are passed as references
● Local variables are enforced to be manually initialized
Java course – IAG0040 Lecture 2
Anton Keks Slide 9
10. Accessing fields and methods
●
Java uses a dot '.' as an accessor operator
– MyClass myObject = new MyClass();
myObject.isEmpty = true; // field access
– myObject.setCount(5); // method call
– int j = new MyClass().getCount(); // also allowed
● Static fields and methods are accessed
through the class itself, not instance variables
– int i = Integer.MAX_VALUE;
●
“Deep” access is possible
– System.out.println(“Hello!”);
Java course – IAG0040 Lecture 2
Anton Keks Slide 10
11. Access Modifiers and Visibility
●
Several access modifiers are in use (keywords):
– private – accessible only in the same class
– default, no keyword (package local) – accessible only
in the same package
– protected – accessible in the same package and
subclasses
– public – accessible from anywhere
● public class HelloWorld { // public class
int a; // package local field
private int b; // private field
protected void doStuff() {} // protected method
private HelloWorld() {} // private constructor
}
Java course – IAG0040 Lecture 2
Anton Keks Slide 11
12. Encapsulation
●
Use the most restrictive access modifiers as
possible
● Classes 'hide' implementations and internal
details from their users
– This allows changing of internals any times
without breaking any usages
– The less internals exposed, the easier is to
change them
●
Public/accessible interface must be obvious to
use; don't do anything unexpected!
Java course – IAG0040 Lecture 2
Anton Keks Slide 12