Introduction to Object-Oriented Concepts in Java Overview of Java Introduction to Object-Oriented Concepts in Java
Outline The For-Each Loop Statement Blocks Local Variable Declarations Method Declarations Modifiers Creating & Initializing Objects
The For-Each Loop Finding the sum of elements in an array int[] numbers = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31}; int sum = 0; for(int i=0; i<numbers.length; i++){ sum += numbers[i]; }
The For-Each Loop For each value in numbers ... Execute loop int[] numbers = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31}; int sum = 0; for(int value:numbers){ sum += value; } For each value in numbers ... Execute loop For-Each operates on one array at a time
The For-Each Loop Self-Test Write a for-each loop to find the average of two arrays defined as follows. double[] fuji = new double[MAX]; double[] gala = new double[MAX]; Assume that the arrays have ben populated with valid data.
Statement Blocks In Python statement blocks are tabbed if n > 6:#assuming n and sum are properly defined sum += n print(n) n +=1 In Java statement blocks are enclosed in opening and closing braces. if(n>6){ //assuming n and sum are properly defined sum += n; print(n); n +=1; }
Local Variable Declaration Local variable declarations can occur anywhere in a statement block, typically in the body of a method. Local variable have local scope, i.e., they are visible only within the block. The scope of a local variable begins at its declaration and extends to its enclosing statement block. Local variable must be initialized before use. All variables must be.
Local Variable Declaration public class LocalVariable{ public void methodA(int num){ int sum = 0;//visible anywhere in the method for(int i=0; i<7; i++){//i is visible in block int val = 3;//visible only in this block sum += i*val; } System.out.println("Sum is: " + sum); System.out.println(“Error!!" val + “ is not visible”); public static void main(String[] args) { }//end of main }//end of class
Local Variable Declaration
Class Declarations [ClassModifiers] class ClassName [extends SuperClass] [implements Interface1, Interface2, …] { ClassMemberDeclarations } ClassModifiers = public/abstract/final ClassModifiers is never private except for inner classes.
Method Declarations [MethodModifiers] ReturnType MethodNameName ([ParameterList]){ Statements } Declare a method action that take an integer and a string parameter and returns the number of characters in the string added to the integer parameter.
Modifiers That can be applied to method, field/variable, and inner class declarations: <none> default to package public any class can access public members protected the class and its subclasses private the class itself static shared by all class instances. A static method accesses only static members. A static nested class does not have implicit reference to enclosing class. final a final field has a constant value which cannot be changed. A final method cannot be overridden in subclasses.
Other Modifiers For methods: abstract defers implementation to subclasses synchronized used in multithreading environment native implemented in C/C++
Other Modifiers For fields/variables: volatile may be modified by non-synchronized methods in a multi-threading environment transient is not part of the persistent state of instances
Creating & Initializing Objects Write the class declaration that correspond with the following pieces of code final int MAX = 100; double[] values = {4.4, 5.5, 6.6, 7.7, 8, 9, 12.8}; String name = “James Truly”; Account obj = new Account(MAX, values, name); double[] values = obj.getValues(MAX);//returns an array with MAX numbers. 15
Creating & Initializing Objects Write code to initialize the class defined below: public class SomeClass { double num; int val; String[] names; public SomeClass(double num, int val){ this.num = num; this.val = val; } public SomeClass(String[] names, double num, int val){ this.names = names;//shallow copy 16
Class (Static) Fields and Methods
Object Reference this
Wrapper Classes
Abstract Classes
Packages Using packages Directory structure Java Class Library
Exceptions Sources of Exceptions Hierarchy of Exceptions Throwing exceptions Catching and Handling Exceptions
The End Qu es ti ons? ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________
Arithmetic Operators General Assignment operators Increment/Decrement + addition - subtraction * Multiplication / division % remainder/modulus Assignment operators +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= Increment/Decrement ++, --
Bitwise Operators ~x x & y x | y x ^ y Complement of x bitwise and of x and y x | y bitwise inclusive or of x and y x ^ y bitwise exclusive or of x and y
Shift Operators x << k = x*2k x >> k = x/2k Shifts the bits in x k places to the left x >> k = x/2k Shifts the bits in x, k places to the right, filling in with the highest bit (the sign bit) on the left-hand side. x >>> k Shifts the bits in x k places to the right, filling in with 0 bits on the left-hand side.
Logical Operators Translating logic into Java AND && OR || XOR ^ NOT !
Relational Operators == != < > <= >=
Selection Statements if statements
Loop Statements if statements
The break & continue Statements if statements
The End Qu es ti ons? ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________
Ss
Example of a Java Class public class Account implements IAccount { private String accName; private int accNumber; private double balance; public boolean deposit(double amount){ if(amount > 0){ this.balance += amount; return true; } return false; public boolean withdraw(double amount){ if(this.balance >= amount){ this.balance -= amount; }//end of class Example of a Java Class
How Java Works See https://javarevisited.blogspot.com/2014/06/is-java- interpreted-or-compiled-programming-language.html https://www.quora.com/Is-Java-a-compiled-language-or- interpreted-What-is-the-difference-What-is-the-JIT-compiler