我是 Java 的初学者,最近遇到了一个关于 Java String 概念的面试问题:
public class Test1 {
public static void changeStr(String str){
str="welcome";
}
public static void main(String[] args) {
String str="1234";
changeStr(str);
System.out.println(str);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为输出应该是“welcome”,但是,我在 Eclipse 中对其进行了测试,它显示“1234”,Java 字符串不是引用,因此在方法 changeStr 中将 Java 字符串“str”引用更改为“welcome” ?
请原谅我的初学者问题!
我有一个方法来返回文件夹路径的字符串.CSVFile方法调用另一个方法ConfigFilePath从配置文件中提取值并将其返回以用于另一个条件.
public CSVFile(ManagedElement_T[] meInfo) {
String path = null;
ConfigFilePath(path);
system.out.print("CSV "+path);
}
public String ConfigFilePath(String path) {
final String directory = System.getProperty("user.dir");
final String filename = "config.properties";
final File configFile = new File(directory+"/"+filename);
final Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configFile);
prop.load(input);
path = prop.getProperty("diretory_csv");
System.out.println("PATH " + path);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// Close the file
}
File checkPath = new File(path.toString());
System.out.println("CHECK " + …Run Code Online (Sandbox Code Playgroud) 我理解在将数组asn参数传递给函数,并对函数内部的数组元素进行一些更改时,更改也将反映在调用函数中,因为数组直接在内存上运行(通过引用调用)
但是,为什么同样的行为不适用于字符串?因为String基本上是一个字符数组,所以我也期待Strings以同样的方式工作.
请参阅下面的代码.我将一个String(字符数组)以及一个int数组传入函数并对其进行一些更改.在main中打印这些时,我看到String保持不受影响,而反映了对数组的更改.
import java.util.Arrays;
public class TestString
{
public static void main(String[] args)
{
String s = "hello";
int[] a = new int[5];
Arrays.fill(a, -1);
fun(s,a);
System.out.println(s);
System.out.println(a[0]);
}
static void fun(String s, int[] a)
{
s = "world";
a[0] = 99;
}
}
Run Code Online (Sandbox Code Playgroud)
产量
hello
99
Run Code Online (Sandbox Code Playgroud) 我有一个为Fraction对象编写的类.我需要交换两个Fraction对象,而不是对象的内容.这是我的代码.
public void fswap(Fraction other){
Fraction temp = other.copy();
other = this;
this = temp;
Run Code Online (Sandbox Code Playgroud)
copy()方法返回与调用它的对象相同的Fraction对象.此代码的最后一行在我的IDE中抛出一个错误"无法为最终变量赋值".任何帮助是极大的赞赏.
我试图在定义的类的数组中将所有元素设置为null.我刚学会了for-each循环的用法,所以我尝试了以下内容:
for(MyClass element:array){
element=null;
}
Run Code Online (Sandbox Code Playgroud)
但是这在编译后不起作用,并且有一个警告"不使用局部变量元素的值".我尝试了一个正常的for循环,它按预期工作:
for(int i=0;i<array.length;i++){
array[i]=null;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么for-each循环不起作用?我对它的用法有误解吗?
编辑:: 更新代码以显示我关注的问题:更改 Frag 中 mData 的属性会更改 Sender 中的属性 mData 并且它们的内存引用相同..
我正在通过包将自定义数据类传递到子视图中,目的是将数据返回给父视图。因此,我的数据对象作为引用传递很方便,因此可以直接操作。
然而,来自 iOS,这当然是反对函数式编程的。我在这里主要只是好奇,我是否需要做更多的事情来实现 Serializable?或者是打算让 putSerializable 序列化引用,而不是值?
class Sender{
DataClass mData = new DataClass();
void openFragment(){
Bundle bundle = new Bundle();
bundle.putSerializable("Key", mData);
Frag frag = new Frag(); //@6945
//EDIT for test
{
testForEquality(bundle);
}
frag.setArguments(bundle);
pushFragment(frag);//Makes a new view appear on top
}
void testForEquality(Bundle bundle){
DataClass newData = (DataClass) bundle.getSerializable("Key");
Log.e("Equal", "" + (newData == mData); //TRUE
}
}
class Frag{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle …Run Code Online (Sandbox Code Playgroud) public class Account {
public double balance ;
public double deposite;
public double withdraw;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getDeposite() {
balance = balance + deposite;
return deposite;
}
public void setDeposite(double deposite) {
this.deposite = deposite;
}
public double getWithdraw() {
return withdraw;
}
public void setWithdraw(double withdraw) {
this.withdraw = withdraw;
if(withdraw <= balance){
balance = balance …Run Code Online (Sandbox Code Playgroud) /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava;
/**
*
* @author Administrator
*/
public class CoreJava {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Test test=new Test();
test.setx(20);
test.printx();
Test test1=test;
test1.setx(40);
test1.printx();
test.printx();
int a=20;
int b=a;
a=40;
System.out.println(a);
System.out.println(b);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
run:
X is 20
X is 40
X is 40
40
20
Run Code Online (Sandbox Code Playgroud)
现在,当我在“a”中设置“x”的值并将对象“a”分配给“b”时,“b”将指向对象“a”的相同值。因此对“a”的任何更改也会更改“b”的值;
但是当我使用简单变量时,为什么它不像对象那样做。为什么对象存储在堆中,变量存储在栈区。
Class Player {
Player setName(String name){
this.name = name;
return this;
// or
void setName(String name){
this.name = name;
}}
Run Code Online (Sandbox Code Playgroud)
你好.如果我使用"void"或"return this"语句使用该方法有什么区别?为什么"返回此"语句存在,如果它也一样?
我想通过Unity学习C#,我想知道C#在传递参数或返回值的按引用或按值传递时是否具有与Java相同的语义?
我在C#语言中看到,您可以使用它ref来指示某些东西是通过引用传递的,这是否意味着默认情况下它是通过值传递的?
默认情况下,除非引用是原始类型,否则Java默认通过引用传递(即,不从扩展的东西Object)
这包括Java中的数组。但是我不确定C#。
有人可以向我解释这个,因为我对Java相对较新并且来自Objective-c背景(不同的是,通过引用传递而不是通过值传递(引用)).
鉴于Java是按值传递的,我不应期望能够在不同的范围内重新分配变量并保持这些更改,对吧?这就是为什么objectValue为null.String和int是原语,这就是它们工作的原因.但是,为什么Integer有效呢?
Object objectValue = null;
int intValue;
String stringValue = null;
Integer integerValue = null;
if (true) {
objectValue = new Aircraft();
intValue = 1;
stringValue = "is one";
integerValue = new Integer(1);
}
// objectValue == null
// intValue == 1
// stringValue == "is one"
// integerValue == 1
Run Code Online (Sandbox Code Playgroud) public class ggg
{
static int y=0;
static int x;
static String h;
public static void main(String [] args)
{
String s = "hadoyef";
x = s.length();
System.out.println(s);
reverse(s);
System.out.println(s);
}
public static String reverse(String s){
if (s.length() == 1){
//System.out.print(s);
h = h + s.substring(0,1);
s=h;
System.out.println(s);
return s;
}
else{
h = h + s.substring(s.length()-1,s.length());
return reverse (s.substring(0, s.length()-1));
//System.out.print(s.substring(0,1));
}
}
}
Run Code Online (Sandbox Code Playgroud)
请帮帮我,我不明白为什么s = h; 部分不起作用.忽略这里它让我发布更多的细节和idk说什么,所以我只是絮絮叨叨,直到它的工作感谢无论谁帮助.