Static , Heap, Stack

Dec 28, 2023
Static , Heap, Stack

๐Ÿ’ก
.class ํŒŒ์ผ์„ ๋“ค๊ณ  ์™€์„œ ์‹คํ–‰ํ• ๋•Œ
  1. static ์ฐพ์Œ
  1. main ์ฐพ์•„ ์‹คํ–‰
 

๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉ โ†’ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

 
๋ฉ”๋ชจ๋ฆฌ : ์ €์žฅ ์˜์—ญ, ์˜์—ญ์— ๋Œ€ํ•œ ์ฃผ์†Œ(๋ฒˆ์ง€)๊ฐ€ ์žˆ์Œ
 
๋…ผ๋ฆฌ์ ์œผ๋กœ ์˜์—ญ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉํ•จ
 

Static : ์‹œ์ž‘์ „๋ถ€ํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์— ๋„์›€


package ex04; /** * JVM์ด ์‹œ์ž‘ํ• ๋•Œ * 1. static ์ฐพ์Œ -> age, main * 2. main ์ฐพ์•„์„œ ์‹คํ–‰ */ class Person1{ static int age = 20; char gender = '์—ฌ'; // ์›์‹œ ์ž๋ฃŒํ˜•(primitive data type) // ์ปค์Šคํ…€ ์ž๋ฃŒํ˜•(๋‚ด๊ฐ€ ๋งŒ๋“  ์ž๋ฃŒํ˜•) } public class MemEx01 { public static void main(String[] args) { System.out.println(Person1.age); } }
notion image
๐Ÿ’ก
ํŠน์ง• : ์ •์  ํ• ๋‹น
main์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ๋ฉ”๋ชจ๋ฆฌ์— ๋œธ
์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ๊ธบ
ํด๋ž˜์Šค๋ช….๋ณ€์ˆ˜๋ช… : static ๊ณต๊ฐ„์€ ์–ด๋””์„œ๋‚˜ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ
ํด๋ž˜์Šค์— ์žˆ์œผ๋ฉด ํด๋ž˜์Šค๋ช…์„ ์ƒ๋žต ๊ฐ€๋Šฅํ•จ
๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•จ

Heap : ๋™์ ์œผ๋กœ ์›ํ•  ๋•Œ ๋„์›€


package ex04; class Person2{ int age = 10; char gender = '๋‚จ'; } public class MemEx02 { public static void main(String[] args) { System.out.println("1๋…„ ์ง€๋‚จ"); System.out.println("2๋…„ ์ง€๋‚จ"); System.out.println("3๋…„ ์ง€๋‚จ"); Person2 p2 = new Person2(); // ํƒ€์ž…๋ช… + ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค๋ช…; System.out.println(p2.age); System.out.println(p2.gender); System.out.println("๋ฉ”์ธ ์ข…๋ฃŒ"); } }
notion image
notion image
์•ก์„ธ์Šค :
ํŠน์ง• : ๋™์  ํ• ๋‹น
๋‚ด๊ฐ€ ์›ํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ์— ๋„์›€
static ๋ณด๋‹ค ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์งง์Œ
 
heap ์ฐพ๋Š” ๋ฐฉ๋ฒ•
์—ฌ๋Ÿฌ๋ฒˆ ๋„์šธ ์ˆ˜ ์žˆ์Œ > ๊ฐ€๋ฅดํ‚ค๋Š” ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•จ
ํƒ€์ž…๋ช… + ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค๋ช…(); // ํด๋ž˜์Šค ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋„์›€

Stack : ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๋„์›€


package ex04; public class ScopeEx01 { int n1 = 1; static int n2 =2; static void m1(){ int n1 = 10; System.out.println("m1 : " + n1); // n1 = 10 } void m2(){ System.out.println("m2 : " + n1); // n1 = 1 } //main ์‹œ์ž‘์ „์— static์— n2 ๋ณ€์ˆ˜์™€ n1 ๋ฉ”์„œ๋“œ ์ด๋ฆ„๋งŒ ๋กœ๋“œ๋˜์–ด ์žˆ์Œ public static void main(String[] args) { System.out.println("1"); m1(); System.out.println("2"); } }
 
๐Ÿ’ก
Stack : ์ž„์‹œ ์ €์žฅ์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง
Queue : ์ˆœ์„œ๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง
(๋ฐ์ดํ„ฐ ์ €์žฅx)
Scope : ํ•ด๋‹น ๋ณ€์ˆ˜์˜ ์‚ฌ์šฉ ๋ฒ”์œ„
 
๐Ÿ’ก
์„œ๋กœ์˜ stack์— ์ ‘๊ทผ์ด ์•ˆ๋จ
์ž ๊น ๋–ด๋‹ค ์‚ฌ๋ผ์ง€๋‹ˆ๊นŒ ์™ธ๋ถ€์—์„œ ์ฐพ์„ ์ˆ˜ ์—†์Œ
return์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์•„์•ผ ํ•จ/main ๋ณ€์ˆ˜๊ฐ€ ๋จ
 
๐Ÿ’ก
์ƒ๋ช…์ฃผ๊ธฐ : static < heap< statick
๐Ÿ’ก
1. ํด๋ž˜์Šค ๋กœ๋“œ(๋ฉ”๋ชจ๋ฆฌ์— ํด๋ž˜์Šค๊ฐ€ ๋œธ)
  1. static ์ฐพ๊ธฐ
ํด๋ž˜์Šค ๋ณ„๋กœ ์˜์—ญ์ด ๋‚˜๋‰จ
์›๋ž˜๋Š” ํ”„๋กœ์ ํŠธ ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  static์ด ๋œธ
  1. main์„ ์ฐพ์•„ ์‹คํ–‰
main์˜ ๊ทผ๊ฑฐ
static : ์‹คํ–‰์ „์— ๋ฉ”๋ชจ๋ฆฌ์— ๋–  ์žˆ์Œ
public : ์™ธ๋ถ€์— ์žˆ๋Š” JVM์ด ์ ‘๊ทผ ๊ฐ€๋Šฅ
  1. main๋ฉ”์„œ๋“œ๊ฐ€ ์—ด๋ฆฌ๋ฉด main Queue๊ฐ€ ์‹คํ–‰
put : ๋ผ์ธ๋“ค์ด ๋‹ด๊น€
pop : ๋๋‚˜์„œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ CPU๋กœ ๊ฐ€์„œ ๋ชจ๋‹ˆํ„ฐ์— ์ถœ๋ ฅ๋จ
notion image
๐Ÿ’ก
์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์ด ๋‹ค๋ฆ„
 
package ex04; public class ScopeEx01 { int n1 = 1; static int n2 =2; static void m1(){ int n1 = 10; System.out.println("m1 : " + n1); // n1 = 10 } void m2(){ System.out.println("m2 : " + n1); // n1 = 1 } //main ์‹œ์ž‘์ „์— static์— n2 ๋ณ€์ˆ˜์™€ n1 ๋ฉ”์„œ๋“œ ์ด๋ฆ„๋งŒ ๋กœ๋“œ๋˜์–ด ์žˆ์Œ public static void main(String[] args) { System.out.println("1"); m1(); System.out.println("2"); ScopeEx01 sc = new ScopeEx01(); sc.m2(); } }
notion image
 
๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ๋ณ€์ˆ˜ : ์ง€์—ญ๋ณ€์ˆ˜ / ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ์งง์Œ
๋ฉ”์„œ๋“œ๋Š” ์ด๋ฆ„๋งŒ ๋œธ
์ฐธ์กฐ๋ณ€์ˆ˜ : ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ / *๋ฅผ ๋ถ™์ž„
heap์ด๋ž‘ static์€ ๋‹ค ์ฐพ์„ ์ˆ˜ ์žˆ์Œ

๊ฐ™์€ scope์— ์žˆ๋Š” data ์ •๋ฆฌ


static (n2 =2, m1(), main)
์–ด๋””์—์„œ๋“ ์ง€ ์ ‘๊ทผ ๊ฐ€๋Šฅ
heap ( n1=1, m2())
static์ด ์•ˆ ์ ํžŒ ๋ชจ๋“  ๊ฒƒ
heap๋ผ๋ฆฌ๋Š” ๊ฐ™์€ scope์—ฌ์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰์€ heap
stack (n5=10)
๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€๊ฐ€ ์ €์žฅ๋งŒ ๋จ
์™ธ๋ถ€ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ
๐Ÿ’ก
๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ์ฐพ์•„ ์‹คํ–‰ํ•˜๋Š” ์œ„์น˜๊ฐ€ ๋‹ค๋ฆ„
Share article

vosw1