변수와 자료형

Java 프로그래밍 - 변수와 자료형_1

public class Main {
    public static void main(String[] args) {

//      1. 변수 사용하기
        System.out.println("== 변수 사용하기 ==");
        int age = 10;
        String country = "Korea";

        System.out.println("age = " + age);
        System.out.println("country = " + country);
        System.out.println();

//      2. 변수 이름 규칙
        System.out.println("== 변수 이름 규칙 ==");
//      2-1. 문자, 숫자, _(underscore), $ 사용 가능
        int apple = 2000;
        int apple3 = 2000;
        int _apple = 2000;
        int $apple = 2000;

        System.out.println("apple = " + apple);
        System.out.println("apple3 = " + apple3);
        System.out.println("_apple = " + _apple);
        System.out.println("$apple = " + $apple);
        System.out.println();


//      2-2. 숫자로 시작 X
//        int 3apple = 2000;


//      2-3. 대소문자 구분
        int apple1 = 2000;
        int Apple1 = 3000;

        System.out.println("apple1 = " + apple1);
        System.out.println("Apple1 = " + Apple1);
        System.out.println();

//      2-4. 공백 사용 X
//        int one apple = 2000;
        int one_apple = 2000;
        int oneApple = 2000;


//      2-5. 예약어 사용 X
//      예약어 예시: true, false, if, switch, for, continue, break, ...
//        int true = 1;
//        int false = 0;
//        int if = 1;
//        int continue = 10;


//      참고) 한글 사용 가능
        int 나이 = 20;
        System.out.println("나이 = " + 나이);

        
//      3. 표기법
        System.out.println("== 표기법 ==");
//      3-1. 카멜 표기법 (camelCase)
//      변수, 함수
        int myAge = 10;
        int oneApplePrice = 1000;

        
//      3-2. 파스칼 표기법 (PascalCase)
//      클래스
        int MyAge = 10;
        int OneApplePrice = 1000;

        
//      참고) 스네이크 표기법 (snake_case)
//      사용 X
        int my_age = 10;
        int one_apple_price = 1000;

    }
}
== 변수 사용하기 ==
age = 10
country = Korea

== 변수 이름 규칙 ==
apple = 2000
apple3 = 2000
_apple = 2000
$apple = 2000

apple1 = 2000
Apple1 = 3000

나이 = 20
== 표기법 ==

Java 프로그래밍 - 변수와 자료형_2

public class Main {
    public static void main(String[] args) {

//      1. 자료형 - 숫자
        System.out.println("== 숫자 ==");
//      1-1. 정수
        int intNum = 10;
        System.out.println("intNum = " + intNum);

        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);
        int intNum2 = Integer.MAX_VALUE;
        int intNum3 = Integer.MAX_VALUE + 1;
        System.out.println("intNum3 = " + intNum3);
        long longNum = (long)Integer.MAX_VALUE + 1;
        System.out.println("longNum = " + longNum);

//      1-2. 실수
        float floatNum = 1.123f;
        System.out.println(Float.MAX_VALUE);
        double doubleNum = 3.4028;
        System.out.println(Double.MAX_VALUE);

//      1-3. 2진수 / 8진수 / 16진수
        int numBase2 = 0b1100;
        int numBase8 = 014;
        int numBase16 = 0xC;
        System.out.println(numBase2);
        System.out.println(numBase8);
        System.out.println(numBase16);

        System.out.println("0b" + Integer.toBinaryString(numBase2));
        System.out.println("0" + Integer.toOctalString(numBase8));
        System.out.println("0x" + Integer.toHexString(numBase16));

//      2. 자료형 - 부울
        System.out.println("== 부울 ==");
        boolean isPass = true;
        boolean isOk = false;

        System.out.println(isPass);
        System.out.println(isOk);


//      3. 자료형 - 문자
        System.out.println("== 문자 ==");
        char keyFirst = 'a';
        char keyLast = 'z';

        System.out.println(keyFirst);
        System.out.println((int)keyFirst);
        System.out.println((int)keyLast);

    }
}
== 숫자 ==
intNum = 10
-2147483648
2147483647
intNum3 = -2147483648
longNum = 2147483648
3.4028235E38
1.7976931348623157E308
12
12
12
0b1100
014
0xc
== 부울 ==
true
false
== 문자 ==
a
97
122

Java 프로그래밍 - 변수와 자료형_3

