JavaRush /Java Blog /Random EN /Coffee break #116. How to use function pointers in Java. ...

Coffee break #116. How to use function pointers in Java. indexOf in Java - How to find the index of a string in Java

Published in the Random EN group

How to use function pointers in Java

Source: Dev.to Pointers are objects that store a memory address and can save memory by directly pointing to a target object, array, or variable address instead of passing by value. Unfortunately, there is no “real” concept of pointers in Java. But luckily for us, there is a workaround using method references that is close to the real thing. Coffee break #116.  How to use function pointers in Java.  indexOf in Java - How to find the index of a string in Java - 1

Function pointers

A function pointer is a pointer that points to the address of a function. Some use cases include creating a callback routine by creating a function that calls another function based on its execution, or storing an array of function pointers for dynamically called methods (for example, storing processor instructions for an emulator).

Function pointer simulation

There are four types of method references . We use a type that refers to an instance method of a specific object. Let's start by defining an interface that defines the signatures of the methods you point to.
// Wrapping interface
private interface FunctionPointer {
  // Method signatures of pointed method
  void methodSignature(int a);
}
Then we will create methods with the signature of the target method.
public void method1(int b) {
  System.out.println("Called method1 with integer " + b);
}

public void method2(int v) {
  System.out.println("Called method2 with integer " + v);
}

public void method3(int a) {
  System.out.println("Called method3 with integer " + a);
}
The next step is to create wrapper interface variables and assign methods to them. Variables will act as a pointer to the function to be stored or executed.
// Create a variable of the interface and assign
// the method references
FunctionPointer pointer1 = this::method1;
FunctionPointer pointer2 = this::method2;

// Call both methods using their "pointer"
pointer1.methodSignature(3);
pointer2.methodSignature(2);

// Reassign and call pointer 1
pointer1 = this::method3;

pointer1.methodSignature(5);
Called method1 with integer 3 Called method2 with integer 2 Called method3 with integer 5

Using Lambda Expressions

Method references can be assigned using lambda expressions.
// Create a method reference and assign a methods using a lambda.
FunctionPointer pointer1 =
  (a) -> System.out.println("Called pointer1 with int " + a);

FunctionPointer pointer2 =
  (b) -> System.out.println("Called pointer2 with int " + b);

Array of function pointers

The functionality of an array of method references can be emulated by creating a wrapper interface array.
// Create an array of "pointers"
FunctionPointer[] functionPointersArray = new FunctionPointer[3];

// Assign methods
functionPointersArray[0] = this::method1;
functionPointersArray[1] = this::method2;
functionPointersArray[2] = this::method3;

// Call methods
functionPointersArray[0].methodSignature(3);
functionPointersArray[1].methodSignature(4);
functionPointersArray[2].methodSignature(5);

Function pointer vs direct call

If you compare both options, calling a method directly is almost 5 times faster than using a method reference. This is all because of the extra step of calling a lambda expression compared to a stored method. But you probably won't notice any performance loss in a year, so don't worry about it.

Conclusion

Pointers are variables that point directly to the address of an object rather than a value. The function pointer directly points to the function address, which can reduce memory consumption. Java does not have pointers, but can emulate behavior using method references or lambda expressions. Using a method reference is slower than calling a method directly, but this does not prevent its use.

indexOf in Java - How to find the index of a string in Java

Source: FreeCodeCamp A string is a set of characters enclosed in double quotes. The indexOf method returns the index position of the specified character or substring in a string. In this article, we will learn about the syntax of various indexOf methods . We will also look at examples that will help you understand and effectively use searching for the index of a character or substring in Java code. Coffee break #116.  How to use function pointers in Java.  indexOf in Java - How to find the index of a string in Java - 2

Syntax of the indexOf method

The indexOf method has the following methods:
public int indexOf(int char)
public int indexOf(int char, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
Let's explain these parameters:
  • char represents one character per line.
  • fromIndex specifies the position at which the search for the index of a character or substring should begin. This is important when you have two characters/strings that have the same value in a string. With this parameter, you can tell the indexOf method where to start.
  • str represents a substring in a string.
Don't worry if you don't understand how it all works yet—the examples will make everything clear!

How to use indexOf method in Java

In the first example we will find the index of one character in a string. This will help us understand the public int indexOf(int char) method .

IndexOf(int Char) method example

public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    System.out.println(greetings.indexOf("o"));

    // 4
  }
}
In the code above, we got the index of the character “o” returned to us, which is 4. We have two characters “o”, but only the index of the first one was returned. In the following example we will see how we can return the index of the second "o". If you're wondering how sequence numbers are obtained, you'll notice that the first character in a string has an index of zero, the second character has an index of one, and so on.

Example of the indexOf(int Char, Int fromIndex) method

Here is an example explaining the int indexOf(int char, int fromIndex) method :
public class Main {
  public static void main(String[] args) {
    String greetings = "Hello World";

    System.out.println(greetings.indexOf("o", 5));

    // 7
  }
}
In the example above, we tell the indexOf method to start working at the fifth index. H => index 0 e => index 1 l => index 2 l => index 3 0 => index 4 Note that index 5 is not a “W” character. The fifth index is the space between “Hello” and “World”. Thus, in this code, all other characters preceding the fifth index will be ignored. 7 is returned as the index of the second character “o”.

Example of the Int indexOf(String Str) method

In the following example, we will understand how the public int indexOf(String str) method works , which returns the index of a substring.
public class Main {
  public static void main(String[] args) {
    String motivation = "Coding can be difficult but don't give up";

    System.out.println(motivation.indexOf("be"));

    // 11
  }
}
I wonder how we got 11 back? You should review the last section to understand how indexes are counted and how spaces between substrings are also considered indices. Note that when a substring is passed as a parameter, the index returned is the index of the first character in the substring. 11 is the index of the character “b”.

Example of the indexOf(String Str, Int fromIndex) method

The last method is public int indexOf(String str, int fromIndex) - the same as the public int indexOf(int char, int fromIndex) method . It returns the index from the specified position.
public class Main {
  public static void main(String[] args) {
    String motivation = "The for loop is used for the following";

    System.out.println(motivation.indexOf("for", 5));

    // 21
  }
}
In this example, we specified that the method should begin its work at the fifth index, which comes after the first substring for. 21 is the index of the second substring of for. Finally, when we pass a character or substring that is not in the string, the indexOf method will return -1. Here's an example:
public class Main {
  public static void main(String[] args) {
    String motivation = "The for loop is used for the following";

    System.out.println(motivation.indexOf("code"));

    // -1
  }
}

Conclusion

In this article, we learned how to use the four indexOf methods , with clear examples. We also saw what the syntax for each of these methods looks like and how they tell the index to return. In addition, we were able to find out what happens when a non-existent character or substring is passed as a parameter.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION