JAVA GEEKS BLOG

CORE JAVA




Important Links:-


Important Keyboard Shortcuts:-
----------------------------------------
1) Ctrl+Shift+F --> To Format the Java code
2) Ctrl+1 --> Assign object of class to a new variable
3) Alt+UpArrow --> To move the current line or selection up
4) Alt+DownArrow --> To move the current line or selection down
5) Ctrl+Shift+O --> To remove the unwanted imports

Arrays:-

---------------------------------------------------

Access the location of an index in an array:-

-------------------------------------------------------

arr[i] = Address of starting index of array+index of the array to be accessed*(size of the datatype of the array)
e.g.- a[3] = 2000+3*(4)=2012

Features of Array:-

-------------------------

1) Accessing the elements is very easy and fast using an array but inserting or deleting an element is slow due to shift operations in an array.
2) Arrays are static means once the memory is allocated at runtime it can't be changed or increased.
3) Arrays are homogeneous in nature.
4) Array is itself an object in Java. The elements in an array are all Objects.
5) Once we define an array as a particular datatype we cannot assign any other type values to it.
6) We can iterate or loop over the arrays using for loop and for each loop.
7) If we don't assign any values to arrays then default values of that particular datatype will be assigned to arrays.
8) We cannot declare an element in arrays beyond its initial declaration. If we try to declare it then an ArrayIndexOutOfBoundsException is thrown by the arrays.
9) The maximum size of an array is the maximum amount of value or size a datatype can hold.
10) An array variable points to the first element's memory location.

For Each/Enhanced For Loop:-

-------------------------------------------

1) It makes our job a lot more easier while working with arrays and collections.
2) Using it we can access the data structures like arrays and Collections without specifying any boundaries.
3) We can only iterate or loop in the forward direction using for each loop.
4) We can access only one element at a time, i.e., we cannot jump at any other index.

Syntax:-

------------------

for (datatype variable_name: data_structure_name) {
// statements
}

e.g.- for (int element : arr) {
System.out.println(element);
       }

Java 8 New Features:-

---------------------------------------------------------

1) Lambda Expression:-

----------------------------------

1) The main benefit of Lambda Expression is to introduce the benefits of Functional Programming to Java.
2) A Lambda is an Anonymous or Closure function in Java, i.e., a function that does not have a name, return type, and access modifier in Java.
    e.g.- Functional Programming Anonymous Function or Closure
           
           public void display() { i. ()  -> SOP("Amit");
SOP("Amit"); ii. (int a, int b) -> SOP(a*b);
           }
3) If we have multiple statements then we can use flower brackets in Lambda Expression.

Advantages or Benefits of Lambda:-

-----------------------------------------------

1) Less Code
2) Easy to implement anonymous inner classes
3) We can Lambda Expression as parameters to other methods which is very powerful as well.
4) To use Lambdas in Java 8 introduces Functional Interfaces in Java 8.

2) Functional Interfaces:-

-----------------------------------------------------

1) If an interface has only one and one abstract method then it is called as a functional interface and that abstract method is called a Functional Method.
2) We can define any number of default methods in a functional interface. But there should be only and only one abstract method then only we call it a Functional Interface.
3) Java 8 also provides us with an annotation like @FunctionalInterface.If we mark interfaces with this annotation then we can only find one abstract method in the Functional Interface.
4) It is required to implement Lambda Expressions.
5) If we try to implement more than one abstract method once we mark our interface with @FunctionalInterface annotation then the compiler will raise a compile-time error.

Syntax:-

---------------------

interface MyInterface {
         void myMethod();
}

Examples of inbuilt functional interfaces or runtime examples are:-
1) Runnable interface  --->      run() --> abstract method
2) Comparator    ----->         compareTo()  --> only abstract method

3) Default Methods:-
-----------------------------------------

--> If a class implements multiple interfaces which have the same exact default methods then you need to provide an overridden implementation of the method inside the class.

