728x90
반응형

1. 기본 자료형
- boolean - 참 또는 거짓을 저장하는 자료형
- char - 문자를 저장하는 자료형
- byte, short, int, long - 정수를 저장하는 자료형
- float, double - 실수를 저장하는 자료형
2. 변수값 출력
public class Code_02 {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("a = "+a); //""(따옴표 안에 내용을 그래로 출력
System.out.println("b = " +b); // '+' 기호는 연결하라는 의미가 있다.
}
}
결과값
a = 10
b = 20
public class Code_01 {
public static void main(String[] args) {
int a = 10 , b = 20;
System.out.println(a);
System.out.println(b);
a = a + 1;
b = b + 1;
System.out.println(a); //변수에 저장된 값을 출력하는 코드
System.out.println(b);
}
}
//결과값
10
20
11
21
public class Code_03 {
public static void main(String[] args) {
System.out.println("Hellp");
System.out.println("World");
System.out.println("Java coding is fun");
}
}
//결과값
Hellp
World
Java coding is fun
public class Code_03_2 {
public static void main(String[] args) {
System.out.print("Hellp");
System.out.print("World");
System.out.print("Java coding is fun");
}
}
//결과값
HellpWorldJava coding is fun
- System.out.println()은 '엔터'의미가 들어가 결과값에 줄바꿔서 출력이 되고,
System.out.print()은 '엔터'의미가 없어 그래도 줄바꿈 없이 출력이 된다.
public class Code_04 {
public static void main(String[] args) {
int a = 100;
int b = 250;
System.out.println(a);
System.out.println(b);
System.out.println(a+b);
System.out.println("-------------");
System.out.println("a");
System.out.println("a + b");
}
}
//결과값
100
250
350
-------------
a
a + b
- 큰따옴표를 사용하면 그 안의 내용을 그대로 출력하라는 의미, 쌍따옴표와 쌍따옴표 사이에 '+'기호를 적으면 '연결'의 의미가 있다.
public class Code_05 {
public static void main(String[] args) {
System.out.println("Hello" + "World");
System.out.println("Hello" + " " + "World");
}
}
//결과값
HelloWorld
Hello World
- ""값은 공백을 의미해서 두 번째 결과값은 한 칸이 띄어서 출력이 된다.
public class Code_06 {
public static void main(String[] args) {
int a = 10, b = 27;
System.out.println("a is " + a);
System.out.println("b is " + b);
}
}
//결과값
a is 10
b is 27
- '+'를 ""와 변수를 섞어서 사용할 때도 '+'는 연결의 의미를 가진다.
public class Code_07 {
public static void main(String[] args) {
int a = 10, b = 27;
System.out.println("a + b is " + a + b);
System.out.println("a + b is " + (a+b));
}
}
//결과값
a + b is 1027
a + b is 37
- (a+b)처럼 숫자 둘을 묶어서 더하기 연산을 하면 덧셈의 의미가 된다.
public class Code_08 {
public static void main(String[] args) {
int a = 33, b = 25;
System.out.println(a + " hello");
System.out.println(a + " hello " + b);
System.out.println(a + b + " hello ");
System.out.println(a + b + " hello " + a + b);
System.out.println(a + b + " hello " + (a + b));
}
}
//결과값
33 hello
33 hello 25
58 hello
58 hello 3325
58 hello 58
- 문자열과 숫자열을 연결해줄 때도 '+'를 사용한다.
3. 기본 자료형
public class Code_09 {
public static void main(String[] args) {
boolean x = true;
boolean y = false;
System.out.println(x);
System.out.println(y);
x = 5 > 10;
System.out.println(x);
y = 5 < 10;
System.out.println(y);
}
}
//결과값
true
false
false
true
- boolean 변수는 true 또는 false 값만을 저장한다.
ublic class Code_10 {
public static void main(String[] args) {
char c1 = 'A', c2 = 'a', c3 = 'B', c4 = 'b';
System.out.println((int)c1);
System.out.println((int)c2);
System.out.println((int)c3);
System.out.println((int)c4);
}
}
//결과값
65
97
66
98
public class Code_11 {
public static void main(String[] args) {
char ch = '가';
System.out.println(ch);
System.out.println((int)ch);
}
}
//결과값
가
44032
- 변수 앞에 int라고 적으면 문자는 아스키코드를 적용해 숫자로 표현된다.
public class Code_12 {
public static void main(String[] args) {
char x = 'A';
x++;
System.out.println(x);
}
}
//결과값
B
- 문자는 실제로 저장될 때 숫자로 저장되기 때문에 +또는 -연산을 할 수 있다.
public class Code_13 {
public static void main(String[] args) {
byte b = 126;
System.out.println(b);
b++;
System.out.println(b);
b++;
System.out.println(b);
byte c = -126;
System.out.println(c);
c++;
System.out.println(c);
c++;
System.out.println(c);
}
}
//결과값
126
127
-128
-126
-125
-124
- byte 자료형 크기의 범위는 -128~127이므로 127에서 추가하면 -128이 출력된다.
public class Code_14 {
public static void main(String[] args) {
short s = 32767;
System.out.println(s);
s++;
System.out.println(s);
s++;
System.out.println(s);
short s1 = -32767;
System.out.println(s1);
s1++;
System.out.println(s1);
s1++;
System.out.println(s1);
}
}
//결과값
32767
-32768
-32767
-32767
-32766
-32765
------------------------------------
public class Code_15 {
public static void main(String[] args) {
int i = 2147483647;
System.out.println(i);
i++;
System.out.println(i);
int i1 = 2147483647;
System.out.println(i1);
i1++;
System.out.println(i1);
}
}
//결과값
2147483647
-2147483648
2147483647
-2147483648
-----------------------------------
public class Code_16 {
public static void main(String[] args) {
long l = 2147483647;
System.out.println(l);
l++;
System.out.println(l);
}
}
//결과값
2147483647
2147483648
- byte와 마찬가지로 short, int, long 자료형들도 범위가 정해져 있다.
public class Code_17 {
public static void main(String[] args) {
float f1 = 5.3F;
float f2 = 3.775f;
float f3;
f3 = f1 + f2;
System.out.println(f3);
}
}
//결과값
9.075001
---------------------------------------
public class Code_18 {
public static void main(String[] args) {
double d1 = 5.3;
double d2 = 3.775;
double d3;
d3 = d1 + d2;
System.out.println(d3);
}
}
//결과값
9.075
- float를 저장할 때는 반드시 F 또는 f를 숫자 뒤에 붙여야 한다.
- double 자료형은 float보다 더 큰 범위까지 실수를 사용할 수 있다.
4. 리터럴(literal) 상수
- 코드에서 숫자가 직접 적혀 있을 때 리터럴 상수라고 부른다.
public class Code_19 {
public static void main(String[] args) {
int a = 10;
int b = 0b1010;
int c = 012;
int d = 0xA;
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("d: " + d);
}
}
//결과값
a: 10
b: 10
c: 10
d: 10
- 코드에 리터럴로 2진수의 숫자를 사용하려면 숫자 앞에 0b 또는 0B를 붙여주고,
8진수는 숫자 앞에 (0: 숫자 0)을 붙이며, 16진수는 0x 또는 0X를 붙여준다.
public class Code_20 {
public static void main(String[] args) {
String myName = "Peter Pan";
String yourName ="홍길동";
System.out.println("I am " + myName + ".");
System.out.println("You are " + yourName + ".");
}
}
//결과값
I am Peter Pan.
You are 홍길동.
- String 타입으로 선언하고 데이터는 반드시 큰따옴표("...")를 이용해야 한다.
5. 주석
/*작성일 : 2022.09.23
작성자 : 홍길동
버전 : 1.0 */
public class Code_21 {
public static void main(String[] args) {
int no;
no = 10; // 한줄 주
System.out.println("no: " + no);
}
}
//결과값
no: 10
- 주석은 코드에서 실제로 코드 수행에 영향을 주지 않고. 코드에 대한 추가 설명을 붙여 놓고자 주석을 사용할 수가 있다.
- 한 줄 주석 : // ,
- 여러 줄 주석 : /*.....*/
728x90
반응형
'Framwork > Java기초' 카테고리의 다른 글
| Chapter 6. 메소드 (0) | 2022.10.26 |
|---|---|
| Chapter 5. 배열과 문자열 (2) | 2022.10.23 |
| Chapter 4. 조건문과 반복문 (0) | 2022.10.09 |
| Chapter 3. 계산을 위한 연산자 (0) | 2022.10.04 |
| Chapter 1. 자바 시작하기 (6) | 2022.09.21 |