Aká je výhoda použitia Java premennými?

Stack Variables

premennými
V tomto článku budeme hovoriť o rôznych spôsoboch, ako deklarovať premenné v Jave a výhody a nevýhody používania lokálnych premenných alebo premennými. V Jave, existujú rozdiely medzi stack premenné a ďalšie typy premenných, ako sú miestne a triedy. Tento článok bude popisovať detaily s príkladom a pomôže vám ju správne vykonávať.

Úvod:
JVM rozdeľuje pamäť do nasledujúcich sekcií:

  • halda: Táto časť obsahuje objekty a tiež referenčné premenné.
  • Stoh: Táto časť obsahuje metódy, lokálne premenné a referenčné premenné.
  • Code: Tento oddiel obsahuje byte kód.
  • Static: Táto časť obsahuje statické premenné a metódy.

v Jave, môžeme deklarovať premenné v nasledujúcich troch spôsobov:

  • premenné inštancie.
  • Úroveň triedy alebo statické premenné.
  • Lokálne alebo premennými.

premenné inštancie: Instance variables indicate that they exist per instance of the class. If a class has ten instances, we will have ten copies of each instance variables. Instance variables are also termed as non static variables.

Class level or Static Variables: Class level or Static variables exist per class no matter how many instance of the class we create. These are declared using the static keyword. Static variables are initialized when the class is first loaded in the JVM.

Local or Stack Variables: Local or stack variables are defined within a method and are local in scope. Their scope varies depending upon the access modifier used to declare them.

Java memory distribution

Java memory distribution

Pic1: Java memory distribution

Let us see the following three java codes

Listing 1: Access to a stack variable:

[Code]

package com.home.variables;

public class StackVariables {

// Access to Stack variables

public void stackAccess(int maxVal) {

int j = 0;

pre (int i = 0; ja < val; i ) {

j = 1;

}

}

}

[/Code]

Listing 2: Access to an instance variable

[Code]

package com.home.variables;

public class InstanceVariables {

private int instVar;

//Access to Class instance variable

public void instanceAccess(int val) {

pre (int i = 0; ja < val; i ) {

instVar = 1;

}

}

}

[/Code]

Listing 3: Access to a static variable

[Code]

package com.home.variables;

public class StaticVariables {

private static int staticVar;

//Class static variable access

public void staticAccess(int val) {

pre (int i = 0; ja < val; i ) {

staticVar = 1;

}

}

}

[/Code]

Each of the above java classes executes a loop for equal number of iterations. The only place where they differ is the type of variable which gets incremented within the for loop. In listing 1, a local stack variable is used and incremented by 1 every time the loop executes. Listing 2 shows an example of using and incrementing a class level or an instance level variable. Listing 3 is an example of using an incrementing a static variable.

Premenné úrovni inštancie a statické premenné trvať takmer rovnaké množstvo času na vykonanie. Že miestne alebo stack premenné sú dvakrát až trikrát rýchlejšie. V prípade miestnych alebo premennými, pretože JVM musí vykonať menšiu úlohu ako pri vstupe statickú alebo triedy úroveň alebo premenných úrovni inštancie. Nasledujúci generovaný byte kód dáva jasne najavo,:

Listing 4: Byte kód vygenerovaný proti Výpis 1 (stack Access)

[Code]

Metóda void stackAccess(int)

0 iconst_0 // push 0 do fronty.

1 istore_2 // Pop 0 a uložiť ju na indexe 2 v tabuľke lokálnych premenných(j).

2 iconst_0 // push 0.

3 istore_3 // Pop 0 a uložiť ju na indexe 3 v tabuľke lokálnych premenných(ja).

4 ísť do 13 //Prejsť na mieste 13.

7 iinc 2 1 //Prírastok j uložená na indexe 2 by 1.

10 iinc 3 1 //Veľkosť kroku aj skladované na indexe 3 by 1.

13 iload_3 // prenesie hodnotu na indexe 3(ja).

14 iload_1 // prenesie hodnotu na indexe 1(val).

15 if_icmplt 7 //Pop i and val. Prejsť na mieste 7 if i is less than val.

18 return //Return to calling method.

[/Code]

Listing 5: Byte kód vygenerovaný proti Výpis 2 (Instance or Class level Access)

[Code]

Method void instanceAccess(int)

0 iconst_0 // push 0 do fronty.

1 istore_2 // Pop 0 a uložiť ju na indexe 2 v tabuľke lokálnych premenných(ja).

2 ísť do 18 //Prejsť na mieste 18.

5 aload_0 //Push index 0(this).

