GithubHelp home page GithubHelp logo

leetcode-java's People

Contributors

bibabolynn avatar

leetcode-java's Issues

java传参

基本数据类型传递和对象传递,到底是传值还是传引用?
基本数据类型传递的是一个值的副本,对象传的是引用的副本,即当前对象的另外一个引用
举个栗子
public String change(String s, int i, StringBuffer sb, Person p){
s="123";
i=3;
sb.append("woshi");
p.setAge(100);
sb = new StringBuffer("sbsb");
p = new Person("bb",44);
return s;
}

@Test
public void testChange(){
    StringBuffer sb = new StringBuffer("buff");
    String s = "aaa"; 
    int i = 1;
    Person p = new Person("aa",12);
    i=2;
    change(s,i,sb,p);

// s="222";
System.out.println(s);//aaa
System.out.println(i);//2
System.out.println(sb.toString());//buffwoshi
System.out.println(p);//Person{id=0, name='aa', age=100, Country=null, hashcode=638783031}
}
这里一共测试了String,int,一个对象StringBuffer,一个对象people。让我们来仔细看看这些传递都发生了什么。

int是基本类型,所以int只是将值复制一份给别的方法用
但是对于对象,只是将原来对象的引用复制了一份传递给change(),在change方法里,这份复制的引用指向了另一个对象,但是原来的引用还是指向之前的对象。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.