Learning Java Day02 - 副本

Learning Java Day02

6. Method

​ Method: a piece of code associated with a class or object to perform a task.

Benefit: improve the reusability and readability of the code.

6.1 how to define a method

The format:

method modifier + return type modifier +method name ( type modifier +arguments ,…) {

​ //method’s context;

​ return +return value;

}

public class Getmax {
    public static void main(String[] args) {
        int a = 33;
        int b = 66;
        int max = getMax(a,b);//call the "getMax" method to get the maximum
        System.out.println(max);// print the maximum
    }   
    
    public static int getMax(int i,int j){// define the Getmax method
        if(i>j){
            return i;
        }else
            return j;
    }
}

note:

  • the return’s value and the arguments can be the reference data-type. Such as Classes, Interface, arrays, Strings,etc.
  • the return’s type can be void.
  • the construct method does not have any return’s type(include the “void”) .

6.2 method’s overload

method’s overload: in a class ,there are some methods which have same name; their args have same number ,type and order. We call it overload. As bellows shows:

public class Add3 {
    public static int add1(int a,int b,int c){
        return a+b+c;
    }
    public static double add1(double double b,double c){
        return a+b+c;
    }
}

benefit of overload: we only remember one method’s name can solve different issue.

note: Overload is not related to the return value’s type.

扫描二维码关注公众号,回复: 5621850 查看本文章
// this is not the method overload.
// this is not the method overload.
public class Add3 {
    public static int add1(int a,int b,int c){
        return a+b+c;
    }
    // this is not the method overload.
    public static double add1(int a,int b,int c){
        return (double)(a+b+c);
    }
}

7. java memory model

​ Heap Memory, stack memory, Method Area.

  • VM stack: While running methods the results and local variables are stored in the stack memory.

    Q: when and how to define the memory of stack?

    A: You can define the size of your stack in linking time.As I know, windows app default stack size is 2MB. You can change the size of stack in your project setting. But when App is built, stack size is fixed.And OS would set guard page for stack overflow. If any operation try to access the guard page would trigger EXCEPTION.

  • heap: The Java objects are stored in this area. and after JDK7, the runtime constant pool is store in the heap,too.

  • method area: the .class file will be loaded to this area. This area stores the non-static method() and variable in classes.

  • native Method Stack

  • Program Counter Register

8. Object oriented

class

To Create a Object:

​ Format: ClassName + objectName = new ClassName(); // ClassName() is the defaut Non-parametric constructors method

成员变量&局部变量

  • 在代码位置不同:成员变量:类中方法外,且未被static修饰;局部变量:方法中
  • 内存位置不同:成员变量:堆内存;局部变量:栈内存
  • 默认值不同: 成员变量:有默认值(数值类默认值为0/0.0; 字符:空格’ ', String/数组/接口: null); 局部变量:没有默认值。
  • 生命周期不同: 成员变量: 随对象出现而出现; 局部变量:随方法入栈而出现,方法出栈销毁。

enum&String is a kind of class.

.

猜你喜欢

转载自blog.csdn.net/weixin_39749664/article/details/88750079