6 dup //Duplicate the top stack value and push it.

7 getfield #19 <Field int instVar>

//Pop the object reference for this and push the value for instVar.

10 iconst_1 //Push 1.

11 iadd //Pop the top two values, push their sum.

12 putfield #19 <Field int instVar>

//Pop the top two values and store the sum in instVar.

15 iinc 2 1 //Veľkosť kroku aj skladované na indexe 2 by 1.

18 iload_2 //Push the value at index 2(ja).

19 iload_1 // prenesie hodnotu na indexe 1(val).

20 if_icmplt 5 //Pop i and val. Prejsť na mieste 5 if i is less than val.

23 return //Return to calling method.

[/Code]

Listing 6: Byte kód vygenerovaný proti Výpis 6 (Static Access)

[Code]

Method void staticAccess(int)

0 iconst_0 // push 0 do fronty.

1 istore_2 // Pop 0 a uložiť ju na indexe 2 v tabuľke lokálnych premenných(ja).

2 ísť do 16 //Prejsť na mieste 16.

5 getstatic #25 <Field int staticVar>

//Push the value from the constant pool for staticVar.

8 iconst_1 //Push 1.

9 iadd //Pop the top two values, push their sum.

10 putstatic #25 <Field int staticVar>

//Pop the value for sum and store it in staticVar.

13 iinc 2 1 //Veľkosť kroku aj skladované na indexe 2 by 1.

16 iload_2 //Push the value at index 2(ja).

17 iload_1 // prenesie hodnotu na indexe 1(val).

18 if_icmplt 5 //Pop i and val. Prejsť na mieste 5 if i is less than val.

21 return //Return to calling method.

[/Code]

Vyššie uvedený generovaný byte kód z týchto troch prístupov dáva jasný obraz, ktorý pomocou premenných Stack úrovne alebo lokálnej premennej je účinný spôsob, ako používať premenné v Jave. JVM je zásobník umiestnený a je preto viac optimalizovaný na použitie dát na úrovni stack. Lokálne premenné sú uložené v lokálnej premennej tabuľky na operandu zásobníka Java a sú dostupné veľmi ľahko a efektívne. To sa stáva nákladnú záležitosť používať a manipulovať s statické alebo premenné úrovni inštancie, pretože JVM musí používať drahé operačný kód pre prístup k tieto premenné z konštánt.

Kód uvedené v zozname 2 and listing 3 môže byť reštrukturalizovať nasledujúcim spôsobom, aby plnili úlohy účinne.

Listing 7: Reštrukturalizovať spôsob prístupu premennú inštancie

[Code]

package com.home.variables;

public class InstanceVariables {

private int instVar;

//Access to Class instance variable

public void instanceAccess(int val) {

int j = instVar;

pre (int i = 0; ja < val; i ) {

j = 1;

}

instVar = j;

}

}

[/Code]

Listing 8: Restructured way to access a static variable

[Code]

package com.home.variables;

public class InstanceVariables {

private int staticVar;

//Access to Class instance variable

public void instanceAccess(int val) {

int j = staticVar;

pre (int i = 0; ja < val; i ) {

j = 1;

}

staticVar = j;

}

}

[/Code]

As we see the methods instanceAccess and staticAccess are modified to copy their instance or static variables into a local stack variable. The operation or the manipulation is performed on the copied local stack variable and when the manipulation of a variable is over, the value is copied back to the instance or static variable. This simple change significantly improves the performance of instanceAccess and staticAccess. With this change the execution time of all these three methods are now effectively same.

This approach does not advocates the fact that we should avoid using static or instance variables. Rather we should use whatever mechanism makes sense and is more efficient for our design. e.g. in the above example, if we need to access a access a static or an instance variable in a loop, we can significantly improve the performance of the code by temporarily storing them in a local stack variable. This provides a more efficient sequence of byte code instructions for the JVM to execute.

Summary: To conclude the discussion, we have summarized the points below. We need to understand the concept of java variables and their scope. And the most important part is the implementation of these variables in correct place. Hope this article will help you to understand the details and use it in your application.

  • JVM divides the memory into following sections :halda, Stoh, Code and Static
  • Java allows us to declare variables in the following three ways:
    • Instance
    • Static
    • Local or stack.
  • Použitie premenných na úrovni Stack urýchľuje implementáciu programu o dva až tri krát.
  • Ak majú použiť statické alebo premenné úrovni inštancie, by sme mali skopírovať do lokálnej premennej pred manipuláciu s dátami alebo robiť nejakú operáciu.

 

 

 

 

 

Štítky na: ,
============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share