[nhn next] java 강의 - week1
DESCRIPTION
Java 강의 1주차TRANSCRIPT
PL����������� ������������������ in����������� ������������������ ����������� ������������������ Week01����������� ������������������
Java?
조영호����������� ������������������ [email protected]����������� ������������������
Java! ����������� ������������������
프로그래밍����������� ������������������ 연습����������� ������������������ C/C#����������� ������������������ 과정을����������� ������������������ ����������� ������������������ 이수하셨다면����������� ������������������
오늘����������� ������������������ 배우는����������� ������������������ 내용은����������� ������������������ 아주����������� ������������������ 쉽습니다����������� ������������������
C����������� ������������������ C#����������� ������������������
Java����������� ������������������ 단순����������� ������������������ 변환����������� ������������������
C
C#
Java
Week1����������� ������������������
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
Hello World!����������� ������������������
에디터를����������� ������������������ 이용해����������� ������������������ 코드����������� ������������������ 작성����������� ������������������
public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } }
Hello.java
Compile����������� ������������������
Compile
javac Hello.java Hello.class
Execute����������� ������������������ Execute
java Hello
Hello.class
Hello class의����������� ������������������ main() 메서드를����������� ������������������ 실행하라����������� ������������������
World.class
Call
에디터를����������� ������������������ 이용해����������� ������������������ 코드����������� ������������������ 작성����������� ������������������
public class Hello { public static void main(String [] args) { System.out.print("Hello, "); World.print(); } }
Hello.java
World.java
public class World { public static void print() { System.out.println("World"); } }
Compile����������� ������������������ Compile
javac Hello.java
Hello.java Hello.class
Compile
World.java World.class
Call World.print();
Execute����������� ������������������ Execute
java Hello
Hello.class World.class
Call
Linking??����������� ������������������
Dynamic Class Loading����������� ������������������
Execute
java Hello
Hello.class World.class
Load Hello class
Dynamic Class Loading����������� ������������������
Execute
java Hello
Hello.class World.class
Call
Load Hello class
World.print();
Load World class
Java Virtual Machine����������� ������������������
Hello.class World.class
Load Hello class Load World class
JVM
OS����������� ������������������
Hello.class World.class
Load Hello class Load World class
JVM OS����������� ������������������
Byte Code (OS에����������� ������������������ 독립)����������� ������������������
(OS에����������� ������������������ 종속)����������� ������������������
Write Once Run Anywhere����������� ������������������
WORA����������� ������������������
Eclipse����������� ������������������
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
규칙����������� ������������������ 훑어����������� ������������������ 보기����������� ������������������
public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } }
모든����������� ������������������ 코도는����������� ������������������ 반드시����������� ������������������ ����������� ������������������ 클래스����������� ������������������ 안에����������� ������������������ 위치����������� ������������������
public����������� ������������������ 클래스����������� ������������������ 이름은����������� ������������������ ����������� ������������������ 파일명과����������� ������������������ 동일하게����������� ������������������
명령행����������� ������������������ 인자를����������� ������������������ 전달����������� ������������������ 받는����������� ������������������ ����������� ������������������ 문자열����������� ������������������ 배열����������� ������������������
main()����������� ������������������ 메서드는����������� ������������������ ����������� ������������������ 반드시����������� ������������������ 하나만����������� ������������������ ����������� ������������������ 존재해야����������� ������������������ 함����������� ������������������
화면에����������� ������������������ 출력하기����������� ������������������ 위해서는����������� ������������������ ����������� ������������������ System.out.println(“..”)����������� ������������������ 또는����������� ������������������ ����������� ������������������ System.out.print()����������� ������������������ 사용����������� ������������������
모든����������� ������������������ 문장은����������� ������������������ ‘;’으로����������� ������������������ 종료����������� ������������������
클래스����������� ������������������ 선언����������� ������������������ public����������� ������������������ class����������� ������������������ 클래스명����������� ������������������ {����������� ������������������ ����������� ������������������ }����������� ������������������
“{”은����������� ������������������ ����������� ������������������ 같은����������� ������������������ 행에����������� ������������������ ����������� ������������������
명명����������� ������������������ 규칙����������� ������������������
public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } }
클래스����������� ������������������ 이름은����������� ������������������ 첫글자만����������� ������������������ 대문자로����������� ������������������ 시작����������� ������������������ 매개변수,����������� ������������������ 지역변수는����������� ������������������
소문자로����������� ������������������ 시작����������� ������������������
메소드����������� ������������������ 이름은����������� ������������������ 소문자로����������� ������������������ 시작����������� ������������������
Camel Case����������� ������������������ ClassName����������� ������������������
methodName����������� ������������������
variableName����������� ������������������
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
Type Size Min Max
boolean 8bit false true
char 16bit \u0000 \uffff
byte 8bit -128 127
short 16bit -32,768 32767
int 32bit -2,147,483,648 2,147,483,647
long 64bit -9,223,372,036,854,775,808 9,223,372,036,854,775,807
float 32bit 7개의����������� ������������������ 유효����������� ������������������ 숫자를����������� ������������������ 가지며,����������� ������������������
-3.4E+38의����������� ������������������ 근사값����������� ������������������ 7개의����������� ������������������ 유효����������� ������������������ 숫자를����������� ������������������ 가지며,����������� ������������������
3.4E+38의����������� ������������������ 근사값����������� ������������������
double 64bit 15개의����������� ������������������ 유효����������� ������������������ 숫자를����������� ������������������ 가지며,����������� ������������������
-1.7E+308의����������� ������������������ 근사값����������� ������������������ 15개의����������� ������������������ 유효����������� ������������������ 숫자를����������� ������������������ 가지며,����������� ������������������
1.7E+308의����������� ������������������ 근사값����������� ������������������
Primitive Type����������� ������������������
public class Primitve { public static void main(String [] args) { boolean booleanValue = true; byte byteValue = 10; char charValue = 'A'; short shortValue = 10; int integerValue = 10; long longValue = 10; float floatValue = 0.1f; double doubleValue = 0.1; System.out.printf("%b %d %c %d %d %d %f %f", booleanValue, byteValue, charValue, shortValue, integerValue, longValue, floatValue, doubleValue); } }
Primitive.java
Primitive Type����������� ������������������
diff C/C# Java����������� ������������������
unsigned int����������� ������������������
uint����������� ������������������ or����������� ������������������
No
diff C C#/Java����������� ������������������
No Pointer Type����������� ������������������
diff C C#/Java����������� ������������������
Value Type(Primitive Type)����������� ������������������
Reference Type����������� ������������������
boolean, char, byte, short , int , long, float , double
String, Object, Array …
Pointer Type ?����������� ������������������
Stack Heap
Value Type����������� ������������������ ReferenceType����������� ������������������
Stack & Heap����������� ������������������
boolean, char, byte, short , int , long,
float , double String, Object, Array
Heap
diff C C#/Java����������� ������������������
No����������� ������������������
malloc����������� ������������������ calloc����������� ������������������ realloc����������� ������������������
diff C C#/Java����������� ������������������
new����������� ������������������
Heap
public class NewIsMalloc { public static void main(String [] args) { int sum = 0; int [] intArray = new int [] {1, 2, 3, 4, 5}; for(int loop=0; loop<intArray.length; loop++) { sum += intArray[loop]; } System.out.println(sum); } }
NewIsMalloc.java
new����������� ������������������
Stack Heap
Value Type����������� ������������������ ReferenceType����������� ������������������
Stack & Heap����������� ������������������
sum 0
1 2 3 4 5 intArray
new를����������� ������������������ 이용한����������� ������������������ 메모리����������� ������������������ 할당����������� ������������������ 불가능����������� ������������������ 주소����������� ������������������ 참조����������� ������������������ 불가능����������� ������������������
Primitive Type
diff C C#/Java����������� ������������������
diff C/C# Java����������� ������������������
No struct����������� ������������������
Use Class����������� ������������������
Heap
diff C C#/Java����������� ������������������
No����������� ������������������
free����������� ������������������
Heap
diff C C#/Java����������� ������������������
Garbage Collection����������� ������������������
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
C String����������� ������������������
char *school = "Next";char school[5] = "Next"; int length = strlen(school);char first = school[0];
C# String����������� ������������������
char first = school[0];int length = school.Length;
string school = "Next";
Java String����������� ������������������
String school = "Next"; int length = school.length();
Procedural vs Object-Oriented����������� ������������������
char *school = "Next"; int length = strlen(school);
String school = "Next"; int length = school.length();
Data
Function
Data + Function
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
C = C# = Java����������� ������������������
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
C = C# = Java����������� ������������������
if, switch, for, while����������� ������������������
diff C C#/Java����������� ������������������
C 0 == false !0 == true
No boolean����������� ������������������
diff C C#/Java����������� ������������������
C int number = 0;if (number = 1) { printf("true\n");} else { printf("false\n");}
diff C C#/Java����������� ������������������
C# Java
bool����������� ������������������
boolean����������� ������������������
C# Java
int positive = 10; if (positive) { ... }
boolean positive = true; if (positive) { ... }
diff C C#/Java����������� ������������������
// error
diff C C#/Java����������� ������������������
switch(???)����������� ������������������
C C#/Java
정수계열 정수계열
문자열
C < - > C # < - > J ava ����������� ������������������
#include <stdio.h>#include <time.h>#include <stdlib.h> int main(int argc, char *argv[]) { int number; int count = 1; srand((int)time(NULL)); number = (rand() % 100) + 1; printf("1~100사이의 컴퓨터가 생각한 숫자를 맞춰 보세요. 6번까지 시도할 수 있습니다.\n"); do { int guess; printf("생각하신 숫자는? "); scanf("%d", &guess); if (guess != number && count == 6) { printf("6번의 시도가 모두 종료되었습니다. 다음에 다시 시도해 주세요.\n"); break; } if (guess == number) { printf("성공하셨습니다!!!\n"); break; } if (guess > number) { printf("너무 높네요!\n"); } else { printf("너무 낮네요!\n"); } count++; } while (1);}
C
using System; namespace Guess{ class Guess { public static void Main(string[] args) { Random random = new Random(); int number = random.Next(1, 101); Console.WriteLine("1~100사이의 컴퓨터가 생각한 숫자를 맞춰 보세요. 6번까지 시도할 수 있습니다.");
int count = 1; do { Console.Write("생각하신 숫자는? "); int guess = int.Parse(Console.ReadLine());
if (guess != number && count == 6) { Console.WriteLine("6번의 시도가 모두 종료되었습니다. 다음에 다시 시도해 주세요."); break; }
if (guess == number) { Console.WriteLine("성공하셨습니다!!!"); break; }
if (guess > number) { Console.WriteLine("너무 높네요!"); } else { Console.WriteLine("너무 낮네요!"); } count++; } while (true); } }}
C#
package Guess;import java.util.Scanner; public class Guess { public static void main(String [] args) { int number = (int)(Math.random() * 100) + 1; System.out.println("1~100사이의 컴퓨터가 생각한 숫자를 맞춰 보세요. 6번까지 시도할 수 있습니다."); int count = 0; do { System.out.print("생각하신 숫자는? "); Scanner scanner = new Scanner(System.in); int guess = scanner.nextInt(); scanner.close(); if (guess != number && count == 6) { System.out.println("6번의 시도가 모두 종료되었습니다. 다음에 다시 시도해 주세요."); break; } if (guess == number) { System.out.println("성공하셨습니다!!!"); break; } if (guess > number) { System.out.println("너무 높네요!"); } else { System.out.println("너무 낮네요!"); } count++; } while (true); }}
Java
To p i c s ����������� ������������������
JVM & Class
Convention
Data Type
String
Operation
Condition & Loop
Array
1차원����������� ������������������ 배열����������� ������������������
C����������� ������������������ #include <stdio.h> void show_arrays(int [], int); int main(int argc, char *argv[]) { int arrays[] = {1, 2, 3, 4, 5}; int len = sizeof(arrays) / sizeof(int); int *ptr_arrays = arrays; show_arrays(ptr_arrays, len);}// void show_arrays(int *arrays, int len) void show_arrays(int arrays[], int len) { int loop; for(loop=0; loop < len; loop++) { printf("%d\t", arrays[loop]); } printf("\n");}
using System; namespace Arrays{ class Arrays { public static void Main(string[] args) { int[] arrays = new int[] { 1, 2, 3, 4, 5 }; ShowArrays1(arrays); ShowArrays2(arrays); } public static void ShowArrays1(int[] arrays) { for (int loop = 0; loop < arrays.Length; loop++) { Console.Write("{0}\t", arrays[loop]); } Console.WriteLine(); } public static void ShowArrays2(int[] arrays) { foreach (int each in arrays) { Console.Write("{0}\t", each); } Console.WriteLine(); } }}
C#����������� ������������������
public class Arrays { public static void main(String [] args) { int [] arrays = new int [] { 1, 2, 3, 4, 5 }; showArrays1(arrays); showArrays2(arrays); } public static void showArrays1(int [] arrays) { for (int loop = 0; loop < arrays.length; loop++) { System.out.printf("%d\t", arrays[loop]); } System.out.println(); } public static void showArrays2(int[] arrays) { for(int each : arrays) { System.out.printf("%d\t", each); } System.out.println(); }}
Java����������� ������������������
2차원����������� ������������������ 배열����������� ������������������
C����������� ������������������ #include <stdio.h> void show_arrays(int (*)[5], int len); int main(int argc, char *argv[]) { int arrays[3][5] = { { 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} }; int rows = sizeof(arrays) / sizeof(arrays[0]); show_arrays(arrays, rows);} void show_arrays(int (*arrays)[5], int rows) { int row; for(row=0; row < rows; row++) { int col; for(col=0; col<5; col++) { printf("%d\t", arrays[row][col]); } printf("\n"); } printf("\n");}
using System; namespace Arrays{ class Arrays { public static void Main(string[] args) { int[, ] arrays = new int[,] { { 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15} }; ShowArrays1(arrays); } public static void ShowArrays1(int[,] arrays) { for (int row = 0; row < arrays.GetLength(0); row++) { for (int col = 0; col < arrays.GetLength(1); col++) { Console.Write("{0}\t", arrays[row, col]); } Console.WriteLine(); } Console.WriteLine(); } }}
C#����������� ������������������
public class Arrays { public static void main(String [] args) { int [][] arrays = new int [][] {
{ 1, 2, 3, 4, 5},{ 6, 7, 8, 9, 10},{11, 12, 13, 14, 15}
}; showArrays1(arrays); } public static void showArrays1(int [][] arrays) { for (int row = 0; row < arrays.length; row++) { for(int col=0; col<arrays[row].length; col++) { System.out.printf("%d\t", arrays[row][col]); } System.out.println(); } System.out.println(); }}
Java����������� ������������������