ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA μžλ°” μ‹λ³„μžμ™€ 데이터 ν˜• λ³€ν™˜
    JAVA 2020. 7. 28. 19:23

    🎯 μžλ°” μ‹λ³„μž, 데이터 ν˜• λ³€ν™˜, λ³€μˆ˜ μ’…λ₯˜λ₯Ό μ•Œμ•„λ³Έλ‹€.

     


    πŸ”– λͺ©μ°¨

    μ‹λ³„μž
    λ°μ΄ν„°ν˜•
    λ³€μˆ˜μ™€ μƒμˆ˜
    데이터 ν˜• λ³€ν™˜
    λ³€μˆ˜μ™€ μ½”λ“œλΈ”λŸ­
    λ³€μˆ˜μ’…λ₯˜


     

     

    μ‹λ³„μž(Identifier)

    μ‹λ³„μžλŠ” μžλ°”μ½”λ“œ λ‚΄μ—μ„œ μ‚¬μš©λ˜λŠ” 각각의 단어λ₯Ό μ˜λ―Έν•œλ‹€. 클래슀λͺ…, λ³€μˆ˜λͺ…, λ©”μ†Œλ“œλͺ… 등이 ν•΄λ‹Ήλœλ‹€.

    첫 λ¬ΈμžλŠ” λ°˜λ“œμ‹œ 영문자둜 μ¨μ•Όν•˜λ©° μˆ«μžκ°€ 포함될 수 μžˆλ‹€. 특수문자 쀑 , _ 와 $λŠ” 첫 문자둜 μ‚¬μš©ν•  수 μžˆμ§€λ§Œ 가급적 영문으둜 μž‘μ„±ν•˜λŠ” 게 μ’‹λ‹€. μžλ°”λŠ” λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•œλ‹€.

     

    μ‹λ³„μž μ’…λ₯˜

    μ‹œμŠ€ν…œ μ •μ˜ μ‹λ³„μž : μžλ°”μ‹œμŠ€ν…œμ΄ λ¨Όμ € μ •μ˜ν•œ μ‹λ³„μž. μ˜ˆμ•½μ–΄, ν‚€μ›Œλ“œλΌκ³  λΆ€λ₯Έλ‹€.

    μ‚¬μš©μž μ •μ˜ μ‹λ³„μž : κ°œλ°œμžκ°€ ν•„μš”μ— μ˜ν•΄ μ •μ˜ν•œ μ‹λ³„μž. 클래슀λͺ…, λ³€μˆ˜λͺ…, λ©”μ†Œλ“œλͺ… λ“±

     

    μ‹λ³„μž 지정 κ·œμΉ™

    클래슀 : 첫 λ¬ΈμžλŠ” 항상 λŒ€λ¬Έμžλ‘œ ν‘œν˜„. ν•˜λ‚˜ μ΄μƒμ˜ 단어가 ν•©μ³μ§ˆ 땐 Camel ν‘œκΈ°λ²•μœΌλ‘œ μ²«λ¬ΈμžλŠ” λŒ€λ¬Έμžλ‘œ 이어주기. μ˜λ―ΈμžˆλŠ” λͺ…μ‚¬ν˜•μœΌλ‘œ μž‘μ„±ν•œλ‹€. class JavaTest

    λ³€μˆ˜μ™€ λ©”μ†Œλ“œ : 첫 λ¬ΈμžλŠ” 항상 μ†Œλ¬Έμžλ‘œ ν‘œν˜„. λ³€μˆ˜λŠ” μ˜λ―ΈμžˆλŠ” λͺ…μ‚¬ν˜•, λ©”μ†Œλ“œλŠ” μ˜λ―ΈμžˆλŠ” λ™μ‚¬ν˜•μœΌλ‘œ μ§€μ •ν•œλ‹€. 이 λ³€μˆ˜κ°€ 뭐에 μ“°λŠ”κ±΄μ§€ λ”± μ•Œμ•„λ³Ό 수 있게 μ¨μ£ΌλŠ” 게 μ’‹λ‹€.

    String itLand;
    public void getTest()

    μƒμˆ˜ : λ³€μˆ˜μ™€ 달리 값을 지정해두면 μ ˆλŒ€ 값을 λ°”κΏ€ 수 μ—†λ‹€. λͺ¨λ“  문자λ₯Ό λŒ€λ¬Έμžλ‘œ ν‘œν˜„ν•œλ‹€. ν•˜λ‚˜ μ΄μƒμ˜ 단어가 ν•©μ³μ§ˆ λ•Œ 곡백 ν•„μš” μ‹œ under score(_) λ₯Ό μ‚¬μš©ν•˜μ—¬ μ—°κ²°ν•œλ‹€.

    int javaTest= 10;
    final int JAVA_TEST = 20;

     

     

     

    데이터 ν˜•

    μžλ°” μ–Έμ–΄κ°€ μ²˜λ¦¬ν•  수 μžˆλŠ” λ°μ΄ν„°ν˜• 2가지 νƒ€μž…

     

    κΈ°λ³Έν˜•μ€ μˆ˜μΉ˜ν˜•, λ¬Έμžν˜•, λ…Όλ¦¬ν˜• 데이터 νƒ€μž…μœΌλ‘œ κ΅¬μ„±λ˜μ–΄ 있으며 μ§€μ •λœ 크기의 데이터값을 κ°–λŠ”λ‹€.

    μ°Έμ‘°ν˜•μ€ κΈ°λ³Έ λ°μ΄ν„°ν˜•μ„ μ œμ™Έν•œ λ‚˜λ¨Έμ§€ λͺ¨λ“  λ°μ΄ν„°ν˜•μ„ 가리킀며 클래슀, λ°°μ—΄, μΈν„°νŽ˜μ΄μŠ€ 등이 μžˆλ‹€. μ‹€μ œ λ‚΄μš©μ„ κ°–κ³ μžˆλŠ” 게 μ•„λ‹ˆλΌ λ‚΄μš©μ΄ μžˆλŠ” 곳의 μœ„μΉ˜λ₯Ό 가지고 μ°Έμ‘°ν•œλ‹€.

     

     

    κΈ°λ³Έ λ°μ΄ν„°ν˜•

     

    μ •μˆ˜ν˜•

    ν•™μƒμ˜ ν‚€λ₯Ό λ°μ΄ν„°λ‘œ 담고싢을 경우, 1byte(ν‘œν˜„ λ²”μœ„ : -128 ~ 127)λ₯Ό μ‚¬μš©ν•˜λ©΄ λ„ˆλ¬΄ μž‘μœΌλ‹ˆκΉŒ shortλ₯Ό μ‚¬μš©ν•˜λŠ” 게 μ ν•©ν•˜λ‹€. μ •μˆ˜ν˜• 쀑 κ°€μž₯ 기본이 int이닀. μ •μˆ˜λ₯Ό 담을 수 μžˆλŠ” κ°€μž₯ 큰 ν‚€μ›Œλ“œκ°€ long이며 둱을 μ‚¬μš©ν•  경우 int와 κ΅¬λ³„ν•˜κΈ° μœ„ν•΄ μ •μˆ˜κ°’ 뒀에 L을 ν‘œκΈ°ν•œλ‹€. μ˜›λ‚ μ—” μ΅œλŒ€ν•œ λ©”λͺ¨λ¦¬λ₯Ό μž‘κ²Œ μ°¨μ§€ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ λ°μ΄ν„°ν¬κΈ°λ§Œ 정해놓고 μ“Έ λͺ©μ μœΌλ‘œ μ΄λ ‡κ²Œ λ„€κ°€μ§€λ‘œ λΆ„λ₯˜ν–ˆλ‹€.

     

    μ‹€μˆ˜ν˜•

    기본은 double 8λ°”μ΄νŠΈμΈλ° κ·Έλ ‡κ²ŒκΉŒμ§€ 큰 곡간이 ν•„μš”μ—†λ‹€λ©΄ floatλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€. ν”Œλ‘œνŠΈλŠ” μ‹€μˆ˜ 뒀에 fλ₯Ό λΆ™μ—¬μ€˜μ•Ό ν•œλ‹€.

     

    λ…Όλ¦¬ν˜•

    μžλ°”μ—μ„œλŠ” μ†Œλ¬Έμž true/false 둜 참거짓을 ν‘œμ‹œν•œλ‹€.

     

    λ¬Έμžν˜•

    문자 ν•œ κΈ€μžλŠ” char ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 16λΉ„νŠΈ μœ λ‹ˆμ½”λ“œ 값을 ν‘œν˜„ν•˜λ©°, μœ λ‹ˆμ½”λ“œλž€ 컴퓨터가 세계 각ꡭ의 μ–Έμ–΄λ₯Ό ν†΅μΌλœ λ°©λ²•μœΌλ‘œ ν‘œν˜„ν•  수 있게 λ§Œλ“  문자 ν‘œν˜„ 방법이닀.

    int num = 'A' + 1;  // 66

    'A'의 μ•„μŠ€ν‚€ μ½”λ“œκ°’μ€ 65이닀.

    public class sample01_variable3_char {
    
        public static void main(String[] args) {
    
            //charλŠ” μˆ˜μΉ˜ν˜•μ΄λ‹€.
            char c = 'A'; // 65(μ•„μŠ€ν‚€μ½”λ“œκ°’)
            char c1 = 'a'; // 97
            char c2='1'; //49
    
            System.out.println(c+1);
            System.out.println(c1+1);
            System.out.println(c2+1);
            System.out.println(c2+2);
    
        }
    
    }

    λ¬Έμžμ—΄(String)은 κΈ°λ³Έν˜•μ΄ μ•„λ‹ˆλ‹€. μ°Έμ‘°ν˜•μ΄λ‹€.

     

    λ¬Έμžν˜• λ¦¬ν„°λŸ΄ - escape 문자

    λ¬Έμžμ—΄ λ‚΄μ—μ„œ 특수문자λ₯Ό ν‘œν˜„ν•˜κ±°λ‚˜ 특수기λŠ₯ μ‚¬μš©ν•  λ•Œ μ‚¬μš©ν•œλ‹€. (\t, \n, \r, \', \", \\)

            System.out.println("aaa\tbbb"); //μˆ˜ν‰ νƒ­
            System.out.println("aaa\nbbb"); //μ€„λ°”κΏˆ
            System.out.println("aaa\'bbb'"); //μŠ€νŠΈλ§μ•ˆμ—μ„œ λ”°μ˜΄ν‘œ μ‚¬μš©
            System.out.println("aaa\"bbb"); //μŠ€νŠΈλ§μ•ˆμ—μ„œ ν°λ”°μ˜΄ν‘œ
            System.out.println("aaa\\bbb"); //λ°±μŠ¬λž˜μ‰¬ 특수문자

     

     

    λ°μ΄ν„°νƒ€μž… μ—°μŠ΅ν•΄λ³΄κΈ°

    public class Sample01_DataType {
    
        public static void main(String[] args) {
    
            System.out.println("\uD64D"); // λ¬Έμžμ—΄"", "홍"의 μœ λ‹ˆμ½”λ“œ ν˜•μ‹μ΄λ‹€
            System.out.println("홍");
            System.out.println(100); //μ •μˆ˜
            System.out.println(3.14); // μ‹€μˆ˜, 3.14D와 동일
            System.out.println(3.14F); // μ‹€μˆ˜, f와 동일
            System.out.println(3.14D); // μ‹€μˆ˜, d와 동일
            System.out.println('\ud64d'); //문자 ''
    
            System.out.println(null); // 컴파일 μ—λŸ¬
        }
    
    }

     

     

     

    λ³€μˆ˜ ( variables )

    단 ν•˜λ‚˜μ˜ νƒ€μž…μ„ 가진 ν•˜λ‚˜μ˜ κ°’λ§Œ μ €μž₯ κ°€λŠ₯. 데이터(λ¦¬ν„°λŸ΄)을 μ €μž₯ν•˜κΈ° μœ„ν•œ μš©λ„. λ°μ΄ν„°λŠ” μ–Έμ œλ“ μ§€ λ³€κ²½ κ°€λŠ₯ν•΄μ„œ λ³€μˆ˜μ΄λ‹€. 변경이 λΆˆκ°€λŠ₯ν•œ μˆ˜λŠ” μƒμˆ˜. λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„  λ³€μˆ˜ μ„ μ–Έκ³Ό λ³€μˆ˜ μ΄ˆκΈ°ν™” μž‘μ—…μ΄ ν•„μš”ν•˜λ‹€.

     

    //1. λ³€μˆ˜μ„ μ–Έ( λ©”λͺ¨λ¦¬μ— νŠΉμ •ν¬κΈ°μ˜ μ˜μ—­μƒμ„± )
    
            String name;
            int age;
            String address;
            char gender;
            boolean isMarried;
            float height;
            double weight;

    λ³€μˆ˜λ₯Ό μ“Έλ €λ©΄ 선언을 λ¨Όμ €ν•˜κ³  μ €μž₯곡간을 λ§ˆλ ¨ν•΄μ€˜μ•Ό ν•œλ‹€. λ³€μˆ˜λŠ” μ†Œλ¬Έμžμ— μ˜λ―ΈμžˆλŠ” λͺ…μ‚¬ν˜•μœΌλ‘œ μ§€μ •ν•œλ‹€.

     

    //2.  λ³€μˆ˜μ΄ˆκΈ°ν™” ( 값을 맨처음 μ„€μ •ν•˜λŠ” μž‘μ—… )
    
            name = "홍길동"; 
            age = 20;
            address = "μ„œμšΈ";
            gender = '남';
            isMarried = false;
            height = 185.63F;
            weight = 78.25;

    κ·Έ λ‹€μŒ λ³€μˆ˜μ΄ˆκΈ°ν™”λ₯Ό ν•΄μ•Όν•œλ‹€. μ„ μ–Έλœ λ³€μˆ˜μ— 처음으둜 데이터λ₯Ό μž…λ ₯μ‹œν‚€λŠ” μž‘μ—…μ΄λ‹€. "ν™κΈΈλ™μ΄λΌλŠ” μ• κ°€ λ„€μž„μ— μ €μž₯λ˜λŠ”κ΅¬λ‚˜." 이런 μ‹μœΌλ‘œ 거꾸둜 읽으면 μ΄ν•΄ν•˜κΈ° 쉽닀.

     

    // λ³€μˆ˜ μ„ μ–Έκ³Ό μ΄ˆκΈ°ν™”λ₯Ό ν•œκΊΌλ²ˆμ—
    
    public class Sample02_varible2 {
    
        public static void main(String[] args) {
    
            String name = "홍길동";
            int age = 20;
            String address = "μ„œμšΈ";
            char gender = '남';
            boolean isMarried = false;
            float height = 185.63F;
            double weight = 78.25;
    
    
        }
    }
    //    좜λ ₯
    
            System.out.println("String name=" + name);
            System.out.println("int age=" + age);
            System.out.println("String address=" + address);
            System.out.println("char gender=" + gender);
            System.out.println("boolean isMarried=" + isMarried);
            System.out.println("float height=" + height);
            System.out.println("double weight=" + weight);
    // 슀트링 μ„€λͺ… λ„£μ–΄μ„œ ν•¨κ»˜ 좜λ ₯
    
    System.out.println("이름은 " + name +", λ‚˜μ΄λŠ” "+age + ",μ£Όμ†Œ "+address+ ", 성별은 "+gender
                    + ", κ²°ν˜Όμ—¬λΆ€λŠ” "+isMarried+ ", ν‚€λŠ” "+height  + ", λͺΈλ¬΄κ²ŒλŠ” " + weight+ "μž…λ‹ˆλ‹€.");
    
    System.out.printf("%s, %d, %s, %c, %b, %.2f, %.2f\n", name, age, address, gender, isMarried, height, weight);
    
    System.out.printf("이름은 %s, λ‚˜μ΄λŠ” %d, μ£Όμ†ŒλŠ” %s, 성별은 %c, κ²°ν˜Όμ€ %b, ν‚€λŠ” %.2f, λͺΈλ¬΄κ²ŒλŠ” %.2f μž…λ‹ˆλ‹€.", 
                        name, age, address, gender, isMarried, height, weight);

     

     

     

    μƒμˆ˜

    값이 변경될 수 μžˆλŠ” 것을 ‘λ³€μˆ˜’ 라고 ν•˜λ©°, 값을 λ³€κ²½ν•˜μ§€ λͺ»ν•˜λŠ” 것을 ‘μƒμˆ˜’라고 ν•œλ‹€. final ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ ‘μƒμˆ˜’둜 처리 κ°€λŠ₯ν•˜λ‹€. 일반적으둜 μƒμˆ˜λͺ…은 λŒ€λ¬Έμžλ‘œ μ„€μ •ν•œλ‹€. final λ°μ΄ν„°ν˜• μƒμˆ˜λͺ…=κ°’; public static final int NUM = 30;

    public class sample04_final {
    
        public static void main(String[] args) {
    
            // μƒμˆ˜: κ°’λ³€κ²½λΆˆκ°€, μ „λΆ€λŒ€λ¬Έμžλ‘œ
            //byte.MIN_VALUE, Byte.MAX_VALUE
            final int SIZE = 100;
            //SIZE=200; //κ°’ λ³€κ²½λΆˆκ°€;
            System.out.println(SIZE);
    
            int num = 10;
            System.out.println(num);
    
            num = 20;
            System.out.println(num);
        }
    
    }

     

     

     

    데이터 ν˜• λ³€ν™˜

    같은 μ’…λ₯˜μ˜ λ°μ΄ν„°ν˜•λΌλ¦¬λ§Œ ν˜• λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€. κΈ°λ³Έ-κΈ°λ³Έ / μ°Έμ‘°-μ°Έμ‘°

     

    λ¬΅μ‹œμ ν˜•λ³€ν™˜

    큰 νƒ€μž… 데이터와 μž‘μ€ νƒ€μž… 데이터가 연산될 λ•Œ κ²°κ³ΌλŠ” 큰 νƒ€μž…μœΌλ‘œ μžλ™μœΌλ‘œ λ³€κ²½λœλ‹€.

    byte -> short -> int -> long -> float -> double char - > int

    // μž‘μ€νƒ€μž…κ³Ό ν°νƒ€μž…μ΄ μ—°μ‚° ==> ν°νƒ€μž…
    
            int p = 100;
            double p2 = 3.14;
            double p3 = p + p2;
            byte b = 10;
            short b2 = b;
            int b3 = b2;
            long b4 = b3;
            float b5 = b4;
            double b6 = b5;

     

    int 보닀 μž‘μ€ λ°μ΄ν„°ν˜•κ°„μ˜ μ—°μ‚°κ²°κ³ΌλŠ” int둜 μžλ™μœΌλ‘œ λ³€κ²½λœλ‹€.

    // int 보닀 μž‘μ€ νƒ€μž…μ—°μ‚°κ²°κ³ΌλŠ” int둜
    
    short s = 10;
    short s2 = 20;
    short s3 = s + s2; //μ—λŸ¬

    숏숏끼리 μ—°μ‚°ν•˜λ©΄ μžλ™μœΌλ‘œ int둜 λ³€ν™˜λ˜κΈ° λ•Œλ¬Έμ— short에 λ‹΄μœΌλ©΄ μ•ˆλ˜κ³  int에 λ‹΄μ•„μ•Ό ν•œλ‹€.

     

    // char --> int
    
            char n = 'A';  //μ•„μŠ€ν‚€μ½”λ“œκ°’ 65
            int n2 = n+1;
            System.out.println(n2);  //66

    char νƒ€μž… 문자λ₯Ό μ—°μ‚°ν•  경우 μ•„μŠ€ν‚€μ½”λ“œ κ°’μœΌλ‘œ λ³€κ²½λ˜μ–΄ κ²°κ³Όκ°€ 좜λ ₯λœλ‹€.

     

    // λ¬Έμžμ—΄κ³Ό + λ¬Έμžμ—΄μ΄ μ•„λ‹Œ 데이터 => μ—°κ²°λœ λ¬Έμžμ—΄λ‘œ λ³€ν™˜
    
            System.out.println("10"+1+2+3);
            System.out.println(1+2+3+"10");
            //System.out.println("10" -1); // error

     

    λͺ…μ‹œμ  ν˜•λ³€ν™˜

    κ°•μ œ ν˜• λ³€ν™˜, type casting이라고 λΆ€λ₯Έλ‹€. 큰 λ°μ΄ν„°μ—μ„œ μž‘μ€ λ°μ΄ν„°λ‘œ λ³€ν™˜ν•  λ•Œ κ΄„ν˜Έ μ•ˆμ— λ³€ν™˜ν•˜λ €λŠ” 데이터 ν˜•μ„ λͺ…μ‹œν•œλ‹€.

    int n =10;
    short n2 = (short)n;
    System.out.println(n2);
            short x = 10;
            short x2 = 20;
            short x3 = (short) (x+x2);  // κ΄„ν˜Έλ‘œ λ¬Άμ–΄μ£ΌκΈ°
            //short x4 = (short) x + x2;  // error
    
            System.out.println(x4);

     

    --> λ¬Έμžμ—΄μ„ 숫자둜 λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜
    
        // μžλ°”μŠ€ν¬λ¦½νŠΈ: Number.parseInt("10") ==> 10
        // μžλ°” : Integer.parseInt("10") ==> 10
    
            String k ="10";
            System.out.println(k+20); // 1020
            int k2 = Integer.parseInt(k); //문자-> μ •μˆ˜ intλ³€ν™˜
            System.out.println(k2+20); // 30
    
    
    --> 숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜
    
        // μžλ°”μŠ€ν¬λ¦½νŠΈ: toString()
        // μžλ°”: String.valueOf(10) ==> "10"
    
            System.out.println(String.valueOf(10)+10); //1010

     

     

     

    λ³€μˆ˜μ™€ μ½”λ“œλΈ”λŸ­

    λ³€μˆ˜λŠ” λΈ”λŸ­ { } λ‹¨μœ„λ‘œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€. λ³€μˆ˜κ°€ μ„ μ–Έλœ λΈ”λŸ­μ΄ 이 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” λ²”μœ„(scope)κ°€ λœλ‹€.

    public class sample04_var {
    
        public static void main(String[] args) {
    
    
            //μžλ°”λŠ” λΈ”λŸ­ scope (μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” ν•¨μˆ˜ scope)
    
            {
                int num=10;
                System.out.println(num);  //10
            }
    
            // System.out.println(num); // λΈ”λŸ­λ°–μ—μ„œ μΈμ‹λΆˆκ°€
    
            {
                int num=20;
                System.out.println(num);  //20
            }
        }

    mainν•¨μˆ˜ μ•ˆμ— 두 개의 블둝이 μžˆλ‹€. 이 블둝 μ•ˆμ—μ„œ μ„ μ–Έλœ λ³€μˆ˜λŠ” 블둝 λ²”μœ„ μ•ˆμ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆλ‹€. 블둝 λ°–μ—μ„œλŠ” num 을 ν”„λ¦°νŠΈν•  수 μ—†λ‹€. μ–΄λŠμœ„μΉ˜μ—μ„œ λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λƒμ— λ”°λΌμ„œ μ‚¬μš©κ°€λŠ₯이 달라진닀.

     

    public class sample04_var {
    
        public static void main(String[] args) {
    
            int test = 100;  // λΈ”λŸ­ λ²”μœ„ λ°–μ—μ„œ λ³€μˆ˜μ§€μ •
    
            {
                int num=10;
                System.out.println(num);
                System.out.println(test); //μ‚¬μš©κ°€λŠ₯
            }
    
        }

    블둝 λ²”μœ„ λ°–μ—μ„œ λ³€μˆ˜λ₯Ό 지정할 경우 블둝 λ‚΄μ—μ„œλ„ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

     

    public class sample04_var {
    
        public static void main(String[] args) {
    
    
            {
                int num=10;
                System.out.println(num);
                System.out.println(test); //error
            }
    
    
            int test = 100; //μ—¬κΈ°μ„œ μ‹œμž‘ν•˜λ©΄ μœ„μ— ν”„λ¦°νŠΈ λͺ…령은 μ—λŸ¬λ‚œλ‹€. 
            // μ‚¬μš©ν•˜κΈ° 전에 λ°˜λ“œμ‹œ μœ—λ‹¨μ—μ„œ λ³€μˆ˜μ„ μ–Έλ˜κ³  값이 μ΄ˆκΈ°ν™”λ˜μ–΄μžˆμ–΄μ•Όν•¨
    
            {
                int num=20;
                System.out.println(num);
            }
        }

    λ³€μˆ˜ μ„ μ–Έ 및 μ΄ˆκΈ°ν™”κ°€ λ¨Όμ € μ§„ν–‰λ˜κ³  λ‚˜μ„œ ν”„λ¦°νŠΈλ¬Έμ„ μž‘μ„±ν•  수 μžˆλ‹€. μˆœμ„œκ°€ μ€‘μš”ν•˜λ‹€.

     

     

     

    λ³€μˆ˜ μ’…λ₯˜

    λ‘œμ»¬λ³€μˆ˜

    λ©”μ†Œλ“œ/ν•¨μˆ˜ μ•ˆμ— μ„ μ–Έλœ λ³€μˆ˜. λ©”μ†Œλ“œ μ•ˆμ—μ„œ μƒμ„±λ˜κ³  잠깐 썼닀가 λ©”μ†Œλ“œκ°€ λλ‚˜λ©΄ μ†Œλ©Έλœλ‹€. 둜컬 μ˜μ—­ μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€. λ‘œμ»¬λ³€μˆ˜λŠ” λ°˜λ“œμ‹œ μ‚¬μš© 전에 λ³€μˆ˜μ„ μ–Έκ³Ό μ΄ˆκΈ°ν™”κ°€ λ˜μ–΄μ•Όν•¨.

    public class Member_Test {//class 블둝
    
            public static void main(String[] args) {//main 블둝
    
                    //λ‘œμ»¬λ³€μˆ˜(λ©”μ†Œλ“œ/ν•¨μˆ˜μ— μ„ μ–Έλœ λ³€μˆ˜),μ„ μ–Έ+μ΄ˆκΈ°ν™” ν›„ μ‚¬μš©
                    int num = 10;
                    System.out.println("λ‘œμ»¬λ³€μˆ˜ " + num);
    
        }//main 블둝
    
    }//class 블둝

     

    μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ λ˜λŠ” λ©€λ²„λ³€μˆ˜

    main블둝 λ°–μ—μžˆλŠ” 클래슀 λ‹¨μ—μ„œ μ„ μ–Έλœ λ³€μˆ˜λ“€. μ–˜λ„€λ“€μ€ μ΄ˆκΈ°ν™”ν•  ν•„μš”μ—†κ³  μ„ μ–Έλ§Œ 해도 λœλ‹€. μ΄ˆκΈ°ν™”λ₯Ό μ•ˆν•˜λ©΄ λ°μ΄ν„°νƒ€μž…μ˜ κΈ°λ³Έκ°’μœΌλ‘œ μžλ™μ΄ˆκΈ°ν™” λœλ‹€. μΈμŠ€ν„΄μŠ€ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„  객체생성 과정이 ν•„μš”ν•˜λ‹€.

    public class Member_Test {//class 블둝
    
            // 멀버, μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ - class λΈ”λŸ­μ— μ„ μ–Έ
            // μ„ μ–Έ ν›„ μ΄ˆκΈ°ν™” λΆˆν•„μš” - μ‚¬μš©μ‹œ μžλ™ μ΄ˆκΈ°ν™”
            // ν•¨μˆ˜ μ•ˆμ—μ„œ new둜 ν•„μš”ν•œ 만큼 객체 생성 μ‚¬μš©
            // ν•¨μˆ˜ μ“΄κ²Œ main 밖에 μ—†μœΌλ‹ˆκΉŒ μ €μ•ˆμ—μ„œμ‚¬μš©
    
            String name;
            String phoneNumber;
            String address;
            char gender; 
    
    
            public static void main(String[] args) {//main 블둝
    
                    //λ‘œμ»¬λ³€μˆ˜
                    int num = 10;
                    System.out.println("λ‘œμ»¬λ³€μˆ˜ " + num);
    
        }//main 블둝
    
    }//class 블둝

    ν”„λ‘œκ·Έλž¨μ˜ μ‹œμž‘κ³Ό 끝은 main μ˜μ—­μ΄λ‹€. λ°–μ—μ„œ λ³€μˆ˜μ„ μ–Έ ν–ˆμ–΄λ„ ν”„λ¦°νŠΈν•˜λŠ” 건 main μ•ˆμ— μ¨μ€˜μ•Ό ν•œλ‹€.

    public class Member_Test {//class 블둝
    
            // 멀버, μΈμŠ€ν„΄μŠ€ λ³€μˆ˜ - class λΈ”λŸ­μ— μ„ μ–Έ
    
            String name;
            String phoneNumber;
            String address;
            char gender; 
    
    
            public static void main(String[] args) {//main 블둝
    
                    Member_Test mem1 = new Member_Test(); // 객체생성
                    // 클래슀 이름 적어주고 μ°Έμ‘°λ³€μˆ˜
                    mem1.name="홍길동"; // 생성객체 μ ‘κ·Ό nameλ©€λ²„λ³€μˆ˜ μ €μž₯
    
                    Member_Test mem2 = new Member_Test();
                    mem2.name="μ΄μˆœμ‹ ";
    
                    System.out.println(mem1.name); //생성 객체 λ©€λ²„λ³€μˆ˜ μ‚¬μš©
                    System.out.println(mem2.name);
    
        }//main 블둝
    
    }//class 블둝

    μΈμŠ€ν„΄μŠ€ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ new ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄ 객체생성을 ν•΄μ•Όν•œλ‹€. 클래슀 이름(λ°μ΄ν„°νƒ€μž…) 뒀에 λ³€μˆ˜λͺ…을 지정해주고 new ν‚€μ›Œλ“œ 뒀에 클래슀λͺ…() 으둜 객체λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜λ₯Ό 뢙인닀. λ°μ΄ν„°νƒ€μž… λ³€μˆ˜λͺ… = new 클래슀λͺ…() μƒμ„±λœ 객체에 κ°μ²΄λ³€μˆ˜λͺ….λ³€μˆ˜λͺ… 으둜 μƒˆλ‘œμš΄ 데이터값을 μž…λ ₯ν•΄μ€€λ‹€.

     

    클래슀 λ³€μˆ˜

    μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ— static만 λΆ™μ—¬μ£Όλ©΄ λœλ‹€. 클래슀 λ‹¨μ—μ„œ λ³€μˆ˜λ₯Ό μ„ μ–Έν–ˆκΈ° λ•Œλ¬Έμ— new ν‚€μ›Œλ“œλ‘œ 객체 생성 ν›„ μ‚¬μš©ν•΄μ•Όν•˜μ§€λ§Œ static을 μ„ μ–Έν•˜λ©΄ 객체생성 ν•„μš”μ—†μ΄ λ°”λ‘œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€. 클래슀λͺ….λ³€μˆ˜λͺ…μœΌλ‘œ λ°”λ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

    public class Member_Test {//class 블둝
    
            static int num = 0; // 클래슀 λ³€μˆ˜ //객체생성없이 μ‚¬μš©κ°€λŠ₯
            String name;
            String phoneNumber;
    
    
            public static void main(String[] args) {//main 블둝
    
                    System.out.println(Member_Test.num); // static λ³€μˆ˜μ˜ μ‚¬μš©, 객체생성 ν•„μš”μ—†μŒ
    
    
        }//main 블둝
    
    }//class 블둝

     

     

     

    😜 NEXT : μžλ°” μ—°μ‚°μž κ³΅λΆ€ν•˜κΈ°

    λ°˜μ‘ν˜•

    λŒ“κΈ€

κ°œλ°œκ³΅λΆ€