공부/Java

기본 문법(3)

KoreanPaper 2026. 1. 21. 14:57

1. 비교 연산자

public static void main(String[] args) {
boolean Kor=false;
boolean Adult=true;

System.out.println(Kor && Adult);  // ANd -> &&는 둘다 T일 때만, T
System.out.println(Kor || Adult);  // OR -> ||는 하나라도 T이면, T


System.out.println(1==0 && 1/0==0);  // &&는 앞에 있는 피연산자가 F이면 뒤를 평가하지 않음(단축평가)
System.out.println(1!=0 || 1/0==0);  // ||은 앞에 있는 피연산자가 T이면 뒤를 평가하지 않음

String str1 = new String("java"); 
String str2 = new String("java");
System.out.println(str1==str2);  // String 안에 텍스트가 저장 되어 있는게 아니라 텍스트 데이터가 있는 주소가 저장되어있음

Scanner sc = new Scanner(System.in);
String t1 =sc.nextLine();
String t2 =sc.nextLine();
System.out.println(t1==t2);  // 다르다고 인식, scanner가 각기 다른 별개의 텍스트로 메모리에 저장, 즉 주소비교
System.out.println(t1.equals(t2));  // 텍스트가 같은지 비교 (내용이 같은지)

char c ='A'; // 원시타입 -> 메모리에 직접저장
System.out.println('A'=='A'); //한글자씩 직접적으로 메모리 저장되는 char는 비교가 보장된다.



}


2. 증감 연산자(전위, 후위)

int b =10;
System.out.println(b); //10
System.out.println(b++); //10
System.out.println(b); //11

int c = b++;
System.out.println(b +""+c); // 12 11

int d = ++b;
System.out.println(b+""+d); // 13 13

 

- 파이썬은 증감 연산자가 없음

3. if문(조건)

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

int c = 100;
if (c < 40) {
System.out.println("c가 40보다 작다");
} else if (c<90) {
System.out.println("c가 90보다 작다");
} else {
System.out.println("아무 조건도 해당하지 않는다.");
}

}


4. switch문(조건)

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int day = sc.nextInt();

switch (day%7) {

case 0:
System.out.println("sun");
break;

case 1:
System.out.println("mon");
break;

case 2:
System.out.println("tue");
break;

case 3:
System.out.println("wen");
break;

default:
System.out.println("해당 없음");
break;
}

}


5. for문(반복)

public static void main(String[] args) {

for(int i =0; i<5; i+=2) {
System.out.println(i);
}
        
-------------------------------------------------------
        
for (int a=5; a>=0; a--) {
System.out.println(a); // '5-a' 으로도 표현 가능
}

}


6. while문(반복)

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String name ="";

while(name.equals("")) {
Systehttp://m.out.print("name: ");
name=sc.nextLine();

if(name.equals("")) {
System.out.println("nothing");
} else {
System.out.println("hi "+name);
}
}

----------------------------------------------------------------
        
int i=0;
while(i<5) {
System.out.println(i);
i++;
}

----------------------------------------------------------------
        
int j =0;
do {
System.out.println(j);
j++;
} while (j<5);

}


7. 여러가지 조건에 의해 끝나는 경우 Or 조건문으로 표현하기 매애한 경우

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
while(true) {
System.out.println("name: ");
String input = sc.nextLine();

if(input.equals("")) {
System.out.println("nothing");
continue;
}
System.out.println("hi "+input);
break;
}
}


8. 배열

public static void main(String[] args) {

int[] numbers; //배열 선언
numbers = new int[5]; // 초기화
numbers[0]=10; // 0번째 자리에 10을 삽입
System.out.println(numbers[0]);

String[] names = new String[10]; // 빈 배열을 선언하고 초기화

int[] ages = {10,20,30};
String[] titles = {"dd","ff","gg"};
System.out.println(titles); // 주소 출력
System.out.println(Arrays.toString(titles)); //데이터 출력

int[][] matrix = new int[5][3]; // 2차원 배열, 4주치의 3일치
int [][] matrix2 = {
{1,2,3},
{0,6,4},
}; //행렬
System.out.println(matrix2[1][2]);
System.out.println(Arrays.toString(matrix2)); //주소 출력
System.out.println(Arrays.deepToString(matrix2)); //데이터 출력

}
    ---------------------------------------------------------------------------
          int[] numbers= {1,2,3,4,5};

              for(int i=0; i<numbers.length; i++) {
                  System.out.println(numbers[i]);
              }

              //향상된 for문
              for(int number : numbers) {
                  System.out.println(number); // 1 2 3 4 5 형태로 출력
}

'공부 > Java' 카테고리의 다른 글

객체지향 프로그래밍 기초  (0) 2026.05.15
기본 문법(4)  (0) 2026.01.21
데이터 타입에 따른 저장 방식  (0) 2024.06.26
BufferReader 와 BufferWriter  (0) 2024.06.26
기본 문법(2)  (0) 2024.06.25