[ Pobierz całość w formacie PDF ]
.Because Oolong is closer to the JVM class file format than Java, we use slashes instead of periods.The.class and.super directives in this example are equivalent to the Javafile:///D|/Convert/tmp/0201309726_ch02lev1sec1.html (1 of 4) [10/20/2003 12:24:29 AM]file:///D|/Convert/tmp/0201309726_ch02lev1sec1.htmlpublic class hello extends java.lang.ObjectIn Oolong, you must use the full name of the class.In Java, this line is equivalent topublic class hello extends Objectbecause Java assumes that by Object you mean the particular Object found in the package java.lang.Theclass file does not make this assumption, so you must write out the full name of every class.That can get tiring,but it ensures that the class file is not ambiguous.This saves the JVM time trying to figure out which class youmean, and it ensures that you get the same result each time, even if new classes are added to the system.2.1.2 main MethodThe.method directive marks the beginning of a new method.Every line after that is a part of the method untilthe.end method directive.The.method directive here is.method public static main([Ljava/lang/String;)VThe.method directive names the method being created.In this case, the method is named main.Before the method name is a list of access keywords, which control how the class can be used.This method ismarked public, meaning that it can be used by anybody, and static, meaning that it isn't attached to anyparticular instance of the class.Unlike Java, the access keywords may come in any order.Also unlike Java, the return type of the method does not appear at the end of the list of keywords.Instead, thearguments and return types are written together in the descriptor following the method name.The descriptor is away of expressing a type as letters and symbols.A method descriptor contains the types of the argumentsbetween parentheses, followed by the return type.Most types are represented by a single character: V for void, I for int, and so on.A left bracket ([) means anarray of whatever type follows.L means an object of the type named by everything up to the next semicolon.Thus, Ljava/lang/String; is the type written as java.lang.String in Java, and [Ljava/lang/String; isan array of java/lang/String.The complete list can be found in section 2.5.The descriptor of main is ([Ljava/lang/String;)V.This says that main takes an array of strings, and returnsa void.The.limit directives tell how much space to allocate for the execution of this method:.limit stack puts anupper limit on how many slots on the operand stack the program will use;.limit locals specifies the numberof local variable slots that will be used.The Oolong assembler will guess if the directive isn't given.2.1.3 InstructionsThe remaining lines up to.end method represent JVM instructions.The Oolong assembler translates thefile:///D|/Convert/tmp/0201309726_ch02lev1sec1.html (2 of 4) [10/20/2003 12:24:29 AM]file:///D|/Convert/tmp/0201309726_ch02lev1sec1.htmlinstructions into bytecodes.The first word of each instruction is called a mnemonic.Each mnemonic correspondsto an opcode in the class file, which is a single byte representing an instruction.It's called a mnemonic becauseit's supposed to be easier to remember than the actual opcode for the instruction.The mnemonic may be followedby several arguments.The allowable arguments depend on the mnemonic.They provide more detail about howthe instruction should operate.Let's look at what the instructions mean, one by one.The first instruction isgetstatic java/lang/System/out Ljava/io/PrintStream;This instruction tells the JVM to get the value of the field out from the class java/lang/System.This is a staticfield, meaning that it is a property of the class as a whole instead of any individual object.The value of the objectobtained from this operation is expected to be a java/io/PrintStream object.A reference to this object isplaced on the stack.The second instruction isldc "Hello, world"This causes the JVM to create a java/lang/String object with the value Hello, world.This object is placedon the stack above the out object.The stack now looks like this:The next instruction isinvokevirtual java/io/PrintStream/println(Ljava/lang/String;)VThe invokevirtual instruction is used to invoke a method on an object.The arguments to the instruction namethe method to be called (println), the class in which the method is to be found (java/io/PrintStream), andthe descriptor of the method ((Ljava/lang/String;)V).The JVM checks that the arguments that actually appear on the stack correspond to those expected by themethod [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl matkasanepid.xlx.pl
.Because Oolong is closer to the JVM class file format than Java, we use slashes instead of periods.The.class and.super directives in this example are equivalent to the Javafile:///D|/Convert/tmp/0201309726_ch02lev1sec1.html (1 of 4) [10/20/2003 12:24:29 AM]file:///D|/Convert/tmp/0201309726_ch02lev1sec1.htmlpublic class hello extends java.lang.ObjectIn Oolong, you must use the full name of the class.In Java, this line is equivalent topublic class hello extends Objectbecause Java assumes that by Object you mean the particular Object found in the package java.lang.Theclass file does not make this assumption, so you must write out the full name of every class.That can get tiring,but it ensures that the class file is not ambiguous.This saves the JVM time trying to figure out which class youmean, and it ensures that you get the same result each time, even if new classes are added to the system.2.1.2 main MethodThe.method directive marks the beginning of a new method.Every line after that is a part of the method untilthe.end method directive.The.method directive here is.method public static main([Ljava/lang/String;)VThe.method directive names the method being created.In this case, the method is named main.Before the method name is a list of access keywords, which control how the class can be used.This method ismarked public, meaning that it can be used by anybody, and static, meaning that it isn't attached to anyparticular instance of the class.Unlike Java, the access keywords may come in any order.Also unlike Java, the return type of the method does not appear at the end of the list of keywords.Instead, thearguments and return types are written together in the descriptor following the method name.The descriptor is away of expressing a type as letters and symbols.A method descriptor contains the types of the argumentsbetween parentheses, followed by the return type.Most types are represented by a single character: V for void, I for int, and so on.A left bracket ([) means anarray of whatever type follows.L means an object of the type named by everything up to the next semicolon.Thus, Ljava/lang/String; is the type written as java.lang.String in Java, and [Ljava/lang/String; isan array of java/lang/String.The complete list can be found in section 2.5.The descriptor of main is ([Ljava/lang/String;)V.This says that main takes an array of strings, and returnsa void.The.limit directives tell how much space to allocate for the execution of this method:.limit stack puts anupper limit on how many slots on the operand stack the program will use;.limit locals specifies the numberof local variable slots that will be used.The Oolong assembler will guess if the directive isn't given.2.1.3 InstructionsThe remaining lines up to.end method represent JVM instructions.The Oolong assembler translates thefile:///D|/Convert/tmp/0201309726_ch02lev1sec1.html (2 of 4) [10/20/2003 12:24:29 AM]file:///D|/Convert/tmp/0201309726_ch02lev1sec1.htmlinstructions into bytecodes.The first word of each instruction is called a mnemonic.Each mnemonic correspondsto an opcode in the class file, which is a single byte representing an instruction.It's called a mnemonic becauseit's supposed to be easier to remember than the actual opcode for the instruction.The mnemonic may be followedby several arguments.The allowable arguments depend on the mnemonic.They provide more detail about howthe instruction should operate.Let's look at what the instructions mean, one by one.The first instruction isgetstatic java/lang/System/out Ljava/io/PrintStream;This instruction tells the JVM to get the value of the field out from the class java/lang/System.This is a staticfield, meaning that it is a property of the class as a whole instead of any individual object.The value of the objectobtained from this operation is expected to be a java/io/PrintStream object.A reference to this object isplaced on the stack.The second instruction isldc "Hello, world"This causes the JVM to create a java/lang/String object with the value Hello, world.This object is placedon the stack above the out object.The stack now looks like this:The next instruction isinvokevirtual java/io/PrintStream/println(Ljava/lang/String;)VThe invokevirtual instruction is used to invoke a method on an object.The arguments to the instruction namethe method to be called (println), the class in which the method is to be found (java/io/PrintStream), andthe descriptor of the method ((Ljava/lang/String;)V).The JVM checks that the arguments that actually appear on the stack correspond to those expected by themethod [ Pobierz całość w formacie PDF ]