4) Predicate:-

--------------------------------------

1) A Predicate is a function with a single argument and returns a boolean value.
2) To implement a Predicate we use the Predicate interface that was introduced in Java 1.8.
3) It is a functional interface that has only one abstract method that can take any type of argument but it always should return a boolean true or false.
4) Since it is a Functional Interface we can express it as a Lambda Expression.
5) Predicate is present in java.util.function package.
6) We can pass Predicate to a method as an argument with any number of Predicates and pass the parameter to it.
7) We can invoke the method.

Syntax:-
---------------------

interface Predicate<T> {
        public boolean test(T t);
}

Predicate Joining:-
-------------------------------------------

We can use more than one Predicate together by joining them using the methods and(), or() and negate() that are available on the Predicate interface. 
These methods are very similar to and, or and not operators.

5) Functions:-

-----------------------------------------------------

1) Functions are similar to Predicates except that they can return any type of result.
2) It was introduced in Java 1.8 version.
3) Function is present in java.util.function package.
4) It has only one method called R apply(T t) and it can take any type of parameters and return any type.
5) The Function interface is a Functional Interface because it has only one abstract method and it can be used to express Lamdas or Lambdas Expressions.

Syntax:-

--------------------

interface Function(T, R) {
       R apply(T t);
}

Method Referencing using :: Operator:-

---------------------------------------------------------------------------

1) Java 8 also introduces a :: operator using which we can map methods and constructors to a Functional Interface method.
2) We can map the MyClass method to the Functional Interface method having the same argument using the :: operator.

e.g.-      MyInterface MyClass
string sayHello(String name) string myMethod(String name)   --->  instance method
{
}
MyClass m = new MyClass();  ---> object of the class
MyInterface i = m::myMethod;  ---> using the object and the :: operator we are mapping this implementation to the Functional interface
String s = i.sayHello();  ---> invoke interface method and automatically the implementation inside myMethod() will be used.

* Here, s is the method reference.
* For static method, we use it as follows:-
    MyInterface i = MyClass::myMethod;
* The only rule we need to follow is that the method that we are implementing should take the exact same argument as the method in the Functional Interface. 
   The name of the method, return type, and access modifiers may need not be the same. Only the method argument type should be the same.

Syntax for Mapping the Constructor:-

--------------------------------------------------------------------------------


MyInterface f2 = MyClass :: new;
f2.get("Using Constructor Mapping");

6) Streams:-

---------------

1) Using Streams, we can process the data in a declarative manner.
2) It makes it very easy for us to process the data or objects inside the Collections.
3) These Streams are not the same as java.io streams which are meant for reading and writing files.
4) Stream is an interface inside the java.util.stream.Stream package.
5) We can get the Stream on a Collection by invoking a stream() method which was added to the Collection interface in Java 1.8.
6) Once we have Stream to process the Collection, we will do it in 2 steps or 2 phases-

i) Configuration:-

   -------------------------------

The first phase is configuring the Stream. We can do the configuration in 2 ways:-

a) Filtering:-  
    ---------------------
Wherein we invoke the filter method pass it a Predicate which will evaluate into a boolean expression and the objects in the Collection will be filtered out based on the Predicate and it will return the Stream which will have the filtered out objects.

Syntax:-

------------

public Stream filter(Predicate<T> p)

b) Map:-
    ----------

Syntax:-
---------------

public Stream map(Function f)

    The second way of configuring is using a map() method. The map() method on the Stream again returns a Stream but this time it doesn't take a boolean expression but it takes a function instead. It will create a new Collection or new object for every object in the Collections. If we have to create a new set of objects or a new set of Collection based on the input, then we will use a map. It will map each input to a corresponding output object.

ii) Processing:-

    ------------------------

The second phase is to process the data itself. Stream API provides us various methods such as- collect(), count(), sorted(), min(), max(), etc. to process the data.


0 Comments