Learning Java-method&memory model

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.

// 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.

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值