1주차 월요일

KH·2023년 5월 8일
0

사전캠프

목록 보기
1/5
post-thumbnail

https://nbcamp.gitbook.io/java-handbook

Java Basic Concepts

JDK(Java Development Kit)

JRE(Java Runtime Environment)

JVM(Java Virtual Machine)

Hello, World!

public class Hello {

    public static void main(String[] args) {

        System.out.println("Hello, World!");
        //println stands for printline
    }
}

IntelliJ의 경우 에디터에 sout을 입력하면 System.out.println();으로 자동완성해줌.

Variables

A variable provides us with named storage that our programs can manipulate. Java provides three types of variables.

  • Class variables − Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it.

  • Instance variables − Instance variables are declared in a class, but outside a method. When space is allocated for an object in the heap, a slot for each instance variable value is created. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

  • Local variables − Local variables are declared in methods, constructors, or blocks. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.

class Variable1_4 {
    static int classVal = 100; // 클래스 변수
    int instanceVal = 200; // 인스턴스 변수

    public static void main(String[] args) {
        int num; // 지역 변수
        num = 300;
        System.out.println("int = " + num); // 300

        // 인스턴스 변수
        Variable1_4 instance = new Variable1_4 (); // 인스턴스 변수 사용을 위해 객체 생성
        System.out.println("instanceVal = " + instance.instanceVal); // 200

        // 클래스 변수
        System.out.println("classVal = " + classVal);
        // 같은 클래스 내부는 바로 접근 가능
        System.out.println("Variable1_4.classVal = " + Variable1_4.classVal);
        // 클래스명.클래스변수명 으로도 접근가능
    }
}
int = 300
instanceVal = 200
classVal = 100
Variable1_4.classVal = 100

Data Types in Java

Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:

  1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.

  2. Non-primitive(or reference) data types: The non-primitive data types include Classes, Interfaces, and Arrays.

import java.util.Date;
public class DateDemo {

    public static void main(String args[]) {
        // Instantiate a Date object
        Date date = new Date();
        // Here, date is a non-primitive data
        
        // display time and date using toString()
        System.out.println(date.toString());
    }
}

Mon May 08 19:47:48 KST 2023

String Concatenation in Java

In Java, String concatenation forms a new String that is the combination of multiple strings. There are two ways to concatenate strings in Java:

  1. By + (String concatenation) operator
class TestStringConcatenation2{  
 public static void main(String args[]){  
   String s=50+30+"Sachin"+40+40;  
   System.out.println(s);//80Sachin4040  
 }  
}  

Output:
80Sachin4040

Note: After a string literal, all the + will be treated as string concatenation operator.

  1. By concat() method
class TestStringConcatenation3{  
 public static void main(String args[]){  
   String s1="Sachin ";  
   String s2="Tendulkar";  
   String s3=s1.concat(s2);  
   System.out.println(s3);//Sachin Tendulkar  
  }  
}  

Output:
Sachin Tendulkar

profile
What, How, Why

0개의 댓글