Monday, April 1, 2019

Manipulations of int and String in Java

At first, declare a variable as int:
int n = {your number};
To remove a right most digit:
n = n / 10;
To check if right most digit is 8:
n % 10 == 8;
If we declare a String variable:
String str = "Hello, test here.";
The last character of the String can be gotten like this:
String lastChar = str.substring(str.length() - 1);
To remove the last character of the String:
String str = str.substring(0, str.length() - 1);
Get number of characters of the String:
int numOfChars = str.length();

Weird behaviors

This returns 1:
public int returnOne() {
  return 1;
}
But this returns 0. This looks like it would return 1 though..
public int returnOne() {
  int count = 0;
  return count++;
}
But this returns 1.
public int returnOne() {
  int count = 0;
  return ++count;
}