[Java] Constructor, Casting, and Operators
//The part that creates and executes the actual object (constructor) in the Main class
//In other classes, design (abstract) properties, basic object framework, and functions (methods) are specified.
Creator
Used for actual object creation along with the basic object frame, and uses the same name as the class name.
Default constructor and parameter constructor
The default constructor means that there are no parameters in ( ); after the constructor name. Conversely, a parameter constructor contains parameters within parentheses. As a concept, new Member("hongkd", "qwer1234"@ @T47@@, "Hong Gil-dong", 30, 100); It is expressed like this. It is helpful in creating concrete objects.
this( )
In the field, we try to improve uniformity and readability by keeping variable names the same, but this is when problems arise. This is because of the feature that local variables take precedence over member variables. In other words, if you want to assign a local variable to a member variable attribute within the constructor, if the variable names are the same, it is displayed as assigning the local variable to the local variable. Therefore, if variable names are the same, use this() constructor to differentiate them.
this.userid = userid;
Divided like this so that local variables can be substituted into member variables.
|
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
|
package my.day03.a.constructor;
public class MemberMain {
public static void main(String[] args) {
//Import in the same package is not necessary
… Member hongMbr = new Member("hong kd", "qwer1234", "Hong Gil-dong", 30, 100); //Create instance
//The constructor Member() is undefined ==> Deletion of the default constructor, variables have not been designed by the parameter constructor
… Member cheongaMbr = new Member();
cheongaMbr.userid = "cheonga";
cheongaMbr.passwd = "abcd";
cheongaMbr.name = "Cheong-ah";
cheongaMbr.age = 20;
cheongaMbr.point = 300;
hongMbr.showInfo();
cheongaMbr.showInfo();
hongMbr.changeInfo("KDhong","1234 ","Hong Gil-dong", 25, 150); //method of change function
| ha","abcd0070","Cheong-ah", 21, 7000);
System.out.println(info);
}
}
|
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
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
114
115
116
|
package my.day03.a.constructor;
public class Member {
// 항상 추상화 먼저!!
// field(attribute == property == 속성)
String userid;
String passwd;
String name;
int age;
int point;
//*** Constructor *** // Must be the same as the class name!, return No type (difference from method)
//Created to create an object
/*
· public Member( ) {
… System.out.println(">> The default constructor of the Member class was called. <<");
//The empty space in () is the default constructor { }
*/
// *** If the constructor is not specified, the following is omitted.
// ++A default constructor must be written in the Web JSP specification · public Member() {}
//!!! Important!!! If a parameter constructor is declared, the omitted default constructor is automatically deleted
· public Member(String userid, String p asswd, String name, int age, int point) {
@@T707@The variable inside @//() is called a parameter (== parameter)
…
|
//In field work, variable names must be the same to improve unity and readability. If they are the same, use the this() constructor to distinguish them
this.userid = userid; //this(this) I will insert this value into the local variable value!!
this.passwd = passwd;
this.name = name;
this.age = age;
this.point = point;
{ }
//If you want to use both the default constructor and the parameter constructor, just add both.
// method (behavior == behavior == function)
// Example of creating a method with no parameters and no return type
void showInfo() {
System.out.println(" ==== "@@T794 @@+name+"'s member information ==== \n" //Instance method using instance variable
+ "1. 아이디: "+userid+"\n"
+ "2. 암호: "+passwd+"\n"
+ "3. 성명: "+name+"\n"
+ "4. 나이: "+age+"\n"
+ "5. 포인트: "+point+"\n");
}
// parameter가 있고 return 타입도 없는 메서드 만들기 예제
void changeInfo(String userid, String passwd, String name, int age, int point) {
this.userid = userid;
this.passwd = passwd;
this.name = name;
this.age = age;
this.point = point;
System.out.println(">> 변경 후 정보 조회하기 <<");
showInfo(); //Put showInfo() into the changeInfo() function so no additional input is needed in Main
{ }
// Example of creating a method with parameters and return type
String updateInfo(String userid, String passwd, String name, int age, int point) {
//Before change
String beforeInfo @ @T1010@@= "=== "@@T101 6@@+this.name Result before +"'s membership modification === \n"
@@T1030 @@+ "1.ID: "@@T 1036@@+this.userid+"\n"
@@T105 0@@+ "2.Password: "@@T 1056@@+this.passwd@@T1061 @@+"\n"
@@T107 0@@+ "3.Name: "@@ T1076@@+this.name+"\n"
@@T109 0@@+ "4.Age: "@@ T1096@@+this.age+"\n"
+ "5.포인트: "+this.point+"\n";
// 변경 후
this.userid = userid;
this.passwd = passwd;
this.name = name;
this.age = age;
this.point = point;
String afterInfo = "=== "+this.name+"님의 회원수정 후 결과 === \n"
+ "1.아이디: "+this.userid+"\n"
+ "2.암호: "+this.passwd+"\n"
+ "3.성명: "+this.name+"\n"
@+this.age+"\n"
… +this.point+"\n";
… return beforeInfo @@T1303 @@+ "\n" + afterInfo;
|
/*
Before change
· System.out.println("=== Result before "+this.name+"'s member modification === \n");
// Since it is a newly received variable, it will be accepted as this.variable
//!!! If I receive it like this, it shows " ==== "+name+"'s member information ==== \n" and I don't want to see it!!!
showInfo();
After change
this.userid = userid;
this.passwd = passwd;
this.name = name;
this.age = age;
this.point = point;
return "=== Result after "+this.name+"'s membership modification === \n"
+ "1.ID: "+this.userid+"\n"
+ "2.Password: "+this.passwd+"\n"
+ "3.Name: "+this.name+"\n"
+ "4.Age: "+this.age+"\n"
+ "5.Point: "+this.point+"\n";
*/
{ }
}
|
cs |
Casting
1. Automatic type conversion (implicit type conversion)
Automatic type conversion occurs from small to large data type. Automatic type conversion is possible from integer type to real number type and from char character type to int (integer type).
byte(1byte) - short(2byte) - int(4byte) - long(8byte)
float(4byte) - double(8byte)
char(2byte) Unicode
2. Forced casting
You can forcibly convert the data type from larger to smaller, from double, which is the basic type of real number, to float type. To change, float ft2 @@T 1484@@= (float)db2; Type conversion is possible.
|
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
|
package my.day03.b.casting;
public class CastingMain {
public static void main(String[] args) {
byte no1 = 100;
System.out.println("no1 => "+no1);
// no1 => 100
short no2 = 20000;
System.out.println("no2 => "+no2);
// no2 => 20000
short no3 = no1; //Automatic type conversion
//byte type no1 is automatically converted to short type and assigned to no3.
System.out.printl n("no3 => "+no3);
System.out.pri ntln("\n ~~~~~~~~ ~ Coercion Conversion ~~~~~~~~~~ \n");
int no4 = 300;
short no5 = (short) no4; //Forced type conversion
// In this case, () is called the casting operator.
System.out.printl n("no5 => "+no5);
// no5 => 300
float ft1 = 1.234567f; //Real The basic type is double, so enter the suffix f or F
System.out.println@@T1 800@@("ft1 => "+ft1);
// ft1 => 1.234567
double db1 = ft1; //Automatic conversion
//ft1, which is a float type, is automatically converted to a double type and assigned to db1.
System.out.println@@T1 834@@("db1 => "+db1);
// db1 => 1.2345670461654663 High precision of double type
double db2 @@T1853 @@= 1.93456789123;
float ft2 = (float)db2; //Forced type conversion
System.out.printl n("ft2 => "+ft2);
// ft2 => 1.2345679
//Comparison between bytes is possible only if they are of the same integer or real type.
float ft3 = 1.234567f; // float 4byte
int n = (int)ft3; // int 4byte
long ln = (long)ft3; // long 8byte
System.out.prin tln("n => "+n);
// n => 1
System.out.print ln("ln => "+ln);
// ln => 1
{ }
}
|
cs |
Operator
Increment/decrement operator
The post-increment and decrement operators (a++; b--;) increase and decrease by 1 after all other operations are completed.
The potential increase/decrease operator (++a; --b;) performs another operation after completing the increase/decrease first.
Logical operators
In a conditional statement, the logical operators &, | If only one is used, the number of all cases to be compared is executed to check true or false, and the following statement is executed. On the other hand, &&, || When used together with , execution stops when only one item is satisfied without executing all items.
| ★ Logical operators quiz again Solve int i=1; int j=i++; if( (i > @@T 2078@@++j) & (i++ @@T2 088@@== j) ) { i=i+j; } System.@@T211 6@@out.println("i="+i); In the first sentence, i = 2, j = 1 (as a post-increment/decrement operator, 1, the initialization value of i, is assigned to j and 1 to i. Since it is added) In the conditional statement, both ( ) are checked and if true, i = i + j; is performed. In the first parenthesis, 2 > 2 is false, so it is false. The second parentheses are 3 == 3, so it is false. It is 3. //The point to note here is that the value of the variable initialized in the first line continues to change as the conditional statement progresses. Even if all conditional statements are false, data entered while executing the conditional statement will not become the initialization value of the first line again. + When the conditional statement uses && under the same conditions as above, The output i is 2. Unlike &, in &&, the first parenthesis is false, so it is output without the subsequent parentheses. @@T21 00@@ To use the concept of an unchanging constant, final int a = 1; It should be written like this. |
Assignment operator
+= -= *= /= is calculated first and then assigned.
Ternary operator
Variable declaration = (conditional expression)? Value1 : Value2;
If the condition expression is true, value 1 is assigned to the variable, and if it is false, value 2 is assigned to the variable.
|
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 174 175 |
package my.day03.b.operator;
public class OperatorMain {
//int n = 10; //Instance variable
· public static void main(String[] args) {
// === Operator === //
// 1. Arithmetic operators + - * / % (remainder)
| //Local variable
| "+ (n+3)); //13
// System.out.println("n + 3 = "+ n+3); ==> n+3=103
| "+ (n-3)); //7
| "+ (n*3)); //30
| "+ (n/3)); //3 Share
System.out.println(" n / 3 = "+ (n/3f)); //3.3333333
| @+ (((double@@T2746 @@)n)/3));
|
System.out.println ("n % 3 = "+ n%3); //1 The rest
//Remainder when 10 is divided by 3 1
// 2. Increment/decrement operator ++ --
//int a = 7;
//int b = 3;
// or
int a=@@T28 07@@7, b=3;
System.out.print ln("a => "+ a); //a => 7
//a = a+1; or
a++;
System.out.print ln("a => "+ a); //a => 8
++a;
System.out.println("a => "+ a); //a => 9
System.out.println("b => "+ b); //b => 3
//b = b-1; 또는
b--;
System.out.println("b => "+ b); //b => 2
--b;
System.out.println("b => "+ b); //b => 1
// !!! 꼭 암기하세요 !!! ///
// 후위증감연산자(a++; b--;) 는 다른 연산을 다 마친 이후에 1씩 증감한다.
// 전위증감연산자(++a; --b;) 는 맨 먼저 증감을 마친 이후에 다른 연산을 한다.
int x=10, y=10;
int z=++x;
System.out.println("z => "+z); //z => 11
System.out.prin tln("x => "+x); //x => 11
//Because it is a potential increase/decrease operator, x is increased by 1 and then 11 is put into z. Calculate first and then substitute
z=y@@T30 52@@++;
System.out.prin tln("z => "+z); //z => 10
| @T3084@@+y+"\n"); //y => 11
//Because it is a postfix increment/decrement operator, y is increased by 1 after assigning y to z. Substitute first and calculate
// 2. Logical operators & & | && ||(vertical line)
/*
In mathematics, T ∧ T ∧ F ==> F
In mathematics, T ∧ T ∧ T ==> T
In mathematics, T ∨ T ∨ F ==> T
In mathematics, T ∨ T ∨ T ==> T
In mathematics, F ∨ F ∨ F ==> F
*/
int c=50, d@@T3 152@@=60, e=70;
boolean bool1 = ( c @@T31 74@@> d ) && ( d < e ) &@@T3195 @@& ( c == e );
// F F Skip
System.out.println@ @T3220@@("bool1 => "+bool1); //bool1 => false
boolean bool2 = ( c @@T32 40@@> d ) || ( d < e ) |@@T3261 @@| ( c == e );
// F F T T Skip
Next, skip
System.out.println@ @T3286@@("bool2 => "+bool2); //bool2 => false
System.out.prin tln("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//There is no skip for one logical operator.
boolean bool3 = ( c >@@ d ) & ( d @ @T3209@@< e ) & ( c @ @T3337@@== e );
// F T F
// &의 경우에는 다 연산하고 False
System.out.println@ @T3360@@("bool3 => "+bool3); //bool3 => false
boolean bool4 = ( c @> d ) | ( d @ @T3263@@< e ) | ( c @ @T3393@@== e );
// F T F
// |의 경우에는 다 연산하고 True
System.out.println@ @T3416@@("bool4 => "+bool4); //bool4 => true
System.out.prin tln("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//조건이 참일 때만, 실행해야할 명령을 구동한다.
//조건이 거짓일 때는 구동 안 함
int n1=10;
if(n1 < 20) {
System.out.@@T 3477@@println(">> n1 is less than 20");
}
// QUIZ SOLUTIONS
System.out.@@T349 5@@println("\n~~~~~ Quiz 1 ~~~~~~\n");
//The first question of Quiz 1 is &, and the second question is &&
|
int j=@@T3524@ @i++;
//The i and j values change due to the calculation of () in the logical operation. The value changed by the preceding () affects the operation of the following ()
//If the if() value by the logical operation is false, the value of {} is not executed.
//false, so even if the {} value is not executed, the execution value by () is preserved
if( (i > @@ T3558@@++j) @@T3567 @@& (i++ @ @T3577@@== j) ) {
i=i+j;
}
System.out.pri ntln("i="+i); // i=3
i=1;
j=i ++;
if( (i > @@ T3511@@++j) &@@T352 1@@& (i++@ @T3532@@ == j) ) {
i=i+j;
}
System.out.pri ntln("i="+i); // i=2
System.out.@@T357 9@@println("\n~~~~~ 퀴즈2 ~~~~~~\n");
int m1=0;
n1=1;
if( (m1++ == 0) | (n1++ == 2) ) {
m1=42;
}
System.out.println("m1=>"+m1 + ", n1=>"+n1); //m1=42, n1=2
m1=0;
n1=1;
if( (m1++ == 0) || (n1++ == 2) ) {
m1=42;
}
System.out. println("m1=>"@@T377 8@@+m1 + @@ T3785@@", n1=>"+n1); //m1=42, n1=1
// 4. Comparison operators == != > > < >= <=
// 5. Assignment operator (assignment operator after operation)
|
no+=3; // no = no+3; Add 3 to no and substitute
System.out.print ln("no = "+no); //no=4
no-@@@T3853 @@=@@@T3995@3@=@@T3995@3@@@T3994 // no = no-2; Subtract 2 from no and substitute
| "+no); //no=2
no*==4@@27 // no = no*5; Multiply no by 5 and substitute
System.out.print ln("no = "+no); //no=10
no/@@T390 8@@==@@T4055@6@4@@T40 // no = no/4; union of no divided by 4
System.out.print ln("no = "+no); //no=2
no%=3; // no = no%3; Take the remainder of no divided by 3
| "+no); //no=2
// 6. Ternary operator***
/*
Variable declaration = (conditional expression)? Value1: Value2;
It is used when you want to declare a variable and place a Bookmark.
If the conditional expression is true, assign value 1 to the variableT4,@@4@T412
If the conditional expression is false, value 2 is assigned to the variable.
*/
| num2=60;
| num2)?num1:num2;
| "+num3); //num3 =>60
{ }
}
|
cs |
Comments
No comments yet. Be the first!
164 posts in 테크
- 2233D Gaussian Splatting vs Unreal Engine: Two Ways to Build a 3D World — and Where Each One Ships
- 222LLMs Inside Unreal Engine: The New Skills Game Developers Need in 2026
- 220Living With Claude Fable 5: How the Most Capable Model Changes the Way You Actually Work
- 219Luma's Bet: From Video Generator to a Single Model That Thinks in Pixels
- 215The Best AI Video Models in 2026: Types, Differences, and Where This Is All Going