[Java] Data and Variables

· Tech· Java
Java

Initialization of data


Auto initialization (instance, static variable)

Integer data types (byte, short, int, long) are automatically initialized to 0, Float, double data types are automatically initialized to 0.0, and Character data types (char) are automatically initialized to ' '. Class types including String are automatically initialized to null.

Local variables must be initialized.

Method return type notation

The method must indicate what to do in front of the executed result.  If there is no separate return type, void must be written before the method name. If there is a return type, the return type must be the same. For example, the return type of String is " "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package my.day02;
//=========== Learn about the differences between local variables and instance variables.========
public class Main01 {
//attribute There is no static property, so a variable that only uses one object, an instance is required
String id;
String pwd;
String name;
String email;
int age;        //Integers only
//Member variable => The combination of instance variable and static variable is called member variable.
void showInfo1() {
/*
A local variable is used only in {} and the moment it leaves {}
Local variables are automatically deleted from memory (RAM).
What is important is that local variables must be initialized (== assigned a value). 
*/
|
|
        String shopeMoney = "50 million won";
Local variable names must not overlap within //{}
          
+ "1. ID: " @@T375 @@+id+"\n"
… @+pwd+"\n"
+name+"\n"
+email+"\n"
| @@+age+"\n"
+ "6. Desired salary: "@ @T466@@+hopeMoney+"\n"
                   + "7 .Expected salary: "+shopeMoney);
          
