bibabolynn / leetcode-java Goto Github PK
View Code? Open in Web Editor NEWjava leetcode algorithm
java leetcode algorithm
基本数据类型传递和对象传递,到底是传值还是传引用?
基本数据类型传递的是一个值的副本,对象传的是引用的副本,即当前对象的另外一个引用
举个栗子
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方法里,这份复制的引用指向了另一个对象,但是原来的引用还是指向之前的对象。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.