Skip to content

Commit

Permalink
Methods and Encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilkin Mehdiyev committed Feb 1, 2021
1 parent 45c34e4 commit 1097c2e
Showing 1 changed file with 70 additions and 34 deletions.
104 changes: 70 additions & 34 deletions 1z0-815/1z0-815.MD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [Making Decisions](#Making-Decisions)
- [Core Java APIs](#Core-Java-APIs)
- [Lambdas and Functional Interfaces](#Lambdas-and-Functional-Interfaces)

- [Mehtods and Encapsulation](#Methods-and-Encapsulation)

## Java Building Blocks

Expand Down Expand Up @@ -94,24 +94,28 @@
System.out.print(y + " ");
}
```

### The Structure of an Enhanced For-Each Loop

![](src/images/the-structure-of-a-enhanced-foreach-loop.PNG)

- The right side of the for-each loop must be one of the following:

- A built-in Java array
- An object whose type implements java.lang.Iterable

- This does not include all of the Collections Framework classes or interfaces, but only those that implement or extend that ***Collection*** interface. For example, ***Map*** is not supported in a for-each loop, although ***Map*** does include methods that return Collection instances.
- This does not include all of the Collections Framework classes or interfaces, but only those that implement or extend that **_Collection_** interface. For example, **_Map_** is not supported in a for-each loop, although **_Map_** does include methods that return Collection instances.

- The left side of the for-each loop must include a declaration for an instance of a variable whose type is compatible with the type of the array or collection on the right side of the statement.

## Core Java APIs

- A ***string*** is basically a sequence of characters.
- Placing one String before the other String and combining them is called string ***concatenation***.
- **Immutability** - Once a ***String*** object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change one of the characters inside it.
- A **_string_** is basically a sequence of characters.
- Placing one String before the other String and combining them is called string **_concatenation_**.
- **Immutability** - Once a **_String_** object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change one of the characters inside it.
- **Immutable** classes in Java are final, which prevents subclasses creation.
- A **string** is a sequence of characters and Java counts from 0 when indexed.

- ### Indexing for a string
![](src/images/indexing-for-a-string.png)

Expand All @@ -128,62 +132,94 @@
```
- In the **substring()** method **endIndex** parameter is allowed to be 1 past the end of the sequence if you want to stop at the end of the sequence.
- ### Indexses for a substirng
![](src/images/indexses-for-a-substring.png)
![](src/images/indexses-for-a-substring.png)
- **substring()** methods parametehrs **String substring(int beginIndex, int endIndex)**. beginIndex is inclusive. However endIndex is exclusive:
```java
String string = "animals";
System.out.println(string.substring(3, 7)); // mals
```
- The ***equals()*** method checks whether two String objects contain exactly the same characters in the same order. The ***equalsIgnoreCase()*** method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed:
- The **_equals()_** method checks whether two String objects contain exactly the same characters in the same order. The **_equalsIgnoreCase()_** method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed:
```java
System.out.println("abc".equals("ABC")); // false
System.out.println("ABC".equals("ABC")); // true
System.out.println("abc".equalsIgnoreCase("ABC")); // true
```
- The ***strip()*** method is new in Java 11. It does everything that ***trim()*** does, but it supports Unicode.
- The ***intern()*** method returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.
- Unlike the ***String*** class, ***StringBuilder*** is not immutable.
- **StringBuffer** works the same way as ***StringBuilder*** except it supports threads(threadsafe).
- ***StringBuffer*** is no longer on either exam. It performs slower than **StringBuilder**.
- The ***delete()*** method is more flexible than some others when it comes to array indexes. If you specify a second parameter that is past the end of the StringBuilder, Java will just assume you meant the end. That means this code is legal:
- The **_strip()_** method is new in Java 11. It does everything that **_trim()_** does, but it supports Unicode.
- The **_intern()_** method returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.
- Unlike the **_String_** class, **_StringBuilder_** is not immutable.
- **StringBuffer** works the same way as **_StringBuilder_** except it supports threads(threadsafe).
- **_StringBuffer_** is no longer on either exam. It performs slower than **StringBuilder**.
- The **_delete()_** method is more flexible than some others when it comes to array indexes. If you specify a second parameter that is past the end of the StringBuilder, Java will just assume you meant the end. That means this code is legal:
```java
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1, 100); // sb = a
```
- ***equals*** method check the values inside the String rather than the string reference itself. If a class doesn’t have an equals method, Java determines whether the references point to the same object—which is exactly what **==** does.
- **_equals_** method check the values inside the String rather than the string reference itself. If a class doesn’t have an equals method, Java determines whether the references point to the same object—which is exactly what **==** does.
- **==** is checking for object reference equality.
- The **string pool**, also known as the **intern pool**, is a location in the Java virtual machine (JVM) that collects all these strings.
- The ***JVM*** created only **one** literal in memory.
- The **_JVM_** created only **one** literal in memory.
- Never use intern() or == to compare String objects in your code.
- This approach is called an anonymous array. It is anonymous because you don’t specify the type and size:
```java
int[] numbers2 = {42, 55, 99};
```
- Just like a **StringBuilder**, an **ArrayList** can change capacity at runtime as needed. Like an ***array***, an **ArrayList** is an ordered sequence that allows duplicates.
- ***ArrayList*** implements an interface called **List**. In other words, an ***ArrayList is a List***.
- The **valueOf()** allows object caching. The wrapper classes are immutable and take advantage of some caching as well. The **valueOf()** returns Wrapper class (Integer, Double, etc.). **Parse** methods (***parseInt()*** etc.) return a primitive,
and the ***valueOf()*** method returns a wrapper class.
- **Arrays.asList(array)** point to the same data store ***array***. Change in **Arrays.asList(array)** will affect ***array*** directly and vice verse.
- ***Array.asList()*** cannot change size.
- ***List.of(array)*** create immutable list. Have not any realtion with original ***array***.
- Just like a **StringBuilder**, an **ArrayList** can change capacity at runtime as needed. Like an **_array_**, an **ArrayList** is an ordered sequence that allows duplicates.
- **_ArrayList_** implements an interface called **List**. In other words, an **_ArrayList is a List_**.
- The **valueOf()** allows object caching. The wrapper classes are immutable and take advantage of some caching as well. The **valueOf()** returns Wrapper class (Integer, Double, etc.). **Parse** methods (**_parseInt()_** etc.) return a primitive,
and the **_valueOf()_** method returns a wrapper class.
- **Arrays.asList(array)** point to the same data store **_array_**. Change in **Arrays.asList(array)** will affect **_array_** directly and vice verse.
- **_Array.asList()_** cannot change size.
- **_List.of(array)_** create immutable list. Have not any realtion with original **_array_**.
- ### Array and List conversions
![](src/images/array-and-list-conversions.png)
- Calling == on ***String*** objects will check whether they point to the same object in the pool. Calling == on ***StringBuilder*** references will check whether they are pointing to the same StringBuilder object. Calling equals() on ***String*** objects will check whether the
sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.
- Understand the difference between == and equals(). == checks object equality.
equals() depends on the implementation of the object it is being called on. For Strings, equals() checks the characters inside of it.

## Lambdas and Functional Interfaces
- ***Deferred execution*** means that code is specified now but will run later.
- **Lambdas** work with interfaces that have only one abstract method. These are called ***functional interfaces***.
![](src/images/array-and-list-conversions.png)
- Calling == on **_String_** objects will check whether they point to the same object in the pool. Calling == on **_StringBuilder_** references will check whether they are pointing to the same StringBuilder object. Calling equals() on **_String_** objects will check whether the
sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.
- Understand the difference between == and equals(). == checks object equality.
equals() depends on the implementation of the object it is being called on. For Strings, equals() checks the characters inside of it.

## Lambdas and Functional Interfaces

- **_Deferred execution_** means that code is specified now but will run later.
- **Lambdas** work with interfaces that have only one abstract method. These are called **_functional interfaces_**.
- These two lines do the exact same thing:
```java
a -> a.canHop()
(Animal a) -> { return a.canHop(); }
```
- The parentheses can be omitted only if there is a single parameter and its type is not explicitly stated.
- The parentheses can be omitted only if there is a single parameter and its type is not explicitly stated.
- **Java** doesn’t require you to type return or use a semicolon when no braces are used.
- ***Lambda*** can access an instance variable, method parameter, or local variable under certain conditions. Instance variables (and class variables) are always allowed.
- Method parameters and local variables are allowed to be referenced if they are ***effectively final***.
- **_Lambda_** can access an instance variable, method parameter, or local variable under certain conditions. Instance variables (and class variables) are always allowed.
- Method parameters and local variables are allowed to be referenced if they are **_effectively final_**.
- A **functional interface** is one with a single abstract method.

## Mehtods and Encapsulation

- Order of modifiers, descenidng:
- public < protected < package (or default) < private
- Java will create an empty array if no parameters are passed for a **_vararg_**.
- **Private** - The **_private_** modifier means the method can be called only from within the same class.
- **Default (Package-Private) Access** - With default access, private plus other classes in the same package
- **Protected** - The **_protected_** modifier means the method can be called only from classes in the same package or subclasses.
- **Public** - The **_public_** modifier means the method can be called from any class.
- In addition to **_main()_** methods, static methods have two main purposes:
- For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate an object just to call the method.
- For state that is shared by all instances of a class. All instances must share the same state. Methods that merely use that state should be static as well.
- You can use an instance of the object to call a **_static_** method.

```java
public static int count = 0; // static variable
5: Koala k = new Koala();
6: System.out.println(k.count); // k is a Koala
7: k = null;
8: System.out.println(k.count); // k is still a Koala, VALID
```

- A static member cannot call an instance member without referencing an instance of the class.
- Static methods/variables cannot call instance methods/variables, and vice verse.
- Regular imports are for importing classes. **_Static imports_** are for importing static members of classes.
- Java uses **_pass-by-value_** to get data into a method.
- **_Method overloading_** occurs when methods have the same name but different method signatures. Everything other than the method name can vary for overloading methods.
- Java has a concept called type erasure where generics are used only at compile time.
- Encapsulation means only methods in the class with the variables can refer to the instance variables.
- Accessor method = getter, Mutator method = setter.
- Java uses pass-by-value, which means that calls to methods create a copy of the parameters. Assigning new values to those parameters in the method doesn’t affect the caller’s variables.

0 comments on commit 1097c2e

Please sign in to comment.