Download presentation
Presentation is loading. Please wait.
Published byRonald Whitehead Modified over 9 years ago
1
Java Tutorial
2
Object-Oriented Programming Concepts Object –a representation of some item state fields/members and should be encapsulated behavior methods Class –an implementation of some object Interface –the contract between classes Inheritance –a way to share common aspects of objects Package –a way to organize a set things together.
3
Object-Oriented Programming Concepts super class (common aspects) subclass (which extends the super class to specialize) extends
4
Language Basics literals (constants) variables –instance, class, local, parameters –data types (primitive and objects) –assignments and comparisons operators (unary, binary, ternary) expressions –comparisons a = "hello world"; b = "hello world"; (a == b) TRUE a.equals(b) TRUE i1 = new Integer(1); i2 = new Integer(1); (i1 == i2) FALSE i1.equals(i2) TRUE statements blocks control flow –if/then, switch, while, do/while, for, –branching - break, continue, return
5
Operator Precedence OperatorsPrecedence postfix expr++ expr-- unary ++expr --expr +expr -expr ~ ! multiplicative * / % additive + - shift > >>> relational = instanceof equality == != bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | logical AND && logical OR || ternary ? : assignment = += -= *= /= %= &= ^= |= >= >>>=
6
public class Factorial { /* The lowest value we can compute factorial for */ public final long FACTORIAL_LOW_VALUE = 0; // constant/literal /* The highest value we can compute factorial for */ public final long FACTORIAL_HIGH_VALUE = 20; // constant/literal /* Error message for out of range values */ public final String INVALID_FACTORIAL_VALUE = "Error: factorial can only be computed for values between " + FACTORIAL_LOW_VALUE + " to " + FACTORIAL_HIGH_VALUE + "."; /** * …. **/ public int factorial(int x) { int factorial = 1; // local variable to this method /* * By definition factorial can only be computed * for non-negative values, hence negative * values are not allowed. */ if((x FACTORIAL_HIGH_VALUE)) { throw new IllegalArgumentException(INVALID_FACTORIAL_VALUE); }/* if */ for(int i = 1; i <= x; ++i) { factorial *= i; }/* for */ return factorial; }/* public int factorial(...) */ }/* public class Factorial */
7
Classes, Parameters, and Objects Classes –Constructors when they are and not provided –Override vs Overloading Methods override – replaces overloading – augments –don’t get in the habit of using: static initialization blocks unnamed initialization blocks Parameters –passed by value referenced and value type parameters Objects –declaration –instantiation –initialization
8
Access Levels ModifierClassPackageSubclassWorld public YYYY protected YYYN no modifierYYNN private YNNN
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.