编译器如何在Java中翻译lambda表达式?

时间:2015-07-05 10:56:46

标签: java lambda java-8

program function1
implicit none
 real(10) :: g, a, x, b, k, s
 ! frequency intervals for function and her transform
 real(10) :: step, dstep
 ! number of inervals
 integer, parameter :: nmax=10
 integer :: j
 ! function and result of her transformation
 real(kind=10), dimension(0:nmax) :: f, wynik
 step=0.01
 dstep=0.1
 !constant value for function
 a=1
 b=-1
 s=2
 open(1, file='wykresik.dat', action='write', status='replace')
 ! loop over all frequencies in the function
do j=0,1000
  k=float(j)*step
  f=-1*cos(k*s)
enddo
  call filonc(step,dstep,nmax,f,wynik)
write(1,*) k, wynik
! filon subroutine call
end program function1
include 'filon.f95'

我想知道的是,编译器如何实现该语句?

  

() - > 98.6; //声明1

以上陈述相当于

SUBROUTINE FILONC ( DT, DOM, NMAX, D, CHAT )
    INTEGER :: NMAX
        REAL(kind=10) :: DT, DOM
        REAL(kind=10), dimension(0:NMAX) ::D, CHAT
    REAL(kind=10) :: TMAX, OMEGA, THETA, SINTH, COSTH, CE, CO
        REAL(kind=10) :: SINSQ, COSSQ, THSQ, THCUB, ALPHA, BETA, GAMMA
        INTEGER    TAU, NU

      IF ( MOD ( NMAX, 2 ) .NE. 0 ) then

      stop ' NMAX SHOULD BE EVEN '

      ENDIF

        TMAX = float(NMAX) * DT

       DO NU = 0, NMAX

           OMEGA = float(NU) * DOM
           THETA = OMEGA * DT



           SINTH = SIN ( THETA )
           COSTH = COS ( THETA )
           SINSQ = SINTH * SINTH
           COSSQ = COSTH * COSTH
           THSQ  = THETA * THETA
           THCUB = THSQ * THETA

           IF ( THETA .EQ. 0.0 ) THEN

              ALPHA = 0.0
              BETA  = 2.0 / 3.0
              GAMMA = 4.0 / 3.0

            ELSE

              ALPHA = ( 1.0 / THCUB )* ( THSQ + THETA * SINTH * COSTH - 2.0 * SINSQ )

              BETA  = ( 2.0 / THCUB )* ( THETA * ( 1.0 + COSSQ ) -2.0 * SINTH * COSTH )

              GAMMA = ( 4.0 / THCUB ) * ( SINTH - THETA * COSTH )

           ENDIF


       CE = 0.0

           DO  TAU = 0, NMAX, 2

              CE = CE + D(TAU) * COS ( THETA * float( TAU ) )

       ENDDO



           CE = CE - 0.5 * ( D(0) + D(NMAX) * COS ( OMEGA * TMAX ) )


           CO = 0.0

           DO  TAU = 1, NMAX - 1, 2

              CO = CO + D(TAU) * COS ( THETA * REAL ( TAU ) )

       ENDDO



           CHAT(NU) = 2.0 * ( ALPHA * D(NMAX) * SIN ( OMEGA * TMAX )+ BETA * CE + GAMMA * CO ) * DT

           ENDDO
ENDSUBROUTINE FILONC

那么,编译器

期间编译器是否将//Code 1 interface Demo { double myMeth(); } class MyClass { public static void main(String args[]) { Demo myDemo = () -> 98.6; } } 替换为//Code 2 double myMeth() { return 98.6; }

或编译器做了别的什么?

1 个答案:

答案 0 :(得分:-1)

语义上(虽然不是实现性的),它基本上与

相同
class MyClass {
    public static void main(String args[]) {
        Demo myDemo = new Demo() {
            public double myMeth() {
                return 98.6;
            }
        };
    }
}

编译器实际上并没有将lambda表达式转换为匿名类创建表达式。 Lambda表达式以更有效的方式实现。