                                                                                                                                                                                                    //The + between the strings means string combination, and
          //The + between numbers means plus, and
          //The + between a string and a number means a combination of strings
         //"Hello"+" Nice to meet you" ==> "Hello, nice to meet you" *String combination
//"Hello" + 25 ==> "Hello 25" *Viewed as a string combination with numbers
//20+30 ==> 50 *Here, just add
        //"20"+30 ==> "2030" *String combination
        //20+30+"Goodbye" ==> "50Goodbye" *Operations are from left to right
          
{ }
|
//Must specify return type, method
· String showInfo2() {
          
|
         return "=== Member Information ===\n"
| @@+id+"\n"
… @+pwd+"\n"
+name+"\n"
+email+"\n"
| @+age+"\n"
                  + " 6. Hoped salary: "+hopeMoney;
{ }
public static void main(String[] args) {
          //Instantiation == Object (ma1) creation required
        Main01 ma1 = new Main01();
          
           //JavaScript treats "," the same, Java differentiates
· ma1.id = "leess";                      //Instance required when running ma1
· ma1.pwd = "qwer1234";
· ma1.age = 25;
· ma1.email = "leess@naver.com";
· ma1.name = "Lee Soon-shin";
                  
ma1.showInfo1();
          System.out.p rintln("~~~~~~~~~~~~~~~~~~~~~~~~~");
String info1 = ma1.showInfo2();
         System.out.println(info1);
|
        Main01 ma2 = new Main01();
ma2.showInfo1();
          System.out.pr intln("~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
         System.out.println(info2);
{ }
}
cs

Using a static method from another package

To use the static method of the

my.util package in another package, you must know the type of access modifier. (public, etc.)

For common functions, it is recommended to use static methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package my.util;
//import java.lang.*; is always generated
import java.util.Date;
import java.text.SimpleDateFormat;
public class MyUtil {
//=== Create a static method that outputs the current time ====
public static void currentTime() {
        Date now = new Date(); //Current time, local variable now
         SimpleDateFormat sdfmt = @@T98 1@@new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = sdfmt.format(now);
        System.out.println(">> 현재시각: "+result+"입니다. <<");
        
        // 2021-01-07 11:22:35
    }
    
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package my.day02;
 
import my.util.MyUtil;
 
//=========== static 메서드에 대해서 알아본다.===========
public class Main02 {
 
    public static void main(String[] args) {       //콘솔 실행은 Main메서드에서 실행하기
        
         System.out.@@T1169 @@println(">> This is the Main02 class <<");
          //Current time output
MyUtil.currentTime();    //Create and use common functions as static methods
{ }
}
package my.day02;
import my.util.MyUtil;
//=========== Learn about static methods.===========
public class Main03 {
public static void main(String[] args) {
          
          System.out.@@T1235 @@println(">> This is the Main03 class <<");
          
//Current time output
MyUtil.currentTime();
          
{ }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package my.day02.dataType;
 
public class MainSungjuk {
 
    public static void main(String[] args) {
        
    /*    
        
        int hakbun1 = 20191234;
        int hakbun2 = 0014543;
        
        String hakbun3 = "20191234";
        String hakbun4 = "0014543";    
        
        System.out.println("hakbun1: "+hakbun1);  //20191234
        System.out.println("hakbun2: "+hakbun2);  //6499    
        System.out.println("hakbun3: "+hakbun3);  //20191234
        System.out.println("hakbun4: "+hakbun4);  //0014543
    */
        
        Sungjuk lssSj = new Sungjuk();
        lssSj.hakbun = "091234";
        lssSj.name = "이순신";
        lssSj.kor = 68;
        lssSj.eng = 95;
        lssSj.math = 100;
        
        Sungjuk eomSj= new Sungjuk();
        eomSj.hakbun = "109876";
        eomSj.name = "엄정화";
        eomSj.kor = 78;
        eomSj.eng = 88;
        eomSj.math = 95;
        
        lssSj.showSungjuk();        //void로 별도 return 타입 적을 필요가 없음
        System.out.println("----------------------------");
eomSj.showSungjuk();
          
          
/*
=== Grade results ===
1. Student number: 091234
2. Name: Yi Sun-shin
3. Korean: 68
4. English: 95
5. Math: 100
6. Total score: 263
----------------------------
=== Grade results ===
1. Student number: 109876
2. Name: Jeonghwa Uhm
3. Korean: 78
4. English: 88
5. Math: 95
6. Total score: 261
*/
/*          
          System.out.println("----------------------------");        
          System.out.println("5/2 => "+(5/2));       
//quote Integer/Integer ==> Integer@@T1714@ @
          System.out.println("5%2 => "+(5%2));       
//Remainder %Integer = => Remainder@ @T1722@@
          System.out.println("5/2.0 => "+(5/2.0));
          System.out.println("5.0/2 => "+(5.0/2));
          System.out.println("5.0/2.0 => "+(5.0/2.0));
… // Even if only the denominator and numerator are real numbers, the value is real numbers
*/
          
          
          
{ }
}
cs

Types of data types


1. Jeong Soo-hyung

int is the basic type, long must be suffixed with l/L.

byte (1byte == 8bit) : -2^7 ~ 2^7-1 ==> -128 ~ 127

short(2byte == 16bit) : -2^15 ~ 2^15-1 ==> -32,768 ~ 32,767

int (4byte == 32bit) : -2^31 ~ 2^31-1 ==> -2,147,483,648 ~ 2,147,483,647

long (8byte == 64bit) : -2^63 ~ 2^63-1 ==> -9, 223,372,036,854,775,808 ~ 9,223,372,036,854,775,807

            

2. Real number

float(4byte) : Single precision Suffix f/F Expressed up to 7 decimal places.     135.3246235

double(8byte): Double precision expressed up to 15 to 16 decimal places. 135.3246234502345642

3. Character type

char: Java uses the Unicode system, 2byte, and the range is 0 to 65535.  See UNICODE table

char ch6 = '\u2665';  

Variable Naming Rules

It is case sensitive, and the first letter cannot be a number.

Only '_' and '$' can be used as special characters. Reserved words such as package, import, public, class, String, int, float ... cannot be used as variable names.  When defining a variable name, the first letter begins with a lowercase letter, and if a word is combined, the second word begins with an uppercase letter General.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package my.day02.dataType;
 
public class Sungjuk {
    //=== 속성, property, attribute, field ===
    
    String hakbun; // null 학번         String
    String name; // null 학생명      String
    byte kor; // 0 국어(0~100)    숫자(정수)
    byte eng; // 0 영어         숫자(정수)
    byte math; // 0 수학          숫자(정수)
    
    char hakjum; // ' ' 학점 'A' 'B' 'C' 'D' 'F'
    
    //=== 기능, 행위, behavior, method ===
    void showSungjuk() {
        
        short total = (short@@T 2208@@)(kor+eng+math);
        String star = " ";
          
         //*** In Java, if a variable whose data type is byte||short||char performs four arithmetic operations (+,-,*,/),
// The data type of the variable is automatically changed to int!
            // kor+eng+math ==> Automatically converted to int type. 
|
         //0~300 ***Local variables must be initialized
          
       float average1 = (float )(total/3.0);
float average2 =@@T 2276@@ total/3.0f;
        double average3 = t otal/3.0;
        long money = 5000000000L;       
           //In Java, integers basically take the int type, so they cannot be used.
        //If you want to display it as a long type, just write l or L after the number.
          
         if(average1 >@@ T2326@@= 90) {
              hakjum = 'A';
             }
        else if(average1 >= 80) {
            hakjum = 'B';            
        }
        else if(average1 >= 70) {
            hakjum = 'C';            
        }
        else if(average1 >= 60) {
            hakjum = 'D';            
        }
        else {
            hakjum = 'F';            
        }
        
        if(hakjum == 'A') {
            star = "☆☆☆☆☆";
        }
        else if(hakjum == 'B') {
            star = "☆☆☆☆";            
        }
        else if(hakjum == 'C') {
            star = "☆☆☆";            
        }
        else if(hakjum == 'D') {
            star = "☆☆";            
        }
        else {
            star = "☆";            
        }
        
        System.out.println("=== 성적결과 === \n"
                + "1. 학번: "+hakbun+"\n"
                + "2. 성명: "+name+"\n"
                + "3. 국어: "+kor+"\n"
                + "4. 영어: "+eng+"\n"
                + "5. 수학: "+math+"\n"
           + "6. 총점: "@ @T2664@@+total+"\n"
           + "7. 평균1: "@@ T2682@@+average1+"\n"
           + "8. 평균2: "@@ T2700@@+average2+"\n"
           + "9. 평균3: "@@ T2718@@+average3+"\n"
           + "10. 장학금: " +money+"\n"
           + "11. 학점: "@ @T2754@@+ hakjum+"\n"
                + @@T276 9@@"12. 학점(☆):  "+ star);    //☆☆☆☆☆
        
                // “6. 총점:” +68+95+100
           //6. 총점: 68” +95+100   이렇게 문자열의 결합이 됨@@@@@
                // 계산순서에 유의하여 지정해주기
        
    
    // == 문자형 타입 ==
    // 자바는 char 타입을 표현할 때 unicode를 사용한다.
    char ch1 = 50577;  //유니코드 테이블의 숫자
    char ch2 = 65;
    char ch3 = 'a';
    char ch4 = 'a'+1;     //97+1==> 98
    char ch5 = (@@T 2729@@char)(ch4-1);     //***변수는 모가 올 지 모르니까 
    char ch6 = '\u2665';    
    
    System.out.@@T27 57@@println("\n === 문자형 ===");
    System.out.println@@T27 68@@("ch1: "+ch1);
    System.out.println@@T27 82@@("ch2: "+ch2);
    System.out.println@@T27 96@@("ch3: "+ch3);
    System.out.println@@T28 10@@("ch4: "+ch4);
    System.out.println@@T28 24@@("ch5: "+ch5);
    System.out.println@@T28 38@@("ch6: "+ch6);
    
/*
      === 문자형 ===
   ch1: 양
 ch2: A
    ch3: a
 ch4: b
    ch5: a
    ch6: ♥
 */    
    
    }
}
cs

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164