public class Main {
    public static void main(String[] args) {

//      1. 자료형 - 문자열
        System.out.println("== 문자열 ==");
        String s1 = "Hello World!";
        String s2 = "01234";
        System.out.println("s1 = " + s1);
        System.out.println("s2 = " + s2);

//      1-1. equals
        String s3 = "hi";
        String s4 = "hi";
        System.out.println(s3.equals(s4));
        System.out.println(s3 == s4);
        String s5 = new String("hi");
        System.out.println(s3.equals(s5));
        System.out.println(s3 == s5);

//      1-2. indexOf
        String s6 = "Hello! World!";
        System.out.println(s6.indexOf("!"));
        System.out.println(s6.indexOf("!", s6.indexOf("!") + 1));

//      1-3. replace
        String s7 = s6.replace("Hello", "Bye");
        System.out.println("s7 = " + s7);

//      1-4. substring
        System.out.println(s7.substring(0, 3));
        System.out.println(s7.substring(0, s7.indexOf("!") + 1));

//      1-5. toUpperCase
        System.out.println(s7.toUpperCase());


//      2. 자료형 - StringBuffer
        System.out.println("== StringBuffer ==");
        StringBuffer sb1 = new StringBuffer();
        sb1.append("01234");
        System.out.println("sb1 = " + sb1);
        sb1.append("56789");
        System.out.println("sb1 = " + sb1);

        String a = "01234";
        String b = "56789";
        String bak = a;
        System.out.println(a == bak);
        a += b;
        System.out.println(a == bak);

        StringBuffer sb2 = new StringBuffer("name is");
        sb2.insert(0, "My ");
        sb2.insert(sb2.length(), " JAVA");
        System.out.println("sb2 = " + sb2);
        System.out.println(sb2.substring(0, 2));


//      3. 자료형 - 배열
        System.out.println("== 배열 ==");
        int[] myArray1 = {1, 2, 3, 4, 5};
        System.out.println(myArray1[0]);
        System.out.println(myArray1[1]);
        System.out.println(myArray1[2]);
        System.out.println(myArray1[3]);
        System.out.println(myArray1[4]);

        char[] myArray2 = {'a', 'b', 'c', 'd', 'e'};
        System.out.println(myArray2[2]);

        String[] myArray3 = new String[3];
        myArray3[0] = "Hello";
        myArray3[1] = " ";
        myArray3[2] = "World!";
        System.out.println(myArray3[0] + myArray3[1] + myArray3[2]);

    }
}
== 문자열 ==
s1 = Hello World!
s2 = 01234
true
true
true
false
5
12
s7 = Bye! World!
Bye
Bye!
BYE! WORLD!
== StringBuffer ==
sb1 = 01234
sb1 = 0123456789
true
false
sb2 = My name is JAVA
My
== 배열 ==
1
2
3
4
5
c
Hello World!

Java 프로그래밍 - 변수와 자료형_4

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;

public class Main {
    public static void main(String[] args) {

//      1. 자료형 - 리스트
        System.out.println("== 리스트 ==");
        ArrayList l1 = new ArrayList();

//      1-1. add
        l1.add(2);
        l1.add("hello");
        l1.add(3);
        l1.add(4);
        l1.add("world");
        System.out.println("l1 = " + l1);

        l1.add(0, 1);
        System.out.println("l1 = " + l1);

//      1-2. get
        System.out.println(l1.get(0));
        System.out.println(l1.get(1));

//      1-3. size
        System.out.println(l1.size());

//      1-4. remove
        l1.remove(1);
        System.out.println("l1 = " + l1);

        l1.remove(Integer.valueOf(1));
        System.out.println("l1 = " + l1);

        l1.remove("hello");
        System.out.println("l1 = " + l1);

//      1-5. clear
        l1.clear();
        System.out.println("l1 = " + l1);

//      1-6. sort
        ArrayList l2 = new ArrayList();
        l2.add(5);
        l2.add(3);
        l2.add(4);
        System.out.println("l2 = " + l2);

        l2.sort(Comparator.naturalOrder());
        System.out.println("l2 = " + l2);

        l2.sort(Comparator.reverseOrder());
        System.out.println("l2 = " + l2);

//      1-7. contains
        System.out.println(l2.contains(1));
        System.out.println(l2.contains(3));


//      2. Maps
        System.out.println("== Maps ==");
        HashMap map = new HashMap();

//      2-1. put
        map.put("kiwi", 9000);
        map.put("apple", 10000);
        map.put("mango", 12000);
        System.out.println("map = " + map);

//      2-2. get
        System.out.println(map.get("mandarin"));
        System.out.println(map.get("apple"));

//      2-3. size
        System.out.println(map.size());

//      2-4. remove
        System.out.println(map.remove("hi"));
        System.out.println(map.remove("mango"));
        System.out.println("map = " + map);

//      2-5. containsKey
        System.out.println(map.containsKey("mango"));
        System.out.println(map.containsKey("kiwi"));


//      3. Generics
        System.out.println("== Generics ==");
        ArrayList l3 = new ArrayList();
        l3.add(1);
        l3.add("hello");
        System.out.println("l3 = " + l3);

        ArrayList<String> l4 = new ArrayList<String>();
//        l4.add(1);
        l4.add("hello");

        HashMap map2 = new HashMap();
        map2.put(123, "id");
        map2.put("apple", 10000);
        System.out.println("map2 = " + map2);

        HashMap<String, Integer> map3 = new HashMap<String, Integer>();
//        map3.put(123, "id");
        map3.put("apple", 10000);
        System.out.println("map3 = " + map3);

    }

}
== 리스트 ==
l1 = [2, hello, 3, 4, world]
l1 = [1, 2, hello, 3, 4, world]
1
2
6
l1 = [1, hello, 3, 4, world]
l1 = [hello, 3, 4, world]
l1 = [3, 4, world]
l1 = []
l2 = [5, 3, 4]
l2 = [3, 4, 5]
l2 = [5, 4, 3]
false
true
== Maps ==
map = {apple=10000, kiwi=9000, mango=12000}
null
10000
3
null
12000
map = {apple=10000, kiwi=9000}
false
true
== Generics ==
l3 = [1, hello]
map2 = {apple=10000, 123=id}
map3 = {apple=10000}

Tags:

Categories:

Updated:

Leave a comment