ELEE28706D-JavaFundamentals S2
ELEE28706D-JavaFundamentals S2
Java Fundamentals
Learning outcomes:
- Java fundamentals
Programming:
Programming in java:
Things to remember:
- A java program is made of class and object
- Class (a template/recipe) is made of fields(properties) and methods
- Object (cake) is created from class
- Java is object oriented programming language
Java Fundamentals
Initializing declaration:
- int i = 5; declaring a variable with initial value
Implicit assignments:
- Increment operator: ++i is same as i = i + 1;
- decrement operator: --i is same as i = i – 1;
Compound Operator:
- i /= 2 is same as i = i/2;
- i += 1 is same as i = i + 1;
Single-statement blocks:
- For a single statement block, the curly braces may be omitted.
Java Fundamentals
Arrays:
Aliasing:
- A array name refers to the whole array: If we assign one array name
to another, then both refer to the same array.
- Example:
int[ ] myArray = new int[N];
…
int[ ] yourArray = myArray;
yourArray [ i ] = 789; //myArray [ i ] is now 789
Two-dimensional array:
- A two dimensional array is an array of one dimensional array
- A M-by-N two dimensional arrays that are arrays of M rows, each an
array of length N (having N columns)
- The notation to refer a two-dimensional array is myArray[ i ][ j ]
Array manipulations:
Finding the maximum:
double max = myArray[0];
for(int i = 1; i < myArray.length; i++){
if(myArray[ i ] > max)
max = myArray[ i ];}
Array manipulations:
Copying:
int N = myArray.length;
double[ ] yourArray = new double[N];
for(int i = 0; i < N; i++)
yourArray[ i ] = myArray[ i ];
Reversing:
int N = myArray.length;
for(int i = 0; i < N/2; i++)
{
double temp = myArray[ i ];
myArray[ i ] = myArray[N – i -1];
myArray[N – i -1] = temp; }
Java Fundamentals
Array manipulations:
Matrix-matrix multiplication (a[ ]*b[ ] = c[ ][ ]):
int N = a.length;
double[ ][ ] c = new double[N];
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
{//compute dot product of row i and column j
for(int k = 0; k < N; k++)
c[ i ][ j ] += a[ i ][ k ] * b[ k ] [ j ];
}
Java Fundamentals
Array manipulations:
The Enhanced for Loop:
Often, you need to visit all elements of an array. The enhanced for loop
makes this process easy
double[ ] myArray = ….
double sum = 0.0;
for (double x : myArray)// x represent each element in the array
{
sum += x;
}
Java Fundamentals
Strings:
- A string is a sequence of characters
- A literal string is a sequence of characters within double quotes,
such as, “Hello Students”
- It is non-primitive type and considered as an object
- Being an object, we can use inbuilt fields and methods( ) associated
with this object.
Concatenation:
- Java has a built-in concatenation operator(+) for String
- -Concatenating two String values is a String value, the first string
followed by the second
“Hello” + “Students” = “HelloStudents” How to “Hello Students”
Conversion:
- Two primary uses of strings:
- Convert values that we enter on a keyboard into data-type values
- Convert data-type values to values that we can read on a display
Methods:
- A method (function in C!) is a sequence of instructions with a name
- Every java program has a method called main( ), the entry point
- Methods are called to execute its instructions
Example:
public static void main(String[ ] args)
{
double myResult = Math.pow(3,4);
……
}
- main calls the Math.pow( )
- The Math.pow method returns its results (34 = 81) back to main
- The main method resumes execution
Java Fundamentals
Methods:
- Static methods are distinct from instance methods
- Static methods are property of class. As such they can be called by
their name or through their class name
- Instance methods must be called through their object
- A static method can call another static method
- A static method can’t access field variables
- Instance methods can access field variables
- Arguments are passed by values
- Methods name can be overloaded
- A method has a single return value but may have multiple return
statements
- A method can have side effects
Java Fundamentals
1. JavaCocepts@CayHorstmann