int[] Arrayname = new int[size] // 생성과선언을 한번에 한 케이스
int[] Arrayname[]// 데이터타입에 []을 넣는다.
int Arrayname[];//생성 C언어처럼 해도 되긴함
Arrayname = new int[size] //선언 new를 써서 객체를 생성
// 배열선언
int[] arr;
float[] arr;
point[] arr; 객체 참조형으로 배열 선언
// 배열 생성
int[] arr; // int 형으로 배열 선언
arr = new int[size]; //배열변수에 쿠기를 명시하여 객체 생성
int[] intArr = {10, 20, 30, 40, 50}; // 정수 배열(크기 5)
String[] weekday = { "sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday" }; // 문자열 배열(크기 7)
int[] intArr;
int = {10,20,30,40,50}; // 에러 난다. 선언과 동시에 초기화를 하든가 해야함
int[] intArr;
intArr = new int[]{10,20,30,40,50}; //이미 선언된것 쓸려면 이렇게 해야함
ArrayName[index] = value;
Arr[2] = 50;
public class PrimitveArrayExample{
public static void main(String[] args){
char[] Arr = {'A','B','C'};
for(int i = 0; i<Arr.length; i++) {
System.out.print(Arr[i] + "\t"};
}
}
}for(char data : Arr){
System.out.print(data + "/t")
} 
참조 타입
import java.awt.Point;
public class refernece{
public static void main(String[] args){
Point[] arr = {
new Point(0, 1), new Point(7, 6), new Point(9, 6),
new Point(0, 8), new Point(2, 3)
};
for(int i =0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
}

import java.util.Arrays;
public class ArrayReferenceExample {
public static void main(String[] args) {
int[] array1 = { 2, 3, 5, 7, 11, 13, 17, 19 };
int[] array2;
System.out.println(Arrays.toString(array1));
array2 = array1;
array2[0] = 0;
array2[2] = 2;
System.out.println(Arrays.toString(array1));
}
}

import java.util.Arrays;
public class ArrayCopyExample {
public static void main(String[] args){
int source[] = {1,2,3,4,5,6};
int destination[] = {9,8,7,6,5,4,3,2,1,0};
System.arraycopy(soure,1,destination,3,4);
System.out.println(Arrays.toString(destination));
}
}
// source배열에서 1번 요소부터 destination배열의 3번쨰 요소를 시작으로 4를 복사한다.
// {9,8,7,2,3,4,5,2,1,0}
arr[num] = inputData;
num++;
for(int =0; i<arr.length; i++){
System.out.print(arr[i] +' ');
}
for(int i =0; i<count; i++){
if(arr[i] == searchData){
System.out.printlln(i + "위치에서 데이터 찾음");
}
}
arr[index] = updateData;
int count = arr.length
int index = 2 // 삭제해야하는 데이터의 인덱스
for (int i = index; i < count -1; i++){
arr[i] = arr[i+1];
}
arr[count] = 0;
count-;
int[][] arr1 = new int[3][5]; //2차월 배열
int[][] arr1 = {{1,2,3},{2,3,4},{5,6,7},{7,8,9}}; // 3행 4열
int[][] arr2 = new int[3][];
arr2[0] = new int[4];
arr2[1] = new int[5];
arr2[2] = new int[7];
int[][] intarr = new int[][10] // 에러 생성안된다.
int[][] arr1 = {{1,2,3},{2,4,6,8,10},{1,2,3,4,5,6,7}};

int[][] arr = new int[3][5];
for(int i=0; i<arr.length; i++) { //length은 속성이기에 ()을 쓰지 않는다.
for(int j=0; j<arr[i].length; j++) {
arr[i][j] = i*j;
}
}
Pencil 클래스로 표현 가능class Account {
String name;
String accountNumber;
long balance;
void deposit(long amount) {
balance += amount;
}
void withdraw(long amount) {
balance -= amount;
}
long getBalance() {
return balance;
}
}
class 클래스명 {
변수 선언;
생성자 선언;
메서드 선언;
}
클래스명 변수명 = new 생성자();
class Pen {
String color;
}
public class Main {
public static void main(String[] args) {
Pen myPen = new Pen(); // 객체 생성
System.out.println(myPen);
}
}
멤버 변수에는 접근 제한자를 사용할 수 있다.
| 접근 제한자 | 설명 | 접근 가능 범위 |
|---|---|---|
public | 어디서든 접근 가능 | 모든 클래스에서 사용 가능 |
protected | 같은 패키지 + 상속받은 클래스 접근 가능 | 패키지 내부 + 자식 클래스 |
| (default) | 같은 패키지 내에서 접근 가능 | 패키지 내부 |
private | 해당 클래스 내부에서만 접근 가능 | 클래스 내부에서만 사용 가능 |
class Pen {
public String brand;
private String color;
public void setColor(String newColor) {
color = newColor;
}
public String getColor() {
return color;
}
}
메서드는 객체의 행동을 정의하는 함수이다.
class Pen {
void write() {
System.out.println("펜으로 글을 씁니다.");
}
}
public class Main {
public static void main(String[] args) {
Pen myPen = new Pen();
myPen.write(); // 메서드 호출
}
}
class Pen {
String color = "black";
int price;
void write(int count) {
int data = 1;
System.out.println("멤버변수: " + this.color);
System.out.println("지역변수: " + data);
System.out.println("매개변수: " + count);
}
}
인스턴스 변수
static 없이 선언된 변수class Example {
int instanceVar = 10;
}
public class Main {
public static void main(String[] args) {
Example obj = new Example();
System.out.println(obj.instanceVar);
}
}
클래스 변수
static이 붙어 있는 변수class Example {
static int staticVar = 20;
}
public class Main {
public static void main(String[] args) {
System.out.println(Example.staticVar);
}
}