-
작성된 프로그램의 의미를 결정하는 규칙
-
프로그램이 작성이 되는것만이 아닌 예측한 정확한 결과가 도출 되어야함.
Tip
|
x++; 이라는 코드를 보았을 때 Syntax에 맞게 작성되었지만 x의 type이 허용하는 value의 값을 넘어선다면 Semantic에 어긋나는 코드가 될 수 있다. |
java interpreter가 프로그램을 실행할 때 main() subroutine을 호출.
-
해당 subroutine이 다른 subroutine이 사용되는 방법, 순서를 결정
프로그램이 실행될 때 수행할 작업을 컴퓨터에게 알려주는 Script
optional-package-declaration;
optional-imports;
public class program-name {
optional-variable-declarations-and-subroutines;
public static void main(String[] args) {
statements;
}
optional-variable-declarations-and-subroutines;
}
-
program-name → 클래스 이름
-
optional-variable-declarations-and-subroutines → style, 해당 위치에 variable을 선언하거나 subroutine이 와야하는 자리
- *.java
-
source code
- *.class
-
컴파일되어 생성된 java bytecode
-
해당 항목에 이름을 주는 규칙
-
이름을 사용해서 작업을 하는 규칙
class, variable, subroutine의 이름을 지정
-
camel case
-
addNumber
-
-
compund names - 일종의 경로
-
System.out.println
-
variable에 data를 넣기 위해 사용
variable = expression;
-
java에서 variable에 data을 넣는 유일한 방법
-
byte
-
short → 2byte
-
int → 4byte
-
long → 8byte
-
float → 4byte
-
double → 8byte
-
char → 2byte
-
boolean → true, false
-
상수 value를 variable에 넣는 방법
-
특수 literal
-
char → 두문자이지만 단일 문자로 인식
-
'\n' → linefeed, 줄바꿈
-
'\t' → tab
-
-
-
String
-
문자들의 sequence, "Hello, world!"
-
type-name variable-name-or-names;
-
좋은 style은 한 statement에 한 variable만 선언
-
주석을 사용해서 해당 variable이 어떤 목적으로 사용이 되는지 함께 작성해주는것도 좋은 방법
01. Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.
Syntax = 프로그래밍 언어의 정해져있는 문법, variable을 선언하지 않고 사용
Semantic = 프로그램의 결과가 정확한 결과를 보여주는지에 대한 규칙, MAX_VALUE의 값에 특정한 수를 더했을 때
variable의 memory location에 이름을 붙임
int variable;
04. One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?
해당 statement 알맞은 값인지 확인 할 수 있는 type
true, false
a) ++ → 특정한 variable의 값을 1 증가시키는 operator
b) && → statement와 statement를 하나의 boolean operation으로 이어주는 역할 두 statement가 모두 true여야 true
c) != → 왼쪽의 value가 오른쪽의 value와 다르면 true의 값을 내는 operator
06. Explain what is meant by an assignment statement, and give an example. What are assignment statements used for?
선언되어있는 variable에 data의 값을 넣는 statement
variable = value;
-
static variable과 subroutine을 grouping
-
object를 생성
10. What is the difference between the statement "x = TextIO.getDouble();" and the statement "x = TextIO.getlnDouble();"
getlnDouble() subroutine은 중간 공백을 같이 읽어서 data를 읽어온 이후 나머지 값을 buffer에 저장해 둔다.
11. Explain why the value of the expression 2 + 3 + "test" is the string "5test" while the value of the expression "test" + 2 + 3 is the string "test23". What is the value of "test" + 2 * 3 ?
expression을 실행하는 과정에서 처음 int literal이 등장해서 int type으로 operation을 진행하다가 이후 String literal이
나왔기 때문에 String type으로 type casting이 이루어 졌다.
"test" + 2 + 3 expression은 이미 처음 String literal이 먼저 나와서 String으로 operation을 수행했기 때문에 test23이라는 결과가 나왔다.
12. Integrated Development Environments such as Eclipse often use syntax coloring, which assigns various colors to the characters in a program to reflect the syntax of the language. A student notices that Eclipse colors the word String differently from int, double, and boolean. The student asks why String should be a different color, since all these words are names of types. What’s the answer to the student’s question?
13. What is the purpose of an import directive, such as import textio.TextIO or import java.util.Scanner?
각각의 class에 미리 정의되어져서 존재하는 subroutine을 가져와서 사용하기 위함