
java论文
-
2023年3月1日发(作者:视贝照明)--.
--考试资料
AdvantagesofManagedCode
MicrosoftintermediatelanguageshareswithJavabytecodetheideathatitisalow-levellanguagewitha
simplesyntax,thiswell-defined
universalsyntaxforcodehassignificantadvantages.
Platformindependence
First,itmeansthatthesamefilecontainingbytecodeinstructionscanbeplacedonanyplatform;
atruntimethefinalstageofcompilationcanthenbeeasilyaccomplishedsothatthecodewillrunon
rwords,,
inmuchthesamewayascompilingtoJavabytecodegivesJavaplatformindependence.
Performanceimprovement
waysJust-In-Timecompiled(knownasJIT),
hedisadvantagesofJavawasthat,onexecution,the
processoftranslatingfromJavabytecodetonativeexecutableresultedinalossofperformance.
Insteadofcompilingtheentireapplicationinonego(whichcouldleadtoaslowstart-uptime),the
JITcompilersimplycompileseachportionofcodeasitiscalled(just-in-time).Whencodehasbeen
,theresultantnativeexecutableisstoreduntiltheapplicationexits,sothatitdoesnotneed
oftarguesthatthisprocessismore
efficientthancompilingtheentireapplicationcodeatthestart,becauseofthelikelihoodthatlargeportions
heJITcompiler,suchcode
willneverbecompiled.
--.
--考试资料
ThisexplainswhywecanexpectthatexecutionofmanagedILcodewillbealmostasfastasexecuting
doesn’texplainiswhyMicrosoftexpectsthatwewillgetaperformance
songivenforthisisthat,sincethefinalstageofcompilationtakesplaceat
runtime,thansthatit
canoptimizethefinalexecutablecodetotakeadvantageofanyfeaturesorparticularmachinecode
instructionsofferedbythatparticularprocessor.
实际上,IL比Java字节代码的作用还要大。IL总是即时编译的(简称JIT),而Java字节代码常常是解释型的,
Java的一个缺点是,在运行应用程序时,把Java字节代码转换为内部可执行代码的过程可可能导致性能的损失。
JIT编译器并不是把整个应用程序一次编译完(这样会有很长的启动时间),而是只编译它调用的那部分代
码。代码编译过一次后,得到的内部可执行代码就存储起来,直到退出该应用程序为止,这样在下次运行这部
分代码时,就不需要重新编译了。Microsoft认为这个过程要比一开始就编译整个应用程序代码的效率高得多,
因为任何应用程序的大部分代码实际上并不是在每次运行过程中都执行。使用JIT编译器,从来都不会编译这
种代码从来都不会被编译。
这解释了为什么托管IL代码的执行几乎和内部机器代码的执行速度一样快,但是并没有说明为什么
Microsoft认为这会提高性能。其原因是编译过程的最后一部分是在运行时进行的,JIT编译器确切地知道程序运
行在什么类型的处理器上,利用该处理器提供的任何特性或特定的机器代码指令来优化最后的可执行代码。
传统的编译器会优化代码,但它们的优化过程是独立于代码所运行的特定处理器的。这是因为传统的编译
器是在发布软件之前编译为内部机器可执行的代码。即编译器不知道代码所运行的处理器类型,例如该处理器
是x86兼容处理器或Alpha处理器,这超出了基本操作的范围。例如VisualStudio6优化了一台一般的Pentium
机器,所以它生成的代码就不能利用PentiumIII处理器的硬件特性。相反,JIT编译器不仅可以进行VisualStudio
6所能完成的优化工作,还可以优化代码所运行的特定处理器。
Traditionalcompilerswilloptimizethecode,buttheycanonlyperformoptimizationsthatare
becausetraditionalcompilers
ansthatthecompilerdoesn’tknow
whattypeofprocessorthecodewillrunonbeyondbasicgeneralities,suchasthatitwillbean
Studio6,forexample,optimizesforageneric
Pentiummachine,
sothecodethatitgener
theotherhand,theJITcompilercandoalltheoptimizationsthatVisualStudio6can,andinadditionit
--.
--考试资料
willoptimizefortheparticularprocessorthecodeisrunningon.
Languageinteroperability
TheuseofILnotonlyenablesplatformindependence;italsofacilitateslanguageinteroperability.
Simplyput,youcancompiletoILfromonelanguage,andthiscompiledcodeshouldthenbeinteroperable
withcodethathasbeencompiledtoILfromanotherlanguage.
You’reprobablynowwonderingwhichlanguagesasidefromC#,solet’s
.
undergoneacompleterevampfromVisualBasic6tobringitup-to-date
thatVisualBasichasevolvedoverthelastfewyearsmeansthatinitspreviousversion,
VisualBasic6,mple,itisheavily
integratedintoCOMandworksbyexposingonlyeventhandlersassourcecodetothedeveloper—mostof
ythat,itdoesnotsupportimplementation
inheritance,Basic6was
,andthechangesthatweremadetothelanguagearesoextensiveyoumight
ngVisualBasic6codedoesnotcompileas
uiresextensive
r,()candomostofthe
,itwillupgradethe
projectforyou,whic
ghthismeansthattheworkinvolvedforyouisheavilycutdown,youwillneedto
etomakesurethattheprojectstillworksasintendedbecause
--.
--考试资料
theconversionmightnotbeperfect.
Onesideeffectofth
pilesonlytoIL,justasC#eedto
continuecodinginVisualBasic6,youmaydoso,buttheexecutablecodeproducedwillcompletelyignore
mework,andyou’llneedtokeepVisualStudio6installedifyouwanttocontinuetoworkinthis
developerenvironment.
VisualC++.NET
VisualC++sual
C++.NET,ansthatexistingC++
sourcecodmeans,
however,antyourC++codetorunwithin
mework,thenyoucansimplyaddthefollowinglinetothebeginningofyourcode:
#using
Youcanalsopasstheflag/clrtothecompiler,whichthenassumesthatyouwanttocompiletomanaged
code,erestingthingaboutC++is
thatwhenyoucompiletomanagedcode,thecompilercanemitILthatcontainsanembeddednative
ansthatyoucanmixmanagedtypesandunmanagedtypesinyourC++
themanagedC++code:
classMyClass
{
definesaplainC++class,whereasthecode:
__gcclassMyClass
--.
--考试资料
{
willgiveyouamanagedclass,justasifyou’dwrittentheclassinC#antage
ofusingmanagedC++overC#codeisthatwecancallunmanagedC++classesfrommanagedC++
codewithouthavingtoresorttoCOMinterop.
Thecompanaged
types(forexample,templatesormultipleinheritanceofclasses).Youwillalsofindthatyouwillneedto
usenonstandardC++features(suchasthe__gckeywordshowninthepreviouscode)whenusing
managedclasses.
BecauseofthefreedomthatC++allowsintermsoflow-levelpointermanipulationandsoon,theC++
compilerisnotabletogeneratecodethatwillpasstheCLR’’simportant
thatyourcodeisrecognizedbytheCLRasmemorytypesafe,thenyou’llneedtowriteyoursourcecode
insomeotherlanguage(suchasC#).
VisualJ#.NET
ThelatestlanguagetobeaddedtothemixisVisualJ#.mework1.1,userswere
abletouseJ#J#
eofthis,J#usersareabletotakeadvantageofalltheusualfeaturesofVisualStudio
.oftexpectsthatmostJ++userswillfinditeasiesttouseJ#.
InsteadofbeingtargetedattheJavaruntimelibraries,J#usesthesamebaseclasslibrariesthattherest
ansthatyoucanuseJ#
applications,
WindowsForms,XMLWebservices,andeverythingelsethatispossible—justasC#andVisual
.
--.
--考试资料
Scriptinglanguages
Scriptinglanguagesarestillaround,although,ingeneral,theirimportanceislikelytodeclinewiththe
t,ontheotherhand,
,compiledratherthananinterpretedlanguage,andwrite
reisnoreasontousescriptinglanguagesin
serverside
,however,stillusedasalanguageforMicrosoftOfficeandVisualStudiomacros.
COMandCOM+
Technicallyspeaking,COMandCOM+aren’,becausecomponentsbased
onthemcannotbecompiledintoIL(althoughit’spossibletodosotosomedegreeusingmanagedC++,if
theoriginalCOMcomponentwaswritteninC++).However,COM+remainsanimportanttool,because
,COMcomponentswillstillwork—orporates
COMinteroperabilityfeaturesthatmakeitpossibleformanagedcodetocallupCOMcomponentsand
viceversa(thisisdiscussedinChapter29).Ingeneral,however,youwillprobablyfinditmoreconvenient
ponents,sothatyoucantakeadvantageofthe
.NETbaseclassesaswellastheotherbenefitsofrunningasmanagedcode.
--.
--考试资料
--.
--考试资料
托管代码的优点
Microsoft中间语言与Java字节代码共享一种理念:它们都是一种低级语言,语法很简单,可以非常快速地
转换为机器码。对于代码来说,这种精心设计的通用语法,有很大的优点。
1.平台无关性
首先,这意味着包含字节代码指令的同一个文件可以放在任一个平台中,运行时编译过程的最后阶段可以
很容易完成,这样代码就可以运行在该特定的平台上。也就是说编译为中间语言就可以获得.NET平台无关性,
这与编译为Java字节代码就会得到Java平台无关性是一样的。
2.提高性能
实际上,IL比Java字节代码的作用还要大。IL总是即时编译的(简称JIT),而Java字节代码常常是解释型的,
Java的一个缺点是,在运行应用程序时,把Java字节代码转换为内部可执行代码的过程可可能导致性能的损失。
JIT编译器并不是把整个应用程序一次编译完(这样会有很长的启动时间),而是只编译它调用的那部分代码
(这是其名称由来)。代码编译过一次后,得到的内部可执行代码就存储起来,直到退出该应用程序为止,这样
在下次运行这部分代码时,就不需要重新编译了。Microsoft认为这个过程要比一开始就编译整个应用程序代码
--.
--考试资料
的效率高得多,因为任何应用程序的大部分代码实际上并不是在每次运行过程中都执行。使用JIT编译器,从
来都不会编译这种代码。
这解释了为什么托管IL代码的执行几乎和内部机器代码的执行速度一样快,但是并没有说明为什么
Microsoft认为这会提高性能。其原因是编译过程的最后一部分是在运行时进行的,JIT编译器确切地知道程序运
行在什么类型的处理器上,利用该处理器提供的任何特性或特定的机器代码指令来优化最后的可执行代码。
传统的编译器会优化代码,但它们的优化过程是独立于代码所运行的特定处理器的。这是因为传统的编译
器是在发布软件之前编译为内部机器可执行的代码。即编译器不知道代码所运行的处理器类型,例如该处理器
是x86兼容处理器或Alpha处理器,这超出了基本操作的范围。例如VisualStudio6优化了一台一般的Pentium
机器,所以它生成的代码就不能利用PentiumIII处理器的硬件特性。相反,JIT编译器不仅可以进行VisualStudio
6所能完成的优化工作,还可以优化代码所运行的特定处理器。
3.语言的互操作性
使用IL不仅支持平台无关性,还支持语言的互操作性。简言之,就是能将任何一种语言编译为中间代码,
编译好的代码可以与从其他语言编译过来的代码进行交互操作。
那么除了C#之外,还有什么语言可以通过.NET进行交互操作呢?下面就简要讨论其他常见语言如何与.NET
交互操作。
(1)
VisualBasic6在升级到时,经历了一番脱胎换骨的变化。VisualBasic是在最近的几年中演
化的,其早期版本VisualBasic6并不适合运行.NET程序。例如,它与COM的高度集成,且只把事件处理程序
作为源代码显示给开发人员,大多数后台代码不能用作源代码。另外,它不支持继承,VisualBasic使用的标准
数据类型也与.NET不兼容。
VisualBasic6已经升级为,对VB进行的改变非常大,完全可以把当作是
一种新语言。现有的VB6代码不能编译为代码,把VB6程序转换为时,需要对代码进行大量的
改动,但大多数修改工作都可以由(VS的升级版本,用于与.NET一起使用)自动完成。如果要
把一个VB6项目读取到中,就会升级该项目,也就是说把VB6源代码重写
为源代码。虽然这意味着其中的工作已大大减轻,但用户仍需要检查新的代码,以确保项目仍
可正确工作,因为这种转换并不十分完美。
这种语言升级的一个副作用是不能再把编译为内部可执行代码了。只编译为中间语言,就
像C#一样。如果需要继续使用VB6编写程序,就可以这么做,但生成的可执行代码会完全忽略.NETFramework,
如果继续把VisualStudio作为开发环境,就需要安装VisualStudio6。
(2)VisualC++.NET
VisualC++6有许多Microsoft对Windows的特定扩展。通过VisualC++.NET,又加入了更多的扩展内容,来
支持.NETFramework。现有的C++源代码会继续编译为内部可执行代码,不会有修改,但它会独立于.NET运行库
运行。如果要让C++代码在.NETFramework中运行,就要在代码的开头添加下述命令:
#using
还要把标记/clr传递给编译器,编译器假定要编译托管代码,因此会生成中间语言,而不是内部机器码。
C++的一个有趣的问题是在编译托管代码时,编译器可以生成包含内嵌本机可执行代码的IL。这表示在C++代码
中可以把托管类型和非托管类型合并起来,因此托管C++代码:
classMyClass
{
定义了一个普通的C++类,而代码:
--.
--考试资料
__gcclassMyClass
{
生成了一个托管类,就好像使用C#或编写类一样。实际上,托管C++比C#更优越的一点是可以在
托管C++代码中调用非托管C++类,而不必采用COM交互功能。
如果在托管类型上试图使用.NET不支持的特性(例如,模板或类的多继承),编译器就会出现一个错误。另
外,在使用托管类时,还需要使用非标准的C++特性(例如上述代码中的__gc关键字)。
因为C++允许低级指针操作,C++编译器不能生成可以通过CLR内存类型安全测试的代码。如果CLR把代
码标识为内存类型安全是非常重要的,就需要用其他一些语言编写源代码,例如C#或。
(3)VisualJ
#最新添加的语言是VisualJ#。在.NETFramework1.1版本推出之前,用户必须下载相应的软件,才能使用J#。
现在J#语言内置于.NETFramework中。因此,J#用户可以利用的所有常见特性。Microsoft希望
大多数J++用户认为他们在使用.NET时,将很容易使用J#。J#不使用Java运行库,而是使用与其他.NET兼容语
言一样的基类库。这说明,与C#和一样,可以使用J#创建应用程序、Windows窗体、XML
Web服务和其他应用程序。
(4)脚本语言
脚本语言仍在使用之中,但由于.NET的推出,一般认为它们的重要性在降低。另一方面,JScript升级到
。页面可以用编写,现在可以把当作一种编译语言来运行,而不是
解释性的语言,也可以编写输入量比较大的代码。有了后,就没有必要在服务器端的Web
页面上使用脚本语言了,但VBA仍用作Office文档和VisualStudio宏语言。
(5)COM和COM+
从技术上讲,COM和COM+并不是面向.NET的技术,因为基于它们的组件不能编译为IL(但如果原来的COM
组件是用C++编写的,使用托管C++,在某种程度上可以这么做)。但是,COM+仍然是一个重要的工具,因为其
特性没有在.NET中完全实现。另外,COM组件仍可以使用——.NET组合了COM的互操作性,从而使托管代码
可以调用COM组件,COM组件也可以调用托管代码(见第29章)。在大多数情况中,把新组件编写为.NET组件,
其多数目的是为了更加方便,因为这样可以利用.NET基类和托管代码的其他优点。