SlideShare a Scribd company logo
Koichi	
  Sakata  (阪田  浩一)  
@jyukutyo
How	
  Scala	
  code	
  is	
  
expressed	
  in	
  the	
  JVM	
  
-­‐	
  ScalaMatsuri	
  2016	
  -­‐
関ジャバです!
2
@jyukutyo
produced three Java framework books
one was translated into Korean
JUG Leader (KansaiJUG)
blog:Fight the Future
http://jyukutyo.hatenablog.com/
FURYU CORPORATION
age: 36
Koichi Sakata (阪田 浩一)
フリューの阪田です 関ジャバの会長です
I	
  ♥	
  JVM
this	
  session	
  is	
  
for	
  beginners	
  
about	
  JVM	
  
byte	
  codes
このセッションは
JVMのバイトコード初心者向けです
Do	
  you	
  know	
  
what	
  happens	
  
when	
  we	
  compile	
  
Scala	
  code?
Scalaコードをコンパイルする時
何が起こるかご存じでしょうか?
Java
Scala
JRuby
Groovy
C
o
m
p
i
l
e
r
Class	
  
file
cafe babe
0000 0032
0017 0100
JVM
Class	
  
file
Class	
  
file
Class	
  
file
Class	
  
file
Class	
  
file § Runs
§ Interprets
§ Translates
bytecode
into
Native
Machine
Code
Have	
  you	
  ever	
  
opened	
  	
  
a	
  class	
  file?
クラスファイルを開いたことはありますか
First,	
  	
  
let's	
  try	
  opening	
  	
  
a	
  class	
  file	
  	
  
from	
  a	
  simple	
  	
  
Java	
  code
Javaのクラスファイルを開いてみましょう
Hello	
  World	
  (Java)	
  
public	
  class	
  HelloWorld	
  {	
  
	
  	
  public	
  static	
  void	
  main(String[]	
  args)	
  {	
  
	
  	
  	
  	
  System.out.println("Hello,	
  world!");	
  
	
  	
  }	
  
}	
  
§ Compiled	
  into...	
  
§ HelloWorld.class	
  
open	
  
HelloWorld.class	
  
file	
  	
  
with	
  binary	
  editor	
  
e.g.)	
  hexl-­‐find-­‐file	
  
(Emacs)
クラスファイルをバイナリエディタで開く
cafe	
  babe	
  0000	
  0032	
  0017	
  0100	
  0a48	
  656c	
  
6c6f	
  576f	
  726c	
  6407	
  0001	
  0100	
  106a	
  6176	
  
612f	
  6c61	
  6e67	
  2f4f	
  626a	
  6563	
  7407	
  0003	
  
0100	
  1048	
  656c	
  6c6f	
  576f	
  726c	
  642e	
  7363	
  
616c	
  6101	
  001e	
  4c73	
  6361	
  6c61	
  2f72	
  6566	
  
6c65	
  6374	
  2f53	
  6361	
  6c61	
  5369	
  676e	
  6174	
  
7572	
  653b	
  0100	
  0562	
  7974	
  6573	
  0100	
  ef06	
  
0115	
  3a51	
  2101	
  0209	
  0215	
  0921	
  0253	
  336d	
  
593e	
  3c76	
  4e1d	
  3765	
  1505	
  1911	
  6102	
  1f66	
  
5b42	
  2418	
  5050	
  0201	
  2109	
  3171	
  2144	
  0103	
  
0d15	
  4121	
  0123	
  010a	
  0529	
  4155	
  0d1c	
  3770	
  
2f3e	
  1448	
  0e5a	
  0a03	
  0f29	
  0122	
  6103	
  080e	
  
0331	
  5111	
  2144	
  0106	
  670e	
  0c47	
  2e59	
  0503	
  
1f31	
  1161	
  2111	
  387a	
  2516	
  3407	
  2242	
  0908
you	
  will	
  
close	
  the	
  editor	
  
right	
  away
すぐにエディタを閉じるでしょう
class	
  file	
  
has	
  a	
  format
クラスファイルには
フォーマットがあります
ClassFile	
  {	
  
	
  	
  u4	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  magic;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  minor_version;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  major_version;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  constant_pool_count;	
  
	
  	
  cp_info	
  	
  constant_pool[constant_pool_count-­‐1];	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  access_flags;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  this_class;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  super_class;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  interfaces_count;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  interfaces[interfaces_count];	
  
...	
  
-­‐-­‐	
  u1	
  represents	
  one-­‐byte	
  quantity,	
  
	
  	
  	
  Hexadecimal	
  2	
  digit.
...	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  fields_count;	
  
	
  	
  field_info	
  	
  	
  	
  	
  fields[fields_count];	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  methods_count;	
  
	
  	
  method_info	
  	
  	
  methods[methods_count];	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  attributes_count;	
  
	
  	
  attribute_info	
  attributes[attributes_count];	
  
}	
  
-­‐-­‐	
  u1	
  represents	
  one-­‐byte	
  quantity,	
  
	
  	
  	
  Hexadecimal	
  2	
  digit.
u1が1バイト
16進数では2桁になります
Let's	
  
try	
  again
cafe	
  babe	
  0000	
  0032	
  0017	
  0100	
  0a48	
  656c	
  
6c6f	
  576f	
  726c	
  6407	
  0001	
  0100	
  106a	
  6176	
  
612f	
  6c61	
  6e67	
  2f4f	
  626a	
  6563	
  7407	
  0003	
  
0100	
  1048	
  656c	
  6c6f	
  576f	
  726c	
  642e	
  7363	
  
616c	
  6101	
  001e	
  4c73	
  6361	
  6c61	
  2f72	
  6566	
  
6c65	
  6374	
  2f53	
  6361	
  6c61	
  5369	
  676e	
  6174	
  
7572	
  653b	
  0100	
  0562	
  7974	
  6573	
  0100	
  ef06	
  
0115	
  3a51	
  2101	
  0209	
  0215	
  0921	
  0253	
  336d	
  
593e	
  3c76	
  4e1d	
  3765	
  1505	
  1911	
  6102	
  1f66	
  
5b42	
  2418	
  5050	
  0201	
  2109	
  3171	
  2144	
  0103	
  
0d15	
  4121	
  0123	
  010a	
  0529	
  4155	
  0d1c	
  3770	
  
2f3e	
  1448	
  0e5a	
  0a03	
  0f29	
  0122	
  6103	
  080e	
  
0331	
  5111	
  2144	
  0106	
  670e	
  0c47	
  2e59	
  0503	
  
1f31	
  1161	
  2111	
  387a	
  2516	
  3407	
  2242	
  0908
cafe	
  babe	
  0000	
  0032	
  
