In-house Web system development with Terasoluna Server for Java (Java7, Java8): 4 years --NTT open source http://www.terasoluna.jp/product/framework/server_framework_5.html
In-house Web system development with Spring Boot: 6 months
Change job in April. Studying Scala
LT at Nagoya Java User Group April 2018!
Main.java
public class Main {
public static void main(String[] args) {
String.valueOf(hoge());
}
static <E> E hoge() {
return (E) "hoge";
}
}
Console
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to [C
at jp.co.sample.Main.main(Main.java:13)
[C
is a char array
https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.8
String.valueOf
has no meaningDon't worry about this announcement.
The compiler is ECJ (Eclipse Java Compiler), which is different from the JDK, but the result is the same.
char []
char []
comes from String.valueOf
.
String.valueOf
is overloadedCan accept char array type or Object type arguments
valueOf(char[] data)
valueOf(Object obj)
MainGenericsMethod.java
public class MainGenericsMethod {
public static void main(String[] args) {
String.valueOf(fuga("fuga"));
}
/**The type parameter is determined from the argument*/
static <E> E fuga(E arg) {
return (E) arg;
}
}
The result of executing in Java 8 by changing the combination of overloaded methods.
char []
: NGchar
: OKThe problem is an overloaded method that accepts an Object or array type.
MainOverload.java
public class MainOverload {
public static void main(String[] args) {
overload(hoge());
}
static <E> E hoge() {
return (E) "hoge";
}
static void overload(Object arg) {}
static void overload(char[] arg) {} //Change the argument type
}
When I call an overloaded method that receives an ʻint [] or
char [] `, I get a compile error that says" Method overload (Object) is of type MainOverload is ambiguous "
//Compile error
static void overload(int[] arg) {}
static void overload(char[] arg) {}
So, whereas before Java 8 the method argument site did not receive any inference, defaulting to Object, in Java 8 the most specific applicable type is inferred, in this case String.
javap -verbose
Classfile /C:/Users/yuji3/Desktop/java-test/java8/Main.class
Last modified 2018/04/14; size 476 bytes
MD5 checksum 409af5d1b1986da6e0c4cd431dc50b59
Compiled from "Main.java"
public class Main
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #7.#20 // java/lang/Object."<init>":()V
#2 = Methodref #6.#21 // Main.hoge:()Ljava/lang/Object;
#3 = Class #22 // "[C"
#4 = Methodref #23.#24 // java/lang/String.valueOf:([C)Ljava/lang/String;
#5 = String #14 // hoge
#6 = Class #25 // Main
#7 = Class #26 // java/lang/Object
#8 = Utf8 <init>
#9 = Utf8 ()V
#10 = Utf8 Code
#11 = Utf8 LineNumberTable
#12 = Utf8 main
#13 = Utf8 ([Ljava/lang/String;)V
#14 = Utf8 hoge
#15 = Utf8 ()Ljava/lang/Object;
#16 = Utf8 Signature
#17 = Utf8 <E:Ljava/lang/Object;>()TE;
#18 = Utf8 SourceFile
#19 = Utf8 Main.java
#20 = NameAndType #8:#9 // "<init>":()V
#21 = NameAndType #14:#15 // hoge:()Ljava/lang/Object;
#22 = Utf8 [C
#23 = Class #27 // java/lang/String
#24 = NameAndType #28:#29 // valueOf:([C)Ljava/lang/String;
#25 = Utf8 Main
#26 = Utf8 java/lang/Object
#27 = Utf8 java/lang/String
#28 = Utf8 valueOf
#29 = Utf8 ([C)Ljava/lang/String;
{
public Main();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 2: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: invokestatic #2 // Method hoge:()Ljava/lang/Object;
3: checkcast #3 // class "[C"
6: invokestatic #4 // Method java/lang/String.valueOf:([C)Ljava/lang/String;
9: pop
10: return
LineNumberTable:
line 4: 0
line 5: 10
static <E extends java.lang.Object> E hoge();
descriptor: ()Ljava/lang/Object;
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: ldc #5 // String hoge
2: areturn
LineNumberTable:
line 8: 0
Signature: #17 // <E:Ljava/lang/Object;>()TE;
}
SourceFile: "Main.java"
javap -verbose
Classfile /C:/Users/yuji3/Desktop/java-test/java7/Main.class
Last modified 2018/04/14; size 481 bytes
MD5 checksum 72c2eee8d5b013579f81dc56f53458d0
Compiled from "Main.java"
public class Main
SourceFile: "Main.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#19 // java/lang/Object."<init>":()V
#2 = Methodref #5.#20 // Main.hoge:()Ljava/lang/Object;
#3 = Methodref #21.#22 // java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
#4 = String #13 // hoge
#5 = Class #23 // Main
#6 = Class #24 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 main
#12 = Utf8 ([Ljava/lang/String;)V
#13 = Utf8 hoge
#14 = Utf8 ()Ljava/lang/Object;
#15 = Utf8 Signature
#16 = Utf8 <E:Ljava/lang/Object;>()TE;
#17 = Utf8 SourceFile
#18 = Utf8 Main.java
#19 = NameAndType #7:#8 // "<init>":()V
#20 = NameAndType #13:#14 // hoge:()Ljava/lang/Object;
#21 = Class #25 // java/lang/String
#22 = NameAndType #26:#27 // valueOf:(Ljava/lang/Object;)Ljava/lang/String;
#23 = Utf8 Main
#24 = Utf8 java/lang/Object
#25 = Utf8 java/lang/String
#26 = Utf8 valueOf
#27 = Utf8 (Ljava/lang/Object;)Ljava/lang/String;
{
public Main();
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 2: 0
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=1, args_size=1
0: invokestatic #2 // Method hoge:()Ljava/lang/Object;
3: invokestatic #3 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
6: pop
7: return
LineNumberTable:
line 4: 0
line 5: 7
static <E extends java/lang/Object> E hoge();
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: ldc #4 // String hoge
2: areturn
LineNumberTable:
line 8: 0
Signature: #16 // <E:Ljava/lang/Object;>()TE;
}
Recommended Posts