1. Java Language Basics
모든 언어를 배우면 처음에 하는 Hello world 부터 프린트해볼거다
자바의 특징 4가지
- Case-sensitive
- Curly braces come in pairs
- Each JAVA program(source file) should contain a class having the same name as the file name
- The main method is the start of every Java program.
1. Case-sensitive
자바는 일단 case sensitive한 언어라서 대소문자 구분이 중요하다.
먼저 class 를 define할때 class 이름을 대문자로 시작한다. 또 string define 할때도 string 이름 대문자로 쓴다.
암튼 대소문자 구분 아주 중요함
2. Curly braces come in pairs
모든 언어가 그렇듯 대괄호가 항상 짝이 있다 {} 이렇게
3. Each JAVA program(source file) should contain a class having the same name as the file name
이거 중요한데 컴퓨터에 저장되어있는 (내가 설정한)파일 이름이랑 class이름이 같아야한다. 아니면 안돌아감.
예를들면 너가 파일을 prac.java라고 지정해놨으면 class 이름도 class prac{} 로 해야한다. 이때 보통 파일 이름은 대문자를 사용한다.
4. The main method is the start of every Java program.
메인은 무조건 public static void main(String[] args) {}로 시작하기로 한다. 저게 뭔뜻인진 밑에서 설명할예정
그리고 메인은 항상 class 안에 있다
즉 JAVA 에서 hello world 는 다음과같이 print 하면 된다. System.out.println 같은 statement 뒤에는 꼭 ; 이 들어간다.
class prac{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
주석처리는 c와 비슷하게 /* */로 하면된다. 한줄 주석도 된다 // 이렇게
모든 언어가 그렇듯 적은 코드가 뭔코든지 주석처리해주면 좋다
자바 컴파일/실행 과정
Variable (변수)
- Data를 저장할때 쓰는것
- 변수마다 이름을 정해줘야한다. 뜻있는 이름으로.. 초반에 귀찮아서 아무이름이나 했다가 나중에 뭔변순지 헷갈리는거 한두번이 아니다
- 변수를 쓰기 전에 declare 해야한다
How to declare Variables?
variable type은 c랑 거의 똑같다
type | description | range |
boolean | true or flase | true, false |
byte | twos-complement integer | -128 to 127 |
char | Unicode character | ASCII value 0 to 255 |
short | twos-complement integer | -32768 to 32767 |
int | twos-complement integer | -2147483648 to 2147483647 |
long | twos-complement integer | -9223372036854755808 to 922337203685475587 |
float | IEEE 754 floating point | up to 7 decimal digits |
double | IEEE 754 floating point | up to 16 decimal digits |
type variablename; 이 format으로 declare하면 된다
예를들면 이렇게
class prac
{
public static void main(String[] args)
{
int i;
int n;
for (i = 0 ; i < 10 ; i++){
for (n = 0 ; n < i; n++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
}
for문은 그냥 재미로 돌려본거니 무시하고 int i; i=0처럼 declare하면됨
class prac
{
public static void main(String[] args)
{
int i;
i=5;
System.out.println(i);
}
}
이렇게 variable값을 print할수도 있다
Reading user input
- scanner object
- 일단 scanner를 import 하기
import java.util.Scanner;
- create a scanner object from the Scanner class to read input from keyboard (stdin)
Scanner scanner = new Scanner(System.in);
Operator Precedence & Associativity
Escape Sequences
Common Escape Sequences | Descriptions | |
\n | Insert a newline character | |
\t | Insert a tab | |
\" | Insert a double quotation character " | print 할때 " 적고싶으면 \"라고 적어야됨. System.out.println("sss\""); 이렇게 적으면 sss"가 나올거임. |
\' | Insert a single quotation character ' | |
\\ | Insert a backslash character \ | \ 넣으려면 \ 두번 쳐야됨 |
Variable naming
Rules | Notes |
Names are case-sensitive. | - E.g., "area" and "Area" are two different variables. - We prefer "area" than "Area". |
A name can only start with: an alphabet, an underscore "_", or a dollar sign "$". Subsequent characters can only be: alphabets, digits, "_", or "$". |
Valid names: “cat", "_cat_", "$cat", “cat3"… |
Invalid names: "1st_cat", “cat+dog", "^_^" , "many cats"… |
|
We prefer starting with a lower-case alphabet, e.g., “cat" instead of “Cat". | |
A variable name cannot be a keyword or a reserved word. | - E.g., cannot use double, void, and class.
|
In Java, we prefer using the Camel Case. http://en.wikipedia.org/wiki/CamelCase. | E.g., instead of using "manycatsanddogs" or "many_cats_and_dogs", we prefer using "manyCatsAndDogs". |
i++ and ++i
++i
- increase value of i by 1
- The value of the expression "++i" is the value of i after the increment operation.
I++
- increase value of i by 1
- The value of the expression "i++" is the value of i before the increment operation.