Java) Wrapper / Expressions / Labeled Loops
This post was migrated from Tistory. You can find the original here.
type check
Wrapper class type
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
String t1 = "TEST";
System.out.println(t1.getClass().getName());
//java.lang.String
Integer t2 = 7777;
System.out.println(t2.getClass().getName());
//java.lang.Integer
List<Integer> t3 = new ArrayList<>();
System.out.println(t3.getClass().getName());
// java.util.ArrayList
}
Primitive type
1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
int a1 = 3;
float a2 = 5;
double a3 = 7;
System.out.println(Integer.class.isInstance(a1)); //true
System.out.println(Float.class.isInstance(a2)); //true
System.out.println(Double.class.isInstance(a3)); //true
}
You can also build a primitive typeof with overloading:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Check {
public static Class<Integer> typeof(final int expr) {
return Integer.TYPE;
}
public static Class<Long> typeof(final long expr) {
return Long.TYPE;
}
public static Class<Float> typeof(final float expr) {
return Float.TYPE;
}
public static Class<Double> typeof(final double expr) {
return Double.TYPE;
}
// ...
}
Wrapper class
1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) {
Integer num1 = new Integer(7); // boxing
Integer num2 = new Integer(3); // boxing
int int1 = num1.intValue(); // unboxing
int int2 = num2.intValue(); // unboxing
Integer result1 = num1 + num2; // 10
Integer result2 = int1 - int2; // 4
int result3 = num1 * int2; // 21
}
Since JDK 1.5, autoboxing and unboxing happen automatically where they make sense.
A Wrapper is an object, and you normally can’t run arithmetic or comparisons directly between objects. The operations above only work because autoboxing/unboxing kicks in.
wrapper class — tcpschool
⬆ The other chapters look worth a read too.
Expressions
- Lambda expression
- method reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
Integer t = -3;
// lambda expression
Function<Integer, Integer> func = (a) -> Math.abs(a);
Function<Integer, Integer> func = (a) -> {
System.out.println("lambda");
return Math.abs(a);
};
// method reference
Function<Integer, Integer> func2 = Math::abs;
// you invoke a functional interface through its apply method
System.out.println(func.apply(t)); //3
System.out.println(func2.apply(t)); //3
}
ArrayList
One of the List data types
List to Array
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
List<Integer> t = new ArrayList<>();
for (int i = 0; i < 10; i++) {
t.add((int) Math.pow(i, 2));
}
//List -> primitive[]
int[] t2 = t.stream().mapToInt(Integer::intValue).toArray();
//List -> Wrapper[]
Integer[] t3 = t.toArray(new Integer[t.size()]);
System.out.println(t2.getClass().getName());
}
Labeled loops
You can put a label on a loop to break out of more than one loop at once.
It works with both while and for, and with break and continue.
1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
allLoop : for (int i = 2; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (i == 5) {
continue allLoop; // skip out of the nested loop and continue right away
}
System.out.println(i + " * " + j + " = " + (i * j));
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void main(String[] args) {
int i = 2;
allLoop :
while (true) {
for (int j = 1; j < 10; j++) {
if (i == 5) {
break allLoop; // break straight out of the nested loop
}
System.out.println(i + " * " + j + " = " + (i * j));
}
i++;
}
}
Array, String
blabla
Java seems to want even the values you pass around to be objects rather than raw primitives.
Let’s take a look at the stream methods you use on collection objects.
