✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

Parameter

  1. Primitive parameter : read only

class Data{int x;}

class Ex6_6 {
    public static void main(String[] args) {
        Data d = new Data();
        d.x = 10;
        System.out.prinln("main() : x = " +d.x);
        
        change(d.x);
        System.out.println("After changed(d.x)");
        System.out.println("main() : x =" + d.x);//x값 10 불변.
        
    }

    static void change(int x) {//primitive type : read only
        x = 1000;//only exist when change method is being executed
        //after change execution, change method and local variable in change method disappear from call stack
        System.out.println("change() : x = " + x);
    }
}

2. Reference parameter : read & write

class Data{int x;}

class Ex6_6 {
    public static void main(String[] args) {
        Data d = new Data();
        d.x = 10;
        System.out.prinln("main() : x = " +d.x);
        
        change(d.x);
        System.out.println("After changed(d.x)");
        System.out.println("main() : x =" + d.x);//x값 10 불변.
        
    }

    static void change(Data d) {//REFERENCE type : read and write
        x = 1000;//only exist when change method is being executed
        //after change execution, change method and local variable in change method disappear from call stack
        System.out.println("change() : x = " + x);
    }
}
  • Reference type return

class Data {int x;}

class Ex6_8 {
    public static void main(String[] args) {
        Data d = new Data();
        d = 10;
        
        Data d2 = copy(d);
        
        System.out.println("d.x = " + d.x);
        System.out.println("d2.x = " + d2.x);
    }
    
    static Data copy(Data d) {//return type and parameter type are reference type
        Data tmp = new Data();
        tmp.x = d.x;
        
        return tmp;
    }
}

reference return type means return the address of object so that after copy method is ended, the d2 from copy method can be used in main method.

The two static methods, void main() {..} and Data copy(){..} are in same class. There is no reference type variable to get something from copy method. The reasons are 1.copy method is static method. 2. main and copy methods are in same class.

Static method can be used without creating object(reference type variable). Other normal method is used like below.

//원래는 이렇게 해주어야하지만 생략 가능
Ex6_8 e = new Ex6_8();
e.copy();

MyMath mm = new MyMath();
mm.add(x,y);

class Ex6_4 {
	public static void main(String args[]) {
		//그냥 호출해서 변수에 저장하면 안 되나? 왜 객체를 생성해서 사용하지?
		//메소드(클래스)에 접근 및 사용하려면 그 클래스는 참조변수 같은 개념이기 때문에 객체가 있어야 .을 통해 접근하고 사용할 수 있다.
		
		//2.MyMath 객체 생성 
		MyMath mm = new MyMath();
		//3.MyMath 객체 사용(객체의 메소드 호출)
		long result1 = mm.add(5L, 3L);
		long result2 = mm.subtract(5L, 3L);
		long result3 = mm.multiply(5L, 3L);
		double result4 = mm.divide(5L, 3L);

		long resultmax = mm.max(5L, 3L);
		long resultmin = mm.min(5L, 3L);
		
		System.out.println("add(5L, 3L) = " + result1);
		System.out.println("subtract(5L, 3L) = " + result2);
		System.out.println("multiply(5L, 3L) = " + result3);
		System.out.println("divide(5L, 3L) = " + result4);
		System.out.println("max(5L, 3L) = " + resultmax);
		System.out.println("min(5L, 3L) = " + resultmin);
		
		mm.printGugudan(9);
	}
 }
//메소드는 클래스다.
//메소드는 클래스 영역에만 정의할 수 있다!!!!!!!
//1. 메소드 작성 - MyMath 클래스 작성 
 class MyMath {
	 void printGugudan(int dan) {
		 if(!(2<=dan && dan <=9)) {
			 System.out.println("2와 9사이의 정수를 입력해주세요.");
			 return;
		 }
		 
		 for(int i=1;i <=9; i++) {
			 System.out.printf("%2d X %2d = %2d%n",dan,i,dan*i);
		 }
	 }
	 long max(long a, long b) {
		 long result = 0;
		 result = a > b ? a : b;
		 
		 return result;
		 //return result = a > b ? a: b;
	 }
	 long min(long a, long b) {
		 long result = 0;
		 result = a < b ? a : b;
		 
		 return result;
		 //return result = a > b ? a: b;
	 }
	long add(long a, long b) {
		long result = a + b;
		return result;
	//	return a + b;	// ���� �� ���� �̿� ���� �� �ٷ� ������ �� �� �ִ�.
	}
	
	long subtract(long a, long b) { return a - b; }
	long multiply(long a, long b) { return a * b; }
	double divide(double a, double b) {
		return a / b;
	}
 }
PreviousCall StackNextStatic method(Class method) and Instance method

Last updated 4 years ago

Was this helpful?

Primitive type parameter
Reference type parameter
Reference type return