[Java ์ ๋ฌธ] ๋ฐ์ดํฐํ์ ๊ณผ ์ฐ์ฐ์
Datatype
์ซ์ํ๊ณผ ๋ฌธ์ํ
public class Datatype{
public static void main(String[] args) {
System.out.println(6); // Number
System.out.println("six"); // String
System.out.println("6"); // String
System.out.println(6+6); // 12
System.out.println("6"+"6"); // 66
System.out.println(6*6); // 36
// System.out.println("6"*"6"); // Error : ๋ฌธ์์ด์ * ์ฐ์ฐ์ ํ ์ ์๊ธฐ ๋๋ฌธ
System.out.println("1111".length());
// System.out.println(1111.length()); // Error : ์ซ์๋ length() ํจ์ ์ ์ฉ ๋ถ๊ฐ
}
}
์ ๊ณผ์ ์์๋ ์ซ์์ ๋ฌธ์์ด ํ์ ๋ง ์ดํด๋ณด์์ง๋ง, ๋๊ฒ ๋ณด๋ฉด ๋ฌดํํ ๋ง์ ๋ฐ์ดํฐํ์ ์ด ์๋ค. ๊ทธ๋ฌ๋ ์ฐ์ ๊ฐ์ฅ ์ค์ํ ๋ฐ์ดํฐํ์ ์ ๋ ๊ฐ์ง๋ผ๊ณ ๋ณผ ์ ์์ผ๋ฉฐ, ์์์ ํ์ธํ ๊ฒ๊ณผ ๊ฐ์ด ๊ฐ ๋ฐ์ดํฐํ์ ์๋ง ์ ์ฉํ ์ ์๋ ์ฐ์ฐ์, ํจ์ ๋ฑ์ด ๊ฐ๊ฐ ๋ค๋ฅด๋ค.
1. ์ซ์ํ์ ํ์ฉ
1-1. ์ฌ์น์ฐ์ฐ
public class Number {
public static void main(String[] args) {
System.out.println(6 + 2); // 8
System.out.println(6 - 2); // 4
System.out.println(6 * 2); // 12
System.out.println(6 / 2); // 3
}
}
์ด๋ ๊ฒ ๊ธฐ๋ณธ์ ์ธ ์ฌ์น์ฐ์ฐ ๋ฟ๋ง ์๋๋ผ, ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์๋ ์ํ์ ์ผ๋ก ๋ง์ด ์ฐ์ด๋ ์ฐ์ฐ๋ค์ groupingํด๋ ๊ฒ์ด ์๋ค. ๋ํ์ ์ผ๋ก Java์ Math ๋ฉ์๋๊ฐ ์๋ค. ์ด๋ ๋ค์ ํญ๋ชฉ์ ์ดํด๋ณด์.
1-2. ํด๋์ค ๋ฉ์๋
public class Number {
public static void main(String[] args) {
System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.floor(Math.PI)); // ๋ด๋ฆผ
System.out.println(Math.ceil(Math.PI)); // ์ฌ๋ฆผ
}
}
์ด๋ฌํ ๊ฐ๋จํ ์ํ์ ๊ณ์ฐ ๋ฟ๋ง ์๋๋ผ, ๋์ฑ ๋ง์ ๋ฉ์๋๋ฅผ ํ์ฉํ๋ฉด ๊ณ์ฐ๊ธฐ๋ก๋ ํ ์ ์๋ ํจ์ฌ ๋ณต์กํ ๊ณ์ฐ๋ค์ ํ ์ ์๋ค.
2. ๋ฌธ์์ด์ ํ์ฉ
2-1. ๋ฌธ์์ด
public class String {
public static void main(java.lang.String[] args) {
System.out.println("Hello World"); // String
// System.out.println('Hello World'); // Error : Java์์ ''๋ Character๋ผ๋ ๋ฐ์ดํฐํ์
System.out.println('H'); // Character => ๊ฒฐ๊ตญ String = Character๋ค์ด ๋ชจ์ฌ ์๋ ๊ฒ
System.out.println("H"); // String
}
}
์ฌ๊ธฐ์ ์ฃผ์ํด์ผ ํ ์ , ํนํ ๋์ฒ๋ผ Python์ ๋จผ์ ์ตํ ์ฌ๋์๊ฒ๋ ์์ฃผ ์ค์ํ ๋ถ๋ถ์ด ์๋ค.
์ฆ, character๋ ๋ฌธ์์ด์ ํ ๊ธ์ ํ ๊ธ์๋ฅผ ์๋ฏธํ๋ค. ์ฝ๊ฒ ๋งํด์, String์ char ๋ค์ ๋ชจ์ ๋ฐ์ดํฐํ์ ์ผ๋ก ๋ด๋ ๋ฌด๋ฐฉํ๋ค.
2-2. ๋ฌธ์์ด์ ์ค๋ฐ๊ฟ
์๋ฐ์์ ๋ฌธ์์ด ์ฝ๋๋ฅผ ์์ฑํ๋ ๋์ค, ์ค๋ฐ๊ฟ์ ํ๊ณ ์ถ์ ๋ ์ด๋ ๊ฒ ์์ฑํ๊ฒ ๋๊ธฐ๋ ํ๋ค. ์๋ฅผ ๋ค๋ฉด,
System.out.println("Hello World");
์ด ์ฝ๋์์ Hello์ World๋ฅผ ๋ค๋ฅธ ์ค์ ์ ๋ ฅํ๊ณ ์ถ๋ค๊ณ ํ์.
๐๐ป ์๋ชป๋ ๋ฐฉ๋ฒ : enter
- ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ์ต์์น ์์ ์ฌ๋๋ค์ Hello ๋ค์ enter๋ฅผ ๋ณดํต ์น ๊ฒ์ด๋ค.
System.out.println("Hello
World");
- ide๊ฐ ์๋ ํ๊ฒฝ์์๋ ์ด๋ฐ ๋ฐฉ์์ผ๋ก ๋์ค๊ฒ ์ง๋ง, ์ถ๋ ฅ์ ์ค๋ฅ๊ฐ ์๊ธฐ๊ฒ ๋๋ค.
- ๊ทธ๋ ๋ค๋ฉด ide์ ์ผ์ข ์ธ eclipse์์๋ enter๋ง ์ณ๋ ์ค๋ฐ๊ฟ์ด ๋ ๊น? ๋ต์, NO์ด๋ค. ์ฐ์ , enter๋ฅผ ์น๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๊ฐ ๋ฐ๋๋ค.

์ด๋ ์ถ๋ ฅํ๋ฉด ์ค๋ฐ๊ฟ์ด ๋์ง ์๊ณ , ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ์ด ๋๋ค.

๐๐ป ๋ง๋ ์์ : \n
- ์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ์ค๋ฐ๊ฟ์ด ์ ์์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
// new line
System.out.println("Hello \nWorld");

2-3. ๋ฌธ์์ด ๋ด์ ํฐ๋ฐ์ดํ
- ๋ฌธ์์ด ๋ด์ ํฐ ๋ฐ์ดํ๋ฅผ ์ฐ๊ณ ์ถ์ ๋๋ ์๊ธธ ์ ์๋ค. ์ด ๋ ์ญ์, \๋ฅผ ํ์ฉํ์ฌ ํด๊ฒฐํ ์ ์๋ค.
// ์ถ๋ ฅํ๊ณ ์ถ์ ์ฝ๋ = Hello "World"
System.out.println("Hello "World""); // => Error
// escape
System.out.println("Hello \"World\""); // Hello "World"
2-3. ๊ธฐํ ๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ
public class StringOperation {
public static void main(String[] args) {
System.out.println("Hello World".length()); // ๋ช ๊ธ์์ธ์ง ์ธ๊ธฐ
System.out.println("Hello, [[[name]]] ... Bye.".replace("[[[name]]]", "MJ");
}
}
- .length() : ๋ฌธ์์ด์ ๊ธ์ ์ ์ธ๊ธฐ
- [[[string]]] + .replace("[[[string]]]", "") : ๋๊ดํธ ์์ ๋ด์ฉ์ replace์ ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ก ๋์ฒดํ๊ธฐ
์ด ํฌ์คํ ์ Youtube - ์ํ์ฝ๋ฉ JAVA1 ๊ฐ์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์์ฑ๋์์ต๋๋ค.