- If both operands are numeric,
+
means numeric addition. - If either operand is a String,
+
means concatenation. - The expression is evaluated left to right.
Whitespace consists of spaces along with the \t
(tab) and \n
(newline) characters.
String s = " 01234 ";
//remove leading & trailing spaces
var s1 = s.strip(); // "01234"
//remove leading spaces
var s2=s.stripLeading(); // "01234 "
//remove trailing spaces
var s3 = s.stripTrailing(); // " 01234"
Both String and StringBuilder implement CharSequence.
jshell> var name = "John"
name ==> "John"
jshell> var age = 30
age ==> 30
jshell> var s = "%s is %d years old".formatted(name, age)
s ==> "John is 30 years old"
String s1 = "ab";
String s2 = "a"+"b";
System.out.println(s1==s2); //true
String s3 = "a";
s3 +="b";
System.out.println(s1==s3); //false
var sb = new StringBuilder("0123456789");
sb.setLength(5);
System.out.println(sb); //01234
There ain't method such as: append(index, string)
var sb = new StringBuilder("hello");
//append has only a single argument
sb.append(" World");
jshell> int[] array = new int[]{1,2,3}
array ==> int[3] { 1, 2, 3 }
jshell> int[] array = {1,2,3} //anonymous array
array ==> int[3] { 1, 2, 3 }
java.util.Date[] dates[] = new java.util.Date[2][];
int[] array = new int[3]; //here I specify the dimenstion
int[] array2 = new int[]{1, 4}; //here I specify the values
// array creation with both dimension expression and initialization is illegal
//int[] array3 = new int[2]{1,2}; //does not compile!
int[][] a3D[], a2D;
a3D = new int[2][2][2]; //3D array
a2D = new int[3][3]; //2D array
int[][] matrix = new int[][]{{1,2}, {3}};
int[][] matrix2 = new int[2][1];
int[][] matrix3 = {{1,2}, {3}};
//int[][] matrix4 = new int[2][1]{{1,2}, {3}}; //does not compile
String[] s1 = {"Camel", "Peacock", "Llama"};
String[] s2 = {"Camel", "Llama", "Peacock"};
String[] s3 = {"Camel", "Llama", "Peacock"};
int position = Arrays.mismatch(s1, s2); //first element to differ is at position 1
System.out.println(position); //1
position = Arrays.mismatch(s2, s3); //they are identical then return -1
System.out.println(position); //-1
int[] array1 = {1, 3, 5};
int[] array2 = {1, 3, 5};
int[] array3 = {1, 5, 5};
int result = Arrays.compare(array1, array2);
System.out.println(result); //0 (the 2 arrays are identical)
//the first element which they differ is the 2nd position (3 vs 5)
//In this case, we get a negative number because 3 is smaller than 5
result = Arrays.compare(array1, array3);
System.out.println(result); //-1
- Numbers
- Uppercase
- Lowercase
var arr = new String[]{"PIG", "pig", "123"};
//Numbers sort before letters and uppercase sorts before lowercase.
Arrays.sort(arr); //"123", "PIG", "pig"
int position = Arrays.binarySearch(arr, "Pippa");
//result : -insertionPoint -1
//insertionPoint (ideally) would be 2 (after "PIG")
//-> result = -2 -1 = -3
System.out.println(position); //-3
double floor = Math.floor(3.8); // 3.0
double ceil = Math.ceil(3.8); // 4.0
return type:
round(double)
islong
round(float)
isint
long round = Math.round(5.2D); //5
int round2 round = Math.round(5.2F); //5
double pow = Math.pow(4.0, 2.0); //16.0
// this method returns a primitive long
var aLongPrimitive = Long.parseLong("123");
//this method returns a Long object
var aLong = Long.valueOf("123");
sout.println(aLong.toString());
However, indent() also normalizes whitespace characters.
First, a line break is added to the end of the string if not already there.
Second, any line breaks are converted to the \n
format.
var name = "1234 \n567";
var indent1 = name.indent(1);
System.out.println("#%s#".formatted(indent1));
It prints:
# 1234
567
#
\n
- new line
String source = "Today is\\nSaturday";
System.out.println(source); //Today is\nSaturday
//Today is
//Saturday
System.out.println(source.translateEscapes());
\t
- tab
String source = "1\\t2";
// 1\t2
System.out.println(source);
// 1 2
System.out.println(source.translateEscapes());