๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
26
[JAVA] Object ํด๋์ค ๊ธฐ๋ณธ ์ฌ์ฉ
class Point{ int x, y; Point(int x, int y){ this.x = x; this.y = y; } } public class ObjectEx { static void print(Object obj) { System.out.println(obj.getClass()); System.out.println(obj.hashCode()); System.out.println(obj.toString()); System.out.println(obj); } public static void main(String[] args) { Point p = new Point(2, 3); print(p); } } ๊ฒฐ๊ณผ
2023.12.21
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์์ฑ์ ๋ง๋ค๊ธฐ ์์
ํ์ ์ ๋ณด ํด๋์ค ๊ตฌํํ๊ณ , 2๊ฐ์ ๊ฐ์ฒด ์์ฑํ๊ธฐ ํด๋์ค ์์ฑ ํด๋์ค ์ด๋ฆ: Student ๋ฉค๋ฒ๋ณ์: num, name, c, java, net(์ ์) ๋ฉ์๋ ์์ฑ์(num, name, c, java, net ์ด๊ธฐํ) ์ด์ ๊ณ์ฐ(getTot()) ์ถ๋ ฅ(printinfo()) class Student{ int num, c, java, net, tot; String name; Student(){//์์ฑ์๋ก ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ด๊ธฐํ num = 0; name = ""; c = 0; java = 0; net = 0; } Student(int num, String name, int c, int java, int net){ this.num = num; this.name = name; this.c = c; this.java ..
2023.12.21
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ๋ฐฐ์ด์ ์ ์ธ๊ณผ ์ฌ์ฉ
public class ExampleEx { public static void main(String[] args) { int[] arr = new int[5]; String[] s = new String[5]; float[] f = new float[5]; for(int i=0; i
2023.12.21
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์ธํฐํ์ด์ค ์์
* ๋ค์์ ์์ ๊ด๊ณ๋ฅผ ๊ฐ๋ ์ธํฐํ์ด์ค์ ํด๋์ค๋ค์ ์์ฑํ์ธ์. Student
2023.12.21
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์ธํฐํ์ด์ค
์ถ์ ๋ฉ์๋์ ์์๋ง์ผ๋ก ๊ตฌ์ฑ ๋ชจ๋ ๋ฉ์๋๋ abstract public์ด๋ฉฐ ์๋ต ๊ฐ๋ฅ ์์๋ public static final ํ์
์ด๋ฉฐ ์๋ต ๊ฐ๋ฅ ์ธํฐํ์ด์ค์ ๊ฐ์ฒด ์์ฑ ๋ถ๊ฐ ๋ค๋ฅธ ์ธํฐํ์ด์ค ์์ ๊ฐ๋ฅ ์ธํฐํ์ด์ค์ ๋ค์ค ์์ ๊ฐ๋ฅ ์ธํฐํ์ด์ค ํ์
์ ๋ ํผ๋ฐ์ค ๋ณ์ ์ ์ธ ๊ฐ๋ฅ implements public interface Phone {//interface๋ก ์์ฑ void sendCall(); void receiveCall(); } interface MobilePhone extends Phone{ void sendSMS(); void receiveSMS(); } interface MP3{ void play(); void stop(); } class PDA{ int calc(int a, int b) { ..
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์ค๋ฒ๋ผ์ด๋ฉ ์์
๋ค์์ ์์ ๊ด๊ณ๋ฅผ ๊ฐ๋ ํด๋์ค๋ค์ ์์ฑํ์ธ์. ํนํ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ ๊ท์น๊ณผ, super() ๋ฐ super๋ฅผ ์ ์ฉํด์ ์ค๋ณต์ด ์ต์ํ๋๋๋ก ์์ฑํ์ธ์. Shape – Line - Square – Rect – Circle 1) Shape - ๋ฉค๋ฒ๋ณ์: String name - ์์ฑ์: name์ ์ด๊ธฐํ - ๋ฉ์๋: void printInfo() //name์ ์ถ๋ ฅ 2) Line - ๋ฉค๋ฒ๋ณ์: int length - ์์ฑ์: name๊ณผ length๋ฅผ ์ด๊ธฐํ - ๋ฉ์๋: void printInfo() //name๊ณผ length๋ฅผ ์ถ๋ ฅ 3) Square - ๋ฉค๋ฒ๋ณ์: int area; - ์์ฑ์: name๊ณผ length๋ฅผ ์ด๊ธฐํ - ๋ฉ์๋: void getArea(int, int) //์ฌ๊ฐํ์ ๋์ด๋ฅผ ๊ตฌํจ=> ..
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ๊ธฐ๋ณธ ๊ตฌ์กฐ, C์ ๋ค๋ฅธ ์
๊ธฐ๋ณธ ๊ตฌ์กฐ public class ์ด๋ฆ {//ํด๋์ค public static int ์ด๋ฆ(๋ฐ๋ ๊ฑฐ){//๋ฉ์๋ ์คํํ๋ ๊ฑฐ } } ctrl+shift+o ์ฌ์ฉํ๋ ํด๋์ค ์ํฌํธ ์๋ ๋ฃ๊ธฐ Scanner ํด๋์ค scanf ๊ฐ์ ๊ฑฐ ๋์ Scanner๋ฅผ ๋ฑ๋กํด์ค์ ๊ฑฐ๊ธฐ ์๋ ๊ฑฐ ์ฌ์ฉํด์ผ ํจ. //์ด ์์๋ ๋ฉ์ธ ๋ฉ์๋์์ ํจ Scanner sc = new Scanner(System.in); int num; num = sc.nextInt(); //String(C์์ char) ๊ฐ์ ๊ฒฝ์ฐ sc.next()๋ฅผ ์ฌ์ฉํ๊ณ //floot, boolean ๊ฐ์ ๊ฒฝ์ฐ๋ ์ญ์ sc.nextFloot(), sc.nextBoolean() for-each for(int i=0; i
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์ถ์ ๋ฉ์๋, ํด๋์ค
์ํ์ ๋ง๋ค์ด๋๊ณ ๋์ค์ ๊ฐ์ ธ๋ค๊ฐ ์ฐ๊ธฐ ์ํ ์ฉ๋. ์ถ์ ๋ฉ์๋๋ฅผ ๋ฐ์ ๊ตฌํํ๋ ค๋ ์๋ธ ํด๋์ค์์๋ ๋ฌด์กฐ๊ฑด ์ค๋ฒ๋ผ์ด๋ฉ์ ํด์ผ ํ๋ค.(๊ตฌํ ์ ํ ๊ฒฝ์ฐ ๊ทธ ํด๋์ค๋ abstract๋ฅผ ๋ถ์ฌ์์ ์ถ์ ํด๋์ค๋ก ๋ง๋ค์ด์ค์ผ ํ๋ค.) abstract class Shape{ abstract void draw(); } class Line extends Shape{ @Override void draw() { System.out.println("Line"); } } class Circle extends Shape{ @Override void draw() { System.out.println("Circle"); } } class Rect extends Shape{ @Override void draw() { System.ou..
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ์ค๋ฒ๋ก๋ฉ vs ์ค๋ฒ๋ผ์ด๋ฉ ์ฐจ์ด
์ค๋ฒ๋ก๋ฉ ์ค๋ฒ๋ผ์ด๋ฉ ๊ฐ์ ํด๋์ค๋ ์์ ๊ด๊ณ์์ ๋์ผ ์ด๋ฆ ๋ฉ์๋ ์ค๋ณต ์ฌ์ฉ ๊ฐ๋ฅ ์๋ธ ํด๋์ค์์ ์ํผ ํด๋์ค์ ์๋ ๋ฉ์๋์ ๋์ผํ ์ด๋ฆ์ ๋ฉ์๋ ์ฌ์์ฑ ๋์ผ ํด๋์ค or ์์ ๊ด๊ณ ์์ ๊ด๊ณ ์ค๋ณต ์์ฑํด ํธ๋ฆฌํ๊ณ , ๋ค์ํ๊ฒ ์ฌ์ฉ ๋ฉ์๋ ์ด๋ฆ์ ๋ฐ๋์ ๋์ผ, ๋งค๊ฐ๋ณ์ ํ์
์ด๋ ๊ฐ์ ๋ฌ๋ผ์ผ ํจ ์ด๋ฆ, ํ์
๊ณผ ๊ฐ์, ๋ฆฌํด ํ์
์ด ๋ชจ๋ ๋์ผํด์ผ ํจ ์ปดํ์ผ ์ ๊ฒฐ์ ์คํ ์์ ์ ๊ฒฐ์
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ
์ดํด๋ฆฝ์ค ์ํธ ์ฌํํธ ์์ค override/implement methods class Shape{ void draw() { System.out.println("Shape"); } } class Line extends Shape{ @Override void draw() { System.out.println("Line"); } } class Rect extends Shape{ @Override void draw() { System.out.println("Rect"); } } class Circle extends Shape{ @Override void draw() { System.out.println("Circle"); } } public class MethodOverEx { static void paint(..
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] instanceof
instanceof ์ด๋ค ํด๋์ค๋ฅผ ์์๋ฐ์๋์ง ํ์ธ class Person{ String name; Person(String name){ this.name = name; } void printInfo() { System.out.println(name); } } class Student extends Person{ int snum; Student(String name, int snum){ super(name); this.snum = snum; } void printStudent() { System.out.println("snum: "+snum); } } class Researcher extends Person{ int rnum; Researcher(String name, int rnum){ super(na..
2023.12.20
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java
[JAVA] HashMap ์ฌ์ฉ๋ฒ
import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class HashMapEx { public static void main(String[] args) { HashMap h = new HashMap(); //ํ ์์ ๋ฐ์ Scanner sc = new Scanner(System.in); h.put("Python","ํ์ด์ฌ"); h.put("Java", "์๋ฐ"); h.put("Database", "๋ฐ์ดํฐ๋ฒ ์ด์ค"); Set keys = h.keySet();//์์ ์๋ String์ผ๋ก ํค๋ฅผ ์ง์ Iterator it = keys.iterator(); while(it..
2023.12.14
๐ฉ๐ปํ๋ก๊ทธ๋๋ฐ&DB/Java