์ด ๊ธฐ์ฌ์์๋ ๋ชจ๋ Java ํ์ต์์๊ฒ Java ๋ณ์๊ฐ ๋ฌด์์ด๋ฉฐ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์ค๋ช
ํฉ๋๋ค.
์๋ฐ ๋ณ์๋?
Java์ ๋ณ์๋ ์์๋ก ์๊ฐํ ์ ์์ต๋๋ค. ์ด "์์"์๋ ํน์ ํฌ๊ธฐ๊ฐ ์์ต๋๋ค. ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ ๋๋ค. ํ ๋น๋๋ ๋ฉ๋ชจ๋ฆฌ์ ์์ ๋ณ์์ ์ ํ์ ๋ฐ๋ผ ๋ค๋ฅด์ง๋ง ์ด์ ๋ํด์๋ ์ ์ ํ์ ์ด์ผ๊ธฐํ๊ฒ ์ต๋๋ค. ๋์์ ์์ ์์ฒด์ ํฌ๊ธฐ๋ ๋ง๋ ํ์ ๋ณ๊ฒฝํ ์ ์์ง๋ง ๋ด์ฉ๋ฌผ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ์์๊ฐ ๋น์ด ์์ ์ ์์ต๋๋ค. ์ผ๋ถ ๊ฐ์ "์ฝ์ "ํ ๋ค์ ์ถ์ถํ์ฌ ๋ค๋ฅธ ๊ฐ์ ์ ๋ ฅํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ณ์๋ Java ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๋ฐ์ดํฐ ๊ฐ์ ์ ์ฅํ๋ ํ๋์ ๋๋ค. ์ด ์์์๋ ๋ค์๊ณผ ๊ฐ์ ์ค์ํ ๊ธฐ๋ฅ์ด ์์ต๋๋ค.- ํญ์ ํ๋์ ๊ฐ๋ง ํฌํจํ ์ ์์ต๋๋ค(๋๋ ๋น์ด ์์ ์ ์์ผ๋ฉฐ ์ด ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ด ํฌํจ๋จ).
- ๋ฐ์ดํฐ ์ ํ์ด ์์ต๋๋ค. ํฌ๊ธฐ๋ ๋ฐ์ดํฐ ์ ํ๊ณผ ์ฅ์๊ฐ ํ ๋น๋ ํน์ ๋ฉ๋ชจ๋ฆฌ ์์ญ(์์น)์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค.
![Java์ ๋ณ์ - 1](https://cdn.codegym.cc/images/article/b3c5ef2b-7f16-4b75-9b33-3acaa7dbbb7c/800.jpeg)
๋ณ์๋ฅผ ์ ์ธํ๋ ๋ฐฉ๋ฒ
Java์์ ๋ณ์๋ฅผ ์ ์ธํ๋ ค๋ฉด ์ ํ๊ณผ ์ด๋ฆ์ ์ง์ ํด์ผ ํฉ๋๋ค. ๋ค์์ ์ธ ๊ฐ์ง ๋ณ์๋ฅผ ์ ์ธํ๋ ์์ ๋๋ค.
int myInt;
String s;
Object variableName;
์ฌ๊ธฐ์ int , String ๋ฐ Object ๋ ๋ฐ์ดํฐ ์ ํ์ด๊ณ myInt , s , variableName ์ ๋ณ์ ์ด๋ฆ์
๋๋ค. ๋ฐ์ดํฐ ์ ํ์ ํ๋ก๊ทธ๋จ์ ํ์์ ๋ฐ๋ผ ์ ํํด์ผ ํ๋ฉฐ ์ด๋ฆ์ ์ ํํ์ฌ ๋ด์ฉ์ด ๋ช
ํํ๋๋ก ํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๋ฌผ๋ก ์ด๋ณด์์๊ฒ๋ ๊ทธ๋ ๊ฒ ์๊ฒฉํ์ง๋ ์์ง๋ง ๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ 's'์ ๊ฐ์ ๋ณ์ ์ด๋ฆ์ด ์ฝ๋์ ๊ฐ๋
์ฑ์ ์์์ํฌ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ฒ์๋ถํฐ ๋ณ์์ ์ฉ๋๋ฅผ ๋ช
ํํ๊ฒ ํ๋ ๋ฐฉ์์ผ๋ก ๋ณ์ ์ด๋ฆ์ ์ง์ ํ๋ ๋ฐ ์ต์ํด์ง๋ ๊ฒ์ด ์ข์ต๋๋ค. ๋ฌผ๋ก ์ด์ ๊ฐ ์์ต๋๋ค.
๋ณ์๋ฅผ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
๋ณ์๋ฅผ ์ ์ธํ๋ฉด ๋ณ์์ ๋ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํฉ๋๋ค. ์ด๊ธฐํ๋ "์์"์ ํน์ ๊ฐ์ "๋ฃ๋" ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ด ์์ ์ ๋ณ์ ์ ์ธ ์ค ๋๋ ๋์ค์ ์ํํ ์ ์์ต๋๋ค. ๋ํ ๋ณ์์ ๊ฐ์ ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค. ์:
public class VariableTest {
public static void main(String[] args) {
int myInt = 5;
String s;
s = "init";
System.out.println(s);
s = s+5;
System.out.println(s);
}
}
์ฌ๊ธฐ์์ myInt ๋ณ์๋ฅผ ํ ์ค๋ก ์ ์ธ ๋ฐ ์ด๊ธฐํํ๊ณ ์ฆ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๊ณ (๋ชจ๋ int ์ซ์์ ๋ํด 32๋นํธ) ์ด๋ฆ์ ์ง์ ํ ๋ค์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ์ ๊ฐ 5๋ฅผ ๋ฐฐ์นํ์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ฐ๋ฆฌ๋ ๋ฌธ์์ด s ์ ๋ํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ ๋นํ๊ณ ์๋ค๊ณ ์ ์ธํ์ผ๋ฉฐ ์ด๋ฏธ ๋ณ๋์ ๋ช
๋ น์ผ๋ก "init" ๊ฐ์ ์
๋ ฅํ์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ๋ฌธ์์ด์ ์ถ๋ ฅํ๊ณ ๋ณ์ s ์ ๊ฐ์ ๋ณ๊ฒฝํ์ต๋๋ค . ์ด ํ๋ก๊ทธ๋จ์์๋ ํ์ ์ถ๋ ฅํ์ผ๋ฉฐ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
์ด๊ธฐํ ์ด๊ธฐํ5
Java์ ๋ณ์ ๋ฐ์ดํฐ ์ ํ: ์์ ๋ฐ ๋น์์
Java์์ ๋ณ์์ ๋ฐ์ดํฐ ์ ํ์ ๋ ๊ทธ๋ฃน์ผ๋ก ๋๋ฉ๋๋ค.- ๊ธฐ๋ณธ ๋ฐ์ดํฐ ์ ํ์๋ byte , short , int , long , float , double , boolean ๋ฐ char ๊ฐ ํฌํจ๋ฉ๋๋ค.
- String , Arrays ๋ฐ Classes ์ ๊ฐ์ ๊ธฐ๋ณธ์ด ์๋ ๋ฐ์ดํฐ ์ ํ
public class VariableTest {
public static void main(String[] args) {
int myInt = - 5; // integer (whole number)
float myFloat = 2.718281828459045f; // Floating point number
char myLetter = 'a'; // character
boolean myBool = true; // boolean
String myText = "Hero"; // String
System.out.println(myInt + " " +
myFloat +" " + myLetter + " " + myBool + " " + myText);
Student myStudent = new Student("Walker","Johnny", "Kyle", null);
}
}
int , char , boolean ๋ฐ float ์ ํ๋ฆฌ๋ฏธํฐ๋ธ์
๋๋ค. ๋ฌธ์์ด ์ ๊ธฐ๋ณธ์ด ์๋๋๋ค. myStudent ๋ณ์๋ ์ด๋ป์ต๋๊น ? ์ด๊ฒ์ ์ฌ์ฉ์๊ฐ ๋ง๋ Student ํด๋์ค์ ๊ฐ์ฒด์
๋๋ค . ๊ธฐ๋ณธ์ด ์๋ ๋ณ์์ ๋ํ ์์
์ ์ค๋ช
ํ๊ธฐ ์ํด ๊ฐ๋จํ ์์ฑ๋์์ต๋๋ค. ์ด ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๊ฑฐ์ ๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ Java์์ ๋๋ถ๋ถ์
๋๋ค. ์ดํด๋ฅผ ๋๊ธฐ ์ํด Student ํด๋์ค ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค .
import java.util.Date;
public class Student {
String surname;
String name;
String secondName;
Long birthday; // Long instead of long is used by Gson/Jackson json parsers and various orm databases
public Student(String surname, String name, String secondName, Date birthday ){
this.surname = surname;
this.name = name;
this.secondName = secondName;
this.birthday = birthday == null ? 0 : birthday.getTime();
}
@Override
public int hashCode(){
//TODO: check for nulls
//return surname.hashCode() ^ name.hashCode() ^ secondName.hashCode() ^ (birthday.hashCode());
return (surname + name + secondName + birthday).hashCode();
}
@Override
public boolean equals(Object other_) {
Student other = (Student)other_;
return (surname == null || surname.equals(other.surname) )
&& (name == null || name.equals(other.name))
&& (secondName == null || secondName.equals(other.secondName))
&& (birthday == null || birthday.equals(other.birthday));
}
}
Java์ ๋ณ์ ์ ํ: ๋ก์ปฌ, ์ธ์คํด์ค ๋ฐ ์ ์
Java์๋ ์ธ ๊ฐ์ง ์ ํ์ ๋ณ์๊ฐ ์์ผ๋ฉฐ ๋ค์๊ณผ ๊ฐ์ด ๋์ดํ์ต๋๋ค.- ์ง์ญ ๋ณ์
- ์ธ์คํด์ค ๋ณ์
- ์ ์ ๋ณ์
์ง์ญ ๋ณ์
๋ฉ์๋ ๋ณธ๋ฌธ ๋ด์์ ์ ์ธ๋ ๋ณ์๋ฅผ ์ง์ญ ๋ณ์๋ผ๊ณ ํฉ๋๋ค. ๋ํ ์ง์ญ ๋ณ์๋ ์์ฑ์์ ๋ธ๋ก ๋ด๋ถ์์ ์ ์ธ๋ฉ๋๋ค. ๋ก์ปฌ ๋ณ์๋ ์์ฑ๋ ๋ฉ์๋, ์์ฑ์ ๋๋ ๋ธ๋ก ๋ด์์๋ง ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ํด๋์ค์ ๋ค๋ฅธ ๋ฉ์๋๋ ๋ณ์๊ฐ ์กด์ฌํ๋ค๋ ์ฌ์ค์กฐ์ฐจ ์ธ์ํ์ง ๋ชปํฉ๋๋ค. ๊ทธ๋์ ์ง์ญ ๋ณ์๋ ๋ฉ์๋, ์์ฑ์ ๋๋ ๋ธ๋ก์ด ์ ๋ ฅ๋๋ฉด ์์ฑ๋๊ณ ๋ณ์๋ ๋ฉ์๋, ์์ฑ์ ๋๋ ๋ธ๋ก์ด ์๋ํ์ง ์์ผ๋ฉด ์๋ฉธ๋ฉ๋๋ค. ์ง์ญ ๋ณ์๋ static ํค์๋๋ก ์ ์ํ ์ ์์ต๋๋ค. ๋ ๋์๊ฐ ์ง์ญ ๋ณ์์ ๋ํ ์ก์ธ์ค ํ์ ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค์์ ์์ ๋๋ค.
public class VariableTest {
public static void main(String[] args) {
System.out.println(myMethod("C plus "));
}
private static String myMethod(String myString) {
String myOtherString = "plus";
return myString + myOtherString;
}
}
์ฌ๊ธฐ์ myOtherString์ ์ง์ญ ๋ณ์์
๋๋ค. ๋ค๋ฅธ ๋ฐฉ๋ฒ์์๋ ์ฌ์ฉํ ์ ์์ง๋ง myMethod . ์ง์ญ ๋ณ์๋ ๊ธฐ๋ณธ๊ฐ์ ๊ฐ์ง ์ ์์ต๋๋ค. ํ๋ก๊ทธ๋จ์์ ํ๋ ์ด์์ ์ง์ญ ๋ณ์๊ฐ ์ด๊ธฐํ๋์ง ์์ผ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ฌ๋ฐ๋ฅด๊ฒ ์๋ํ์ง ์์ต๋๋ค. ์ด์ ์์ ์ค ํ๋๋ฅผ ์ฝ๊ฐ ๋ณ๊ฒฝํ๊ณ myBool ๋ณ์๋ฅผ ์ด๊ธฐํํ๋ ๊ฒ์ "์์ด ๋ด
์๋ค":
public class VariableTest {
public static void main(String[] args) {
int myInt = - 5; // integer (whole number)
float myFloat = 2.718281828459045f; // Floating point
char myLetter = 'a'; // character
boolean myBool; // boolean
String myText = "Hero"; // String
System.out.println(myInt + " " +
myFloat +" " + myLetter + " " + myBool + " " + myText);
Student myStudent = new Student("Walker","Johnny", "Kyle", null);
}
}
ํ๋ก๊ทธ๋จ์ ์คํํ๋ ค๊ณ ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ค๋ฅ:(10, 50) java: myBool ๋ณ์๊ฐ ์ด๊ธฐํ๋์ง ์์์ ์ ์์ต๋๋ค.
์ธ์คํด์ค ๋ณ์
ํด๋์ค ๋ด๋ถ์ ์์ง๋ง ๋ฉ์๋, ์์ฑ์ ๋๋ ๋ธ๋ก์ ๋ณธ๋ฌธ ์ธ๋ถ์ ์ ์ธ๋ ๋ณ์๋ฅผ ์ธ์คํด์ค ๋ณ์๋ผ๊ณ ํฉ๋๋ค. ์ธ์คํด์ค ๋ณ์๋ new ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์์ฑ๋ฉ๋๋ค . ๊ฐ์ฒด๊ฐ ํ๊ดด๋๋ฉด ํ๊ดด๋ฉ๋๋ค. ์ ์ ์ผ๋ก ์ ์ธํ ์๋ ์์ง๋ง ์ธ์คํด์ค ๋ณ์์ ์ก์ธ์ค ํ์ ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ธ์คํด์ค ๋ณ์๋ ํด๋์ค์ ๋ชจ๋ ๋ฉ์๋, ์์ฑ์ ๋ฐ ๋ธ๋ก์ ๋ํด ํ์๋ฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ธ์คํด์ค ๋ณ์๋ ๋น๊ณต๊ฐ์ด์ง๋ง ํ์ ํด๋์ค์ ๊ฐ์์ฑ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค. ๋ก์ปฌ ๋ณ์์ ๋ฌ๋ฆฌ ์ธ์คํด์ค ๋ณ์์๋ ๊ธฐ๋ณธ๊ฐ์ด ์์ต๋๋ค. ๋ชจ๋ ์ซ์ ๊ธฐ๋ณธ ์ ํ์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ 0์ด๊ณ ๋ถ์ธ์ ๊ฒฝ์ฐ false์ด๋ฉฐ ๊ฐ์ฒด ์ฐธ์กฐ์ ๊ฒฝ์ฐ null์ ๋๋ค. ์ ์ธ ์ค์ ๋๋ ์์ฑ์ ๋ด์์ ๊ฐ์ ํ ๋นํ ์ ์์ต๋๋ค. ๋ค์์ ์์ ๋๋ค.
//instance variable example
public class Employee {
// here we have a public instance variable. It is visible for this class and child classes
public String name;
//this instance variable is private so itโs visible for the Employee class only. You can use setters and getters to get it
private double salary;
public Employee (String empName) {
name = empName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double employeeSalary) {
salary = employeeSalary;
}
public static void main(String args[]) {
Employee employee = new Employee("Johnny");
employee.setSalary(1500);
System.out.println("name = " + employee.getName());
System.out.println("salary = " + employee.getSalary());
}
}
์ ์ ๋ณ์
static์ผ๋ก ์ ์ธ๋ ๋ณ์๋ฅผ ์ ์ ๋ณ์๋ผ๊ณ ํฉ๋๋ค. Java๋ ๋ฉ์๋, ์์ฑ์ ๋๋ ๋ธ๋ก ์ธ๋ถ์์ ์ด๋ฌํ ๋ณ์๋ฅผ ์ ์ธํ ์ ์๋๋ก ํฉ๋๋ค. ๋ก์ปฌ์ผ ์ ์์ผ๋ฉฐ ์ธ์คํด์ค๊ฐ ์๋ ํด๋์ค์ ์ํฉ๋๋ค. ์ฆ, ํด๋์ค์ ๋ชจ๋ ์ธ์คํด์ค ๊ฐ์ ์์ฑ๋๊ณ ๊ณต์ ๋ ์ ์ ๋ณ์์ ๋จ์ผ ๋ณต์ฌ๋ณธ์ ์๋ฏธํฉ๋๋ค. ์ ์ ๋ณ์์ ๋ํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํด๋์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋๋ ๋ ํ ๋ฒ๋ง ๋ฐ์ํฉ๋๋ค. ๋ชจ๋ ๊ฐ์์ฑ์ ๊ฐ์ง ์ ์์ง๋ง ์ผ๋ฐ์ ์ผ๋ก ๊ณต๊ฐ๋ก ์ ์ธ๋ฉ๋๋ค. ์ธ์คํด์ค ๋ณ์์ ๊ฐ์ ๊ธฐ๋ณธ๊ฐ๋ ์์ต๋๋ค.
public class Box
{
public void add(int data)
{
Storage.sum = Storage.sum + data;
Storage.count++;
}
public void remove(int data)
{
Storage.sum = Storage.sum - data;
Storage.count--;
}
}
public class Storage
{
public static int count = 0;
public static int sum = 0;
}
์์ ์์์๋ ๋ณ๋์ Storage ํด๋์ค๋ฅผ ๋ง๋ค๊ณ count ๋ฐ sum ๋ณ์๋ฅผ ์ด ํด๋์ค๋ก ์ด๋ํ๊ณ ์ ์ ์ผ๋ก ์ ์ธํ์ต๋๋ค. ๊ณต์ฉ ์ ์ ๋ณ์๋ ๋ชจ๋ ํ๋ก๊ทธ๋จ ๋ฉ์๋์์ ์ก์ธ์คํ ์ ์์ต๋๋ค(๋ฉ์๋๋ฟ๋ง ์๋๋ผ). ๋ฐฐ์ด ๋ด์ฉ์ ๋ณด๊ฐํ๋ ค๋ฉด Java ๊ณผ์ ์์ ๋น๋์ค ๊ฐ์๋ฅผ ์์ฒญํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
GO TO FULL VERSION