ClassFile	
  {	
  
	
  	
  u4	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  magic;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  minor_version;	
  
	
  	
  u2	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  major_version;	
  
"magic"	
  is	
  always	
  
"0xCAFEBABE"
major:	
  0x0032	
  =	
  50	
  
minor:	
  0x0000	
  =	
  0	
  
50.0	
  means	
  Java	
  SE	
  6	
  
51.0	
  is	
  7,	
  52.0	
  is	
  8...
Yes!
but,	
  this	
  is	
  
purgatory
でもこれは苦行です
Never	
  mind!	
  
We	
  have	
  
a	
  handy	
  tool!
便利なツールがあります!
javap	
  command	
  
The	
  Java	
  Class	
  File	
  Disassembler	
  
§ included	
  in	
  JDK	
  
§ `javap	
  -­‐v	
  [FQCN]`	
  
§ Scala	
  REPL	
  
§ scala>	
  :javap	
  [FQCN]	
  
§ equivalent	
  to	
  `javap	
  -­‐v`	
  
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html
javap - Java クラスファイル逆アセンブラ
so,	
  a	
  class	
  file	
  	
  
can	
  be	
  
disassembled	
  
to	
  Java	
  Classes
クラスファイルはJavaのクラスに
逆アセンブルできるのです
javap	
  -­‐v	
  HelloWorld
public	
  class	
  HelloWorld	
  
	
  	
  minor	
  version:	
  0	
  
	
  	
  major	
  version:	
  52	
  
Constant	
  pool:	
  
	
  	
  	
  #1	
  =	
  Methodref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #6.#15	
  	
  	
  	
  	
  	
  	
  	
  
//	
  java/lang/Object."<init>":()V	
  
	
  	
  	
  #2	
  =	
  Fieldref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #16.#17	
  	
  	
  	
  	
  	
  	
  
//	
  java/lang/System.out:Ljava/io/
PrintStream;	
  
	
  	
  	
  #3	
  =	
  String	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #18	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Hello,	
  world!	
  
	
  	
  	
  #4	
  =	
  Methodref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #19.#20	
  	
  	
  	
  	
  	
  	
  
//	
  java/io/PrintStream.println:(Ljava/
lang/String;)V
Constant	
  pool	
  is	
  
a	
  table	
  of	
  
symbolic	
  
information
コンスタントプールは
シンボル情報のテーブル
public	
  static	
  void	
  
main(java.lang.String[]);	
  
	
  Code:	
  
	
  	
  	
  	
  	
  	
  	
  0:	
  getstatic	
  	
  	
  	
  	
  #2	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  3:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #3	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #4	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  8:	
  return	
  
JVM	
  instructions	
  
aaload aastore aconst_null aload
anewarray areturn arraylength astore
athrow baload bastore bipush
caload castore checkcast d2f
d2i d2l dadd daload
dastore dcmp<op> dconst_<d> ddiv
dload dmul dneg ...
about	
  200	
  
instructions
Since	
  first	
  
release	
  of	
  Java,	
  
added	
  instruction	
  
is	
  ...
Javaが最初にリリースされてから
追加されたJVM命令は...
only	
  ONE!
Initial	
  design	
  	
  
of	
  JVM	
  	
  
is	
  excellent!
JVMは初期設計から優秀だった!
instructions	
  of	
  method	
  call	
  
invokevirtual
invoke	
  instance	
  method;	
  
dispatched	
  based	
  on	
  class
invokeinterface invoke	
  interface	
  method
invokestatic invoke	
  static	
  method
invokespecial
invoke	
  method;	
  
superclass,	
  private,	
  
constructor...
invokedynamic invoke	
  dynamic	
  method
public	
  static	
  void	
  
main(java.lang.String[]);	
  
	
  Code:	
  
	
  	
  	
  	
  	
  	
  	
  0:	
  getstatic	
  	
  	
  	
  	
  #2	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  3:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #3	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #4	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  8:	
  return	
  
-­‐-­‐	
  #0	
  =	
  index	
  of	
  Constant	
  pool  
public	
  class	
  HelloWorld	
  
Constant	
  pool:	
  
	
  	
  	
  #1	
  =	
  Methodref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #6.#15	
  	
  	
  	
  	
  	
  	
  	
  
//	
  java/lang/Object."<init>":()V	
  
	
  	
  	
  #2	
  =	
  Fieldref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #16.#17	
  	
  	
  	
  	
  	
  	
  
//	
  java/lang/System.out:Ljava/io/
PrintStream;	
  
	
  	
  	
  #3	
  =	
  String	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #18	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Hello,	
  world!	
  
	
  	
  	
  #4	
  =	
  Methodref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #19.#20	
  	
  	
  	
  	
  	
  	
  
//	
  java/io/PrintStream.println:(Ljava/
lang/String;)V	
  
...
OK!	
  
It's	
  Scala's	
  turn!
Hello	
  world
Hello	
  World	
  (Scala)	
  
object	
  HelloWorld	
  {	
  
	
  	
  def	
  main(args:	
  Array[String])	
  {	
  
	
  	
  	
  	
  println("Hello,	
  world!")	
  
	
  	
  }	
  
}	
  
§ Compiled	
  into...	
  
§ HelloWorld.class	
  
§ HelloWorld$.class	
  
public	
  final	
  class	
  HelloWorld	
  
Constant	
  pool:	
  
	
  	
  #15	
  =	
  NameAndType	
  	
  	
  	
  	
  	
  	
  	
  #13:#14	
  	
  	
  	
  	
  	
  	
  
//	
  MODULE$:LHelloWorld$;	
  
	
  	
  #16	
  =	
  Fieldref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #12.#15	
  	
  	
  	
  	
  	
  	
  
//	
  HelloWorld$.MODULE$:LHelloWorld$;	
  
	
  	
  #17	
  =	
  NameAndType	
  	
  	
  	
  	
  	
  	
  	
  #9:#10	
  	
  	
  	
  	
  	
  	
  	
  
//	
  main:([Ljava/lang/String;)V	
  
	
  	
  #18	
  =	
  Methodref	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #12.#17	
  	
  	
  	
  	
  	
  	
  
//	
  HelloWorld$.main:	
  
([Ljava/lang/String;)V	
  
...
public	
  static	
  void	
  	
  
main(java.lang.String[]);	
  
	
  	
  0:	
  getstatic	
  	
  	
  	
  	
  #16	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Field	
  HelloWorld$.	
  
MODULE$:LHelloWorld$;	
  
	
  	
  3:	
  aload_0	
  
	
  	
  4:	
  invokevirtual	
  #18	
  
//	
  Method	
  HelloWorld$.main:	
  
([Ljava/lang/String;)V	
  
	
  	
  7:	
  return	
  
-­‐-­‐	
  4:	
  in	
  Java	
  
HelloWorld$.MODULE$.main(arg);
public	
  final	
  class	
  HelloWorld$	
  
-­‐-­‐	
  omit	
  Constant	
  pool  
	
  	
  public	
  static	
  {};	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  0:	
  new	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #2	
  	
  	
  	
  	
  	
  
//	
  class	
  HelloWorld$	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  3:	
  invokespecial	
  #12	
  	
  	
  	
  	
  
//	
  Method	
  "<init>":()V	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  6:	
  return	
  
-­‐-­‐	
  <init>	
  means	
  constructor	
  
 	
  public	
  void	
  main(java.lang.String[]);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  0:	
  getstatic	
  	
  	
  	
  	
  #19	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Field	
  scala/Predef$.MODULE$:	
  
Lscala/Predef$;	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  3:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #21	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  Hello,	
  world!	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #25	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  scala/Predef$.println:	
  
(Ljava/lang/Object;)V	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  8:	
  return	
  
you	
  can	
  read	
  it!
The	
  following	
  samples	
  
are	
  quoted	
  	
  
from	
  tutorials	
  	
  
in	
  Scala	
  official	
  site	
  
http://docs.scala-­‐lang.org/tutorials/
これ以降で使用するサンプルは
Scalaの公式サイトのチュートリアルです
Traits
trait	
  Similarity	
  {	
  
	
  	
  def	
  isSimilar(x:	
  Any):	
  Boolean	
  
	
  	
  def	
  isNotSimilar(x:	
  Any):	
  Boolean	
  =	
  
	
  !isSimilar(x)	
  
}	
  
class	
  Point(xc:	
  Int,	
  yc:	
  Int)	
  	
  
extends	
  Similarity	
  {	
  
	
  	
  var	
  x:	
  Int	
  =	
  xc	
  
	
  	
  var	
  y:	
  Int	
  =	
  yc	
  
	
  	
  def	
  isSimilar(obj:	
  Any)	
  =	
  
	
  	
  	
  	
  obj.isInstanceOf[Point]	
  &&	
  
	
  	
  	
  	
  obj.asInstanceOf[Point].x	
  ==	
  x	
  
}	
  
§ Compiled	
  into...	
  
§ Similarity.class	
  
§ Similarity$class.class	
  
§ Point.class	
  
javap	
  -­‐v
public	
  interface	
  Similarity	
  
	
  	
  public	
  abstract	
  boolean	
  	
  
isSimilar(java.lang.Object);	
  
	
  	
  	
  
	
  	
  public	
  abstract	
  boolean	
  	
  
isNotSimilar(java.lang.Object);	
  
public	
  abstract	
  class	
  Similarity$class	
  
	
  	
  public	
  static	
  boolean	
  	
  
isNotSimilar(Similarity,j.l.Object);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  0:	
  aload_0	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  1:	
  aload_1	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  2:	
  invokeinterface	
  #13,	
  	
  2	
  	
  	
  	
  	
  	
  
//	
  InterfaceMethod	
  
Similarity.isSimilar:	
  
(Ljava/lang/Object;)Z	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  7:	
  ifeq	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  14	
  
	
  	
  	
  	
  	
  	
  	
  	
  10:	
  iconst_0	
  
	
  	
  	
  	
  	
  	
  	
  	
  11:	
  goto	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  15	
  
	
  	
  	
  	
  	
  	
  	
  	
  14:	
  iconst_1	
  
	
  	
  	
  	
  	
  	
  	
  	
  15:	
  ireturn
a	
  trait	
  is	
  	
  
divided	
  into	
  	
  
an	
  interface	
  and	
  
an	
  abstract	
  class
トレイトはインタフェースと
抽象クラスに分かれます
Case	
  Classes
abstract	
  class	
  Term	
  
case	
  class	
  Var(name:	
  String)	
  
	
  extends	
  Term	
  
case	
  class	
  Fun(arg:	
  String,	
  body:	
  Term)	
  	
  
extends	
  Term	
  
case	
  class	
  App(f:	
  Term,	
  v:	
  Term)	
  	
  
extends	
  Term	
  
§ Compiled	
  into...	
  
§ Term.class	
  
§ App.class	
  
§ App$.class	
  
§ Fun.class	
  
§ Fun$.class	
  
§ Var.class	
  
§ Var$.class	
  
javap	
  -­‐v
public	
  class	
  App	
  extends	
  Term	
  
	
  implements	
  s.Product,s.Serializable	
  
	
  	
  public	
  static	
  	
  
s.Option<scala.Tuple2<Term,	
  Term>>	
  
unapply(App);	
  
	
  	
  public	
  static	
  App	
  apply(Term,	
  Term);	
  
	
  	
  public	
  ...	
  tupled();	
  
	
  	
  public	
  ...	
  curried();	
  
	
  	
  public	
  Term	
  f();	
  
	
  	
  public	
  Term	
  v();	
  
	
  	
  public	
  App	
  copy(Term,	
  Term);	
  
	
  	
  public	
  j.l.String	
  productPrefix();	
  
	
  	
  public	
  boolean	
  equals(j.l.Object);	
  
...
public	
  final	
  class	
  App$	
  extends	
  	
  
s.r.AbstractFunction2<Term,	
  Term,	
  App>	
  
implements	
  scala.Serializable	
  
	
  	
  public	
  App	
  apply(Term,	
  Term);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  0:	
  new	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #23	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  class	
  App	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  3:	
  dup	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  4:	
  aload_1	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  5:	
  aload_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  6:	
  invokespecial	
  #26	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  App."<init>":(LTerm;LTerm;)V	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  9:	
  areturn	
  
Fun	
  and	
  Var	
  looks	
  
the	
  same	
  as	
  App
FunとVarも同様の内容です
Scala	
  compiler	
  
generates	
  	
  
many	
  functions	
  
from	
  few	
  snippets	
  
of	
  code
Scalaコンパイラは少ないコードから
多くの機能を生成しています
Pattern	
  Matching
object	
  MatchTest2	
  extends	
  App	
  {	
  
	
  	
  def	
  matchTest(x:	
  Any):	
  Any	
  =	
  x	
  match	
  
	
  	
  {	
  
	
  	
  	
  	
  case	
  1	
  =>	
  "one"	
  
	
  	
  	
  	
  case	
  "two"	
  =>	
  2	
  
	
  	
  	
  	
  case	
  y:	
  Int	
  =>	
  "scala.Int"	
  
	
  	
  }	
  
	
  	
  println(matchTest("two"))	
  
}
§ Compiled	
  into...	
  
§ MatchTest2.class	
  
§ MatchTest2$.class	
  
§ MatchTest2$delayedInit$body.class	
  
javap	
  -­‐v
public	
  final	
  class	
  MatchTest2$	
  	
  
implements	
  scala.App	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  7:	
  invokestatic	
  	
  #67	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  BoxesRunTime.equals:..	
  
	
  	
  	
  	
  	
  	
  	
  	
  10:	
  ifeq	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  19	
  
	
  	
  	
  	
  	
  	
  	
  	
  13:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #69	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  one	
  
	
  	
  	
  	
  	
  	
  	
  	
  15:	
  astore_3	
  
	
  	
  	
  	
  	
  	
  	
  	
  16:	
  goto	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  46	
  
	
  	
  	
  	
  	
  	
  	
  	
  19:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #71	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  two	
  
	
  	
  	
  	
  	
  	
  	
  	
  21:	
  aload_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  22:	
  invokevirtual	
  #74	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  java/lang/Object.equals:	
  
 	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  19:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #71	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  two	
  
	
  	
  	
  	
  	
  	
  	
  	
  21:	
  aload_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  22:	
  invokevirtual	
  #74	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  java/lang/Object.equals:	
  
(Ljava/lang/Object;)Z	
  
	
  	
  	
  	
  	
  	
  	
  	
  25:	
  ifeq	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  36	
  
	
  	
  	
  	
  	
  	
  	
  	
  28:	
  iconst_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  32:	
  astore_3	
  
	
  	
  	
  	
  	
  	
  	
  	
  33:	
  goto	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  46
 	
  	
  	
  	
  	
  	
  	
  36:	
  aload_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  37:	
  instanceof	
  	
  	
  	
  #76	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  class	
  java/lang/Integer	
  
	
  	
  	
  	
  	
  	
  	
  	
  40:	
  ifeq	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  48	
  
	
  	
  	
  	
  	
  	
  	
  	
  43:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #78	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  scala.Int	
  
	
  	
  	
  	
  	
  	
  	
  	
  45:	
  astore_3	
  
	
  	
  	
  	
  	
  	
  	
  	
  46:	
  aload_3	
  
	
  	
  	
  	
  	
  	
  	
  	
  47:	
  areturn	
  
...
Compiled	
  into	
  
if	
  statements
if文へコンパイルしています
in	
  case	
  of	
  
another	
  Pattern	
  
Matching...
違うパターンマッチの場合
object	
  MatchTest1	
  {	
  
	
  	
  def	
  matchTest(x:	
  Int):	
  String	
  =	
  x	
  
match	
  {	
  
	
  	
  	
  	
  case	
  1	
  =>	
  "one"	
  
	
  	
  	
  	
  case	
  2	
  =>	
  "two"	
  
	
  	
  	
  	
  case	
  _	
  =>	
  "many"	
  
	
  	
  }	
  
	
  	
  def	
  main(args:	
  Array[String])	
  {	
  
	
  	
  	
  	
  println(matchTest(3))	
  
	
  	
  }	
  
}
javap	
  -­‐v
 	
  	
  	
  	
  	
  	
  	
  	
  3:	
  tableswitch	
  	
  	
  {	
  //	
  1	
  to	
  2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  1:	
  34	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  2:	
  29	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  default:	
  24	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  	
  	
  24:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #16	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  many	
  
	
  	
  	
  	
  	
  	
  	
  	
  26:	
  goto	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  36	
  
	
  	
  	
  	
  	
  	
  	
  	
  29:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #18	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  two	
  
	
  	
  	
  	
  	
  	
  	
  	
  31:	
  goto	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  36	
  
	
  	
  	
  	
  	
  	
  	
  	
  34:	
  ldc	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  #20	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  String	
  one	
  
	
  	
  	
  	
  	
  	
  	
  	
  36:	
  areturn
Compiled	
  into	
  
a	
  switch	
  statement
switch文にコンパイルしています
Currying
object	
  CurryTest	
  {	
  
	
  	
  def	
  filter(xs:	
  List[Int],	
  	
  
p:	
  Int	
  =>	
  Boolean):	
  List[Int]	
  =	
  
	
  	
  	
  	
  if	
  (xs.isEmpty)	
  xs	
  
	
  	
  	
  	
  else	
  if	
  (p(xs.head))	
  	
  
xs.head	
  ::	
  filter(xs.tail,	
  p)	
  
	
  	
  	
  	
  else	
  filter(xs.tail,	
  p)	
  
	
  	
  def	
  modN(n:	
  Int)(x:	
  Int)	
  =	
  	
  
((x	
  %	
  n)	
  ==	
  0)	
  
 	
  def	
  main(args:	
  Array[String])	
  {	
  
	
  	
  	
  	
  val	
  nums	
  =	
  List(1,	
  2,	
  3,	
  4,	
  	
  
5,	
  6,	
  7,	
  8)	
  
	
  	
  	
  	
  println(filter(nums,	
  modN(2)))	
  
	
  	
  	
  	
  println(filter(nums,	
  modN(3)))	
  
	
  	
  }	
  
}	
  
§ Compiled	
  into...	
  
§ CurryTest.class	
  
§ CurryTest$.class	
  
§ CurryTest$$anonfun$main$1.class	
  
§ CurryTest$$anonfun$main$2.class	
  
javap	
  -­‐v
public	
  final	
  class	
  	
  
CurryTest$$anonfun$main$1	
  extends	
  
scala.runtime.AbstractFunction1$mcZI$sp	
  
implements	
  scala.Serializable	
  
	
  	
  public	
  boolean	
  apply$mcZI$sp(int);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  3:	
  iconst_2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  4:	
  iload_1	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #33	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  CurryTest$.modN:(II)Z	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  8:	
  ireturn	
  
public	
  final	
  class	
  	
  
CurryTest$$anonfun$main$2	
  extends	
  
scala.runtime.AbstractFunction1$mcZI$sp	
  
implements	
  scala.Serializable	
  
	
  	
  public	
  boolean	
  apply$mcZI$sp(int);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  3:	
  iconst_3	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  4:	
  iload_1	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  5:	
  invokevirtual	
  #33	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  CurryTest$.modN:(II)Z	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  8:	
  ireturn	
  
 	
  def	
  main(args:	
  Array[String])	
  {	
  
	
  	
  	
  	
  val	
  nums	
  =	
  List(1,	
  2,	
  3,	
  4,	
  	
  
5,	
  6,	
  7,	
  8)	
  
	
  	
  	
  	
  println(filter(nums,	
  modN(2)))	
  
	
  	
  	
  	
  println(filter(nums,	
  modN(3)))	
  
	
  	
  }	
  
}	
  
each	
  partial	
  
application	
  
creates	
  	
  
a	
  new	
  class
部分適用の箇所ごとに
クラスを生成しています
public	
  final	
  class	
  CurryTest$	
  
...	
  
	
  	
  public	
  void	
  main(java.lang.String[]);	
  
	
  	
  	
  	
  	
  	
  	
  	
  63:	
  invokespecial	
  #82	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  CurryTest$$anonfun$main$1.	
  
"<init>":()V	
  
	
  	
  	
  	
  	
  	
  	
  	
  66:	
  invokevirtual	
  #41	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  filter:(Lscala/collection/	
  
immutable/List;Lscala/Function1;)	
  
Lscala/collection/immutable/List;	
  
	
  	
  	
  	
  	
  	
  	
  	
  69:	
  invokevirtual	
  #86	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  scala/Predef$.println:	
  
(Ljava/lang/Object;)V
public	
  final	
  class	
  	
  
CurryTest$$anonfun$main$1	
  extends	
  
scala.runtime.AbstractFunction1$mcZI$sp	
  
implements	
  scala.Serializable	
  
CurryTest$$anonfun
$main$2	
  
is	
  same
$2も同様です
 	
  	
  	
  	
  	
  	
  	
  81:	
  invokespecial	
  #89	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  CurryTest$$anonfun$main$2.	
  
"<init>":()V	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  84:	
  invokevirtual	
  #41	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  filter:(Lscala/collection/	
  
immutable/List;Lscala/Function1;)	
  
Lscala/collection/immutable/List;	
  
	
  	
  	
  	
  	
  	
  	
  	
  87:	
  invokevirtual	
  #86	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
//	
  Method	
  scala/Predef$.println:	
  
(Ljava/lang/Object;)V	
  
§ Summary	
  
§ JVM	
  is	
  excellent!	
  
§ class	
  file	
  has	
  a	
  format	
  
§ javap	
  is	
  useful	
  to	
  understand	
  	
  	
  
Scala	
  at	
  the	
  bytecode	
  level	
  	
  
Try	
  typing	
  "javap"	
  
after	
  you	
  compile	
  
Scala	
  code
Thank	
  you!

More Related Content

What's hot (20)

PDF
Zen of Akka
Konrad Malawski
 
PDF
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
scalaconfjp
 
PDF
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
scalaconfjp
 
PPT
55 New Features in Java 7
Boulder Java User's Group
 
PDF
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
KEY
The Why and How of Scala at Twitter
Alex Payne
 
PDF
Scala coated JVM
Stuart Roebuck
 
KEY
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
PPTX
From Ruby to Scala
tod esking
 
KEY
Static or Dynamic Typing? Why not both?
Mario Camou Riveroll
 
PPTX
Java and OpenJDK: disecting the ecosystem
Rafael Winterhalter
 
PDF
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
scalaconfjp
 
PPTX
Java Serialization Facts and Fallacies
Roman Elizarov
 
PDF
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
Christopher Frohoff
 
PDF
A new execution model for Nashorn in Java 9
Marcus Lagergren
 
PDF
Lagergren jvmls-2013-final
Marcus Lagergren
 
PDF
Why GC is eating all my CPU?
Roman Elizarov
 
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
PDF
Alternatives of JPA/Hibernate
Sunghyouk Bae
 
PDF
Java 5 and 6 New Features
Jussi Pohjolainen
 
Zen of Akka
Konrad Malawski
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
scalaconfjp
 
Scarab: SAT-based Constraint Programming System in Scala / Scala上で実現された制約プログラ...
scalaconfjp
 
55 New Features in Java 7
Boulder Java User's Group
 
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
The Why and How of Scala at Twitter
Alex Payne
 
Scala coated JVM
Stuart Roebuck
 
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
From Ruby to Scala
tod esking
 
Static or Dynamic Typing? Why not both?
Mario Camou Riveroll
 
Java and OpenJDK: disecting the ecosystem
Rafael Winterhalter
 
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
scalaconfjp
 
Java Serialization Facts and Fallacies
Roman Elizarov
 
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
Christopher Frohoff
 
A new execution model for Nashorn in Java 9
Marcus Lagergren
 
Lagergren jvmls-2013-final
Marcus Lagergren
 
Why GC is eating all my CPU?
Roman Elizarov
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
Doug Hawkins
 
Alternatives of JPA/Hibernate
Sunghyouk Bae
 
Java 5 and 6 New Features
Jussi Pohjolainen
 

Similar to How Scala code is expressed in the JVM (20)

PDF
Inside the JVM - Follow the white rabbit! / Breizh JUG
Sylvain Wallez
 
PDF
In Vogue Dynamic
Alexander Shopov
 
PPTX
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 
PDF
No dark magic - Byte code engineering in the real world
tcurdt
 
PDF
Inside the JVM - Follow the white rabbit!
Sylvain Wallez
 
PDF
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
PPTX
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 
PPT
Mastering Java ByteCode
Ecommerce Solution Provider SysIQ
 
PDF
Down the Rabbit Hole: An Adventure in JVM Wonderland
Charles Nutter
 
PDF
FTD JVM Internals
Felipe Mamud
 
PPT
Object Oriented Programming-JAVA
Home
 
PDF
Proceedings Of The 2002 Acm Sigplan Haskell Workshop Haskell 02 Pittsburgh Pe...
qendritaef
 
PDF
Java Bytecode for Discriminating Developers - JavaZone 2011
Anton Arhipov
 
PDF
Jvm internals
Luiz Fernando Teston
 
PDF
engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!
René Winkelmeyer
 
PDF
Learn Advanced Java Programming With Beginners Md Pulok
lwafaziurka
 
PDF
Shiksharth com java_topics
Rajesh Verma
 
PPTX
Java On Speed
Arto Santala
 
DOCX
Introduction to java programming tutorial
jackschitze
 
PDF
Monkeybars in the Manor
martinbtt
 
Inside the JVM - Follow the white rabbit! / Breizh JUG
Sylvain Wallez
 
In Vogue Dynamic
Alexander Shopov
 
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 
No dark magic - Byte code engineering in the real world
tcurdt
 
Inside the JVM - Follow the white rabbit!
Sylvain Wallez
 
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 
Mastering Java ByteCode
Ecommerce Solution Provider SysIQ
 
Down the Rabbit Hole: An Adventure in JVM Wonderland
Charles Nutter
 
FTD JVM Internals
Felipe Mamud
 
Object Oriented Programming-JAVA
Home
 
Proceedings Of The 2002 Acm Sigplan Haskell Workshop Haskell 02 Pittsburgh Pe...
qendritaef
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Anton Arhipov
 
Jvm internals
Luiz Fernando Teston
 
engage 2016 - Get ready for moving from Java 6 to Java 8 - Now!
René Winkelmeyer
 
Learn Advanced Java Programming With Beginners Md Pulok
lwafaziurka
 
Shiksharth com java_topics
Rajesh Verma
 
Java On Speed
Arto Santala
 
Introduction to java programming tutorial
jackschitze
 
Monkeybars in the Manor
martinbtt
 
Ad

More from Koichi Sakata (20)

PPTX
Introduction to JIT Compiler in JVM
Koichi Sakata
 
PPTX
Guide to GraalVM (Oracle Groundbreakers APAC 2019 Tour in Tokyo)
Koichi Sakata
 
PPTX
Guide to GraalVM (JJUG CCC 2019 Fall)
Koichi Sakata
 
PPTX
Introduction to GraalVM and Native Image
Koichi Sakata
 
PPTX
Introduction to GraalVM
Koichi Sakata
 
PPTX
GraalVMで使われている、他言語をJVM上に実装する仕組みを学ぼう
Koichi Sakata
 
PPTX
Bytecode Manipulation with a Java Agent and Byte Buddy
Koichi Sakata
 
PPTX
Great Ideas in GraalVM
Koichi Sakata
 
PPTX
Graal in GraalVM - A New JIT Compiler
Koichi Sakata
 
PPTX
Kanjava 201804 Java News
Koichi Sakata
 
PPTX
KanJava 201804 Career 思い込みから逃れた先には、可能性がある
Koichi Sakata
 
PPTX
from Java EE to Jakarta EE
Koichi Sakata
 
PPTX
Java release cadence has been changed and about Project Amber
Koichi Sakata
 
PPTX
JJUG CCC 2017 Fall オレオレJVM言語を作ってみる
Koichi Sakata
 
PPTX
KANJAVA PARTY 2017 前説
Koichi Sakata
 
PPTX
JJUG CCC 2017 Spring Seasar2からSpringへ移行した俺たちのアプリケーションがマイクロサービスアーキテクチャへ歩み始めた
Koichi Sakata
 
PPTX
“Purikura” culture in Japan and our web application architecture
Koichi Sakata
 
PPTX
デブサミ2017 Javaコミュニティ作ったら人生変わった
Koichi Sakata
 
PPTX
JJUG CCC 2016 fall バイトコードが君のトモダチになりたがっている
Koichi Sakata
 
PPTX
日本からJavaOneに行こう!
Koichi Sakata
 
Introduction to JIT Compiler in JVM
Koichi Sakata
 
Guide to GraalVM (Oracle Groundbreakers APAC 2019 Tour in Tokyo)
Koichi Sakata
 
Guide to GraalVM (JJUG CCC 2019 Fall)
Koichi Sakata
 
Introduction to GraalVM and Native Image
Koichi Sakata
 
Introduction to GraalVM
Koichi Sakata
 
GraalVMで使われている、他言語をJVM上に実装する仕組みを学ぼう
Koichi Sakata
 
Bytecode Manipulation with a Java Agent and Byte Buddy
Koichi Sakata
 
Great Ideas in GraalVM
Koichi Sakata
 
Graal in GraalVM - A New JIT Compiler
Koichi Sakata
 
Kanjava 201804 Java News
Koichi Sakata
 
KanJava 201804 Career 思い込みから逃れた先には、可能性がある
Koichi Sakata
 
from Java EE to Jakarta EE
Koichi Sakata
 
Java release cadence has been changed and about Project Amber
Koichi Sakata
 
JJUG CCC 2017 Fall オレオレJVM言語を作ってみる
Koichi Sakata
 
KANJAVA PARTY 2017 前説
Koichi Sakata
 
JJUG CCC 2017 Spring Seasar2からSpringへ移行した俺たちのアプリケーションがマイクロサービスアーキテクチャへ歩み始めた
Koichi Sakata
 
“Purikura” culture in Japan and our web application architecture
Koichi Sakata
 
デブサミ2017 Javaコミュニティ作ったら人生変わった
Koichi Sakata
 
JJUG CCC 2016 fall バイトコードが君のトモダチになりたがっている
Koichi Sakata
 
日本からJavaOneに行こう!
Koichi Sakata
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 

How Scala code is expressed in the JVM

  • 1. Koichi  Sakata  (阪田  浩一)   @jyukutyo How  Scala  code  is   expressed  in  the  JVM   -­‐  ScalaMatsuri  2016  -­‐
  • 2. 関ジャバです! 2 @jyukutyo produced three Java framework books one was translated into Korean JUG Leader (KansaiJUG) blog:Fight the Future http://jyukutyo.hatenablog.com/ FURYU CORPORATION age: 36 Koichi Sakata (阪田 浩一) フリューの阪田です 関ジャバの会長です
  • 4. this  session  is   for  beginners   about  JVM   byte  codes このセッションは JVMのバイトコード初心者向けです
  • 5. Do  you  know   what  happens   when  we  compile   Scala  code? Scalaコードをコンパイルする時 何が起こるかご存じでしょうか?
  • 6. Java Scala JRuby Groovy C o m p i l e r Class   file cafe babe 0000 0032 0017 0100 JVM Class   file Class   file Class   file Class   file Class   file § Runs § Interprets § Translates bytecode into Native Machine Code
  • 7. Have  you  ever   opened     a  class  file? クラスファイルを開いたことはありますか
  • 8. First,     let's  try  opening     a  class  file     from  a  simple     Java  code Javaのクラスファイルを開いてみましょう
  • 9. Hello  World  (Java)   public  class  HelloWorld  {      public  static  void  main(String[]  args)  {          System.out.println("Hello,  world!");      }   }   § Compiled  into...   § HelloWorld.class  
  • 10. open   HelloWorld.class   file     with  binary  editor   e.g.)  hexl-­‐find-­‐file   (Emacs) クラスファイルをバイナリエディタで開く
  • 11. cafe  babe  0000  0032  0017  0100  0a48  656c   6c6f  576f  726c  6407  0001  0100  106a  6176   612f  6c61  6e67  2f4f  626a  6563  7407  0003   0100  1048  656c  6c6f  576f  726c  642e  7363   616c  6101  001e  4c73  6361  6c61  2f72  6566   6c65  6374  2f53  6361  6c61  5369  676e  6174   7572  653b  0100  0562  7974  6573  0100  ef06   0115  3a51  2101  0209  0215  0921  0253  336d   593e  3c76  4e1d  3765  1505  1911  6102  1f66   5b42  2418  5050  0201  2109  3171  2144  0103   0d15  4121  0123  010a  0529  4155  0d1c  3770   2f3e  1448  0e5a  0a03  0f29  0122  6103  080e   0331  5111  2144  0106  670e  0c47  2e59  0503   1f31  1161  2111  387a  2516  3407  2242  0908
  • 12. you  will   close  the  editor   right  away すぐにエディタを閉じるでしょう
  • 13. class  file   has  a  format クラスファイルには フォーマットがあります
  • 14. ClassFile  {      u4                          magic;      u2                          minor_version;      u2                          major_version;      u2                          constant_pool_count;      cp_info    constant_pool[constant_pool_count-­‐1];      u2                          access_flags;      u2                          this_class;      u2                          super_class;      u2                          interfaces_count;      u2            interfaces[interfaces_count];   ...   -­‐-­‐  u1  represents  one-­‐byte  quantity,        Hexadecimal  2  digit.
  • 15. ...      u2                          fields_count;      field_info          fields[fields_count];      u2                          methods_count;      method_info      methods[methods_count];      u2                          attributes_count;      attribute_info  attributes[attributes_count];   }   -­‐-­‐  u1  represents  one-­‐byte  quantity,        Hexadecimal  2  digit. u1が1バイト 16進数では2桁になります
  • 17. cafe  babe  0000  0032  0017  0100  0a48  656c   6c6f  576f  726c  6407  0001  0100  106a  6176   612f  6c61  6e67  2f4f  626a  6563  7407  0003   0100  1048  656c  6c6f  576f  726c  642e  7363   616c  6101  001e  4c73  6361  6c61  2f72  6566   6c65  6374  2f53  6361  6c61  5369  676e  6174   7572  653b  0100  0562  7974  6573  0100  ef06   0115  3a51  2101  0209  0215  0921  0253  336d   593e  3c76  4e1d  3765  1505  1911  6102  1f66   5b42  2418  5050  0201  2109  3171  2144  0103   0d15  4121  0123  010a  0529  4155  0d1c  3770   2f3e  1448  0e5a  0a03  0f29  0122  6103  080e   0331  5111  2144  0106  670e  0c47  2e59  0503   1f31  1161  2111  387a  2516  3407  2242  0908
  • 18. cafe  babe  0000  0032   ClassFile  {      u4                          magic;      u2                          minor_version;      u2                          major_version;  
  • 19. "magic"  is  always   "0xCAFEBABE"
  • 20. major:  0x0032  =  50   minor:  0x0000  =  0   50.0  means  Java  SE  6   51.0  is  7,  52.0  is  8...
  • 21. Yes!
  • 22. but,  this  is   purgatory でもこれは苦行です
  • 23. Never  mind!   We  have   a  handy  tool! 便利なツールがあります!
  • 24. javap  command   The  Java  Class  File  Disassembler   § included  in  JDK   § `javap  -­‐v  [FQCN]`   § Scala  REPL   § scala>  :javap  [FQCN]   § equivalent  to  `javap  -­‐v`   http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html javap - Java クラスファイル逆アセンブラ
  • 25. so,  a  class  file     can  be   disassembled   to  Java  Classes クラスファイルはJavaのクラスに 逆アセンブルできるのです
  • 27. public  class  HelloWorld      minor  version:  0      major  version:  52   Constant  pool:        #1  =  Methodref                    #6.#15                 //  java/lang/Object."<init>":()V        #2  =  Fieldref                      #16.#17               //  java/lang/System.out:Ljava/io/ PrintStream;        #3  =  String                          #18                       //  Hello,  world!        #4  =  Methodref                    #19.#20               //  java/io/PrintStream.println:(Ljava/ lang/String;)V
  • 28. Constant  pool  is   a  table  of   symbolic   information コンスタントプールは シンボル情報のテーブル
  • 29. public  static  void   main(java.lang.String[]);    Code:                0:  getstatic          #2                            3:  ldc                      #3                            5:  invokevirtual  #4                            8:  return  
  • 30. JVM  instructions   aaload aastore aconst_null aload anewarray areturn arraylength astore athrow baload bastore bipush caload castore checkcast d2f d2i d2l dadd daload dastore dcmp<op> dconst_<d> ddiv dload dmul dneg ...
  • 32. Since  first   release  of  Java,   added  instruction   is  ... Javaが最初にリリースされてから 追加されたJVM命令は...
  • 34. Initial  design     of  JVM     is  excellent! JVMは初期設計から優秀だった!
  • 35. instructions  of  method  call   invokevirtual invoke  instance  method;   dispatched  based  on  class invokeinterface invoke  interface  method invokestatic invoke  static  method invokespecial invoke  method;   superclass,  private,   constructor... invokedynamic invoke  dynamic  method
  • 36. public  static  void   main(java.lang.String[]);    Code:                0:  getstatic          #2                            3:  ldc                      #3                            5:  invokevirtual  #4                            8:  return   -­‐-­‐  #0  =  index  of  Constant  pool  
  • 37. public  class  HelloWorld   Constant  pool:        #1  =  Methodref                    #6.#15                 //  java/lang/Object."<init>":()V        #2  =  Fieldref                      #16.#17               //  java/lang/System.out:Ljava/io/ PrintStream;        #3  =  String                          #18                       //  Hello,  world!        #4  =  Methodref                    #19.#20               //  java/io/PrintStream.println:(Ljava/ lang/String;)V   ...
  • 40. Hello  World  (Scala)   object  HelloWorld  {      def  main(args:  Array[String])  {          println("Hello,  world!")      }   }   § Compiled  into...   § HelloWorld.class   § HelloWorld$.class  
  • 41. public  final  class  HelloWorld   Constant  pool:      #15  =  NameAndType                #13:#14               //  MODULE$:LHelloWorld$;      #16  =  Fieldref                      #12.#15               //  HelloWorld$.MODULE$:LHelloWorld$;      #17  =  NameAndType                #9:#10                 //  main:([Ljava/lang/String;)V      #18  =  Methodref                    #12.#17               //  HelloWorld$.main:   ([Ljava/lang/String;)V   ...
  • 42. public  static  void     main(java.lang.String[]);      0:  getstatic          #16                                 //  Field  HelloWorld$.   MODULE$:LHelloWorld$;      3:  aload_0      4:  invokevirtual  #18   //  Method  HelloWorld$.main:   ([Ljava/lang/String;)V      7:  return   -­‐-­‐  4:  in  Java   HelloWorld$.MODULE$.main(arg);
  • 43. public  final  class  HelloWorld$   -­‐-­‐  omit  Constant  pool      public  static  {};                    0:  new                      #2             //  class  HelloWorld$                    3:  invokespecial  #12           //  Method  "<init>":()V                    6:  return   -­‐-­‐  <init>  means  constructor  
  • 44.    public  void  main(java.lang.String[]);                    0:  getstatic          #19                         //  Field  scala/Predef$.MODULE$:   Lscala/Predef$;                    3:  ldc                      #21                         //  String  Hello,  world!                    5:  invokevirtual  #25                         //  Method  scala/Predef$.println:   (Ljava/lang/Object;)V                    8:  return  
  • 46. The  following  samples   are  quoted     from  tutorials     in  Scala  official  site   http://docs.scala-­‐lang.org/tutorials/ これ以降で使用するサンプルは Scalaの公式サイトのチュートリアルです
  • 48. trait  Similarity  {      def  isSimilar(x:  Any):  Boolean      def  isNotSimilar(x:  Any):  Boolean  =    !isSimilar(x)   }   class  Point(xc:  Int,  yc:  Int)     extends  Similarity  {      var  x:  Int  =  xc      var  y:  Int  =  yc      def  isSimilar(obj:  Any)  =          obj.isInstanceOf[Point]  &&          obj.asInstanceOf[Point].x  ==  x   }  
  • 49. § Compiled  into...   § Similarity.class   § Similarity$class.class   § Point.class  
  • 51. public  interface  Similarity      public  abstract  boolean     isSimilar(java.lang.Object);            public  abstract  boolean     isNotSimilar(java.lang.Object);  
  • 52. public  abstract  class  Similarity$class      public  static  boolean     isNotSimilar(Similarity,j.l.Object);                    0:  aload_0                    1:  aload_1                    2:  invokeinterface  #13,    2             //  InterfaceMethod   Similarity.isSimilar:   (Ljava/lang/Object;)Z                    7:  ifeq                    14                  10:  iconst_0                  11:  goto                    15                  14:  iconst_1                  15:  ireturn
  • 53. a  trait  is     divided  into     an  interface  and   an  abstract  class トレイトはインタフェースと 抽象クラスに分かれます
  • 55. abstract  class  Term   case  class  Var(name:  String)    extends  Term   case  class  Fun(arg:  String,  body:  Term)     extends  Term   case  class  App(f:  Term,  v:  Term)     extends  Term  
  • 56. § Compiled  into...   § Term.class   § App.class   § App$.class   § Fun.class   § Fun$.class   § Var.class   § Var$.class  
  • 58. public  class  App  extends  Term    implements  s.Product,s.Serializable      public  static     s.Option<scala.Tuple2<Term,  Term>>   unapply(App);      public  static  App  apply(Term,  Term);      public  ...  tupled();      public  ...  curried();      public  Term  f();      public  Term  v();      public  App  copy(Term,  Term);      public  j.l.String  productPrefix();      public  boolean  equals(j.l.Object);   ...
  • 59. public  final  class  App$  extends     s.r.AbstractFunction2<Term,  Term,  App>   implements  scala.Serializable      public  App  apply(Term,  Term);                    0:  new                      #23                         //  class  App                    3:  dup                    4:  aload_1                    5:  aload_2                    6:  invokespecial  #26                         //  Method  App."<init>":(LTerm;LTerm;)V                    9:  areturn  
  • 60. Fun  and  Var  looks   the  same  as  App FunとVarも同様の内容です
  • 61. Scala  compiler   generates     many  functions   from  few  snippets   of  code Scalaコンパイラは少ないコードから 多くの機能を生成しています
  • 63. object  MatchTest2  extends  App  {      def  matchTest(x:  Any):  Any  =  x  match      {          case  1  =>  "one"          case  "two"  =>  2          case  y:  Int  =>  "scala.Int"      }      println(matchTest("two"))   }
  • 64. § Compiled  into...   § MatchTest2.class   § MatchTest2$.class   § MatchTest2$delayedInit$body.class  
  • 66. public  final  class  MatchTest2$     implements  scala.App                    7:  invokestatic    #67                         //  Method  BoxesRunTime.equals:..                  10:  ifeq                    19                  13:  ldc                      #69                         //  String  one                  15:  astore_3                  16:  goto                    46                  19:  ldc                      #71                         //  String  two                  21:  aload_2                  22:  invokevirtual  #74                         //  Method  java/lang/Object.equals:  
  • 67.                                  19:  ldc                      #71                         //  String  two                  21:  aload_2                  22:  invokevirtual  #74                         //  Method  java/lang/Object.equals:   (Ljava/lang/Object;)Z                  25:  ifeq                    36                  28:  iconst_2                  32:  astore_3                  33:  goto                    46
  • 68.                36:  aload_2                  37:  instanceof        #76                         //  class  java/lang/Integer                  40:  ifeq                    48                  43:  ldc                      #78                         //  String  scala.Int                  45:  astore_3                  46:  aload_3                  47:  areturn   ...
  • 69. Compiled  into   if  statements if文へコンパイルしています
  • 70. in  case  of   another  Pattern   Matching... 違うパターンマッチの場合
  • 71. object  MatchTest1  {      def  matchTest(x:  Int):  String  =  x   match  {          case  1  =>  "one"          case  2  =>  "two"          case  _  =>  "many"      }      def  main(args:  Array[String])  {          println(matchTest(3))      }   }
  • 73.                  3:  tableswitch      {  //  1  to  2                                                1:  34                                                2:  29                                    default:  24                          }                  24:  ldc                      #16                         //  String  many                  26:  goto                    36                  29:  ldc                      #18                         //  String  two                  31:  goto                    36                  34:  ldc                      #20                         //  String  one                  36:  areturn
  • 74. Compiled  into   a  switch  statement switch文にコンパイルしています
  • 76. object  CurryTest  {      def  filter(xs:  List[Int],     p:  Int  =>  Boolean):  List[Int]  =          if  (xs.isEmpty)  xs          else  if  (p(xs.head))     xs.head  ::  filter(xs.tail,  p)          else  filter(xs.tail,  p)      def  modN(n:  Int)(x:  Int)  =     ((x  %  n)  ==  0)  
  • 77.    def  main(args:  Array[String])  {          val  nums  =  List(1,  2,  3,  4,     5,  6,  7,  8)          println(filter(nums,  modN(2)))          println(filter(nums,  modN(3)))      }   }  
  • 78. § Compiled  into...   § CurryTest.class   § CurryTest$.class   § CurryTest$$anonfun$main$1.class   § CurryTest$$anonfun$main$2.class  
  • 80. public  final  class     CurryTest$$anonfun$main$1  extends   scala.runtime.AbstractFunction1$mcZI$sp   implements  scala.Serializable      public  boolean  apply$mcZI$sp(int);                    3:  iconst_2                    4:  iload_1                    5:  invokevirtual  #33                         //  Method  CurryTest$.modN:(II)Z                    8:  ireturn  
  • 81. public  final  class     CurryTest$$anonfun$main$2  extends   scala.runtime.AbstractFunction1$mcZI$sp   implements  scala.Serializable      public  boolean  apply$mcZI$sp(int);                    3:  iconst_3                    4:  iload_1                    5:  invokevirtual  #33                         //  Method  CurryTest$.modN:(II)Z                    8:  ireturn  
  • 82.    def  main(args:  Array[String])  {          val  nums  =  List(1,  2,  3,  4,     5,  6,  7,  8)          println(filter(nums,  modN(2)))          println(filter(nums,  modN(3)))      }   }  
  • 83. each  partial   application   creates     a  new  class 部分適用の箇所ごとに クラスを生成しています
  • 84. public  final  class  CurryTest$   ...      public  void  main(java.lang.String[]);                  63:  invokespecial  #82                         //  Method  CurryTest$$anonfun$main$1.   "<init>":()V                  66:  invokevirtual  #41                         //  Method  filter:(Lscala/collection/   immutable/List;Lscala/Function1;)   Lscala/collection/immutable/List;                  69:  invokevirtual  #86                         //  Method  scala/Predef$.println:   (Ljava/lang/Object;)V
  • 85. public  final  class     CurryTest$$anonfun$main$1  extends   scala.runtime.AbstractFunction1$mcZI$sp   implements  scala.Serializable  
  • 87.                81:  invokespecial  #89                         //  Method  CurryTest$$anonfun$main$2.   "<init>":()V                                    84:  invokevirtual  #41                         //  Method  filter:(Lscala/collection/   immutable/List;Lscala/Function1;)   Lscala/collection/immutable/List;                  87:  invokevirtual  #86                         //  Method  scala/Predef$.println:   (Ljava/lang/Object;)V  
  • 88. § Summary   § JVM  is  excellent!   § class  file  has  a  format   § javap  is  useful  to  understand       Scala  at  the  bytecode  level     Try  typing  "javap"   after  you  compile   Scala  code