如果字符串对象被中断,那么为什么改变一个不会影响其他对象
public class EqualExample {
public static void main(String[] args) {
String str = new String("Hello");
String str1 = new String("Hello");
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
Run Code Online (Sandbox Code Playgroud)
上述程序的输出将是
错误的
public class EqualExample {
public static void main(String[] args) {
String str = "Hello";
String str1 = "Hello";
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
的确如此
这是因为在字符串池中Heloo alredy存在所以它实际上是字符串并引用相同的对象然后为什么如果我将str1更改为"heloo java"然后为什么str仍然具有值"heloo".因为它们引用相同的对象所以str的值必须更改为public class EqualExample {public static void main(String [] args){
String str = "Hello";
String str1 = "Hello";
System.out.println(str == str1);
System.out.println(str1.equals(str));
str1="Heloo java"; …Run Code Online (Sandbox Code Playgroud) 由于字符串是 dotnet 中的 ref 类型,当我们更新变量 x 时,y 中也应该有更新?(因为它是 x 的参考值)。下面给出的示例程序,当 x 更新时 y 的值如何不改变?
public void assignment()
{
string x= "hello";
string y=x; // referencing x as string is ref type in .net
x = x.Replace('h','j');
Console.WriteLine(x); // gives "jello" output
Console.WriteLine(y); // gives "hello" output
}
Run Code Online (Sandbox Code Playgroud) 我将a的值赋给了variableA另一种形式variableB.为什么两个变量都是空的,之后我清除了variableA?
Before execute entries.clear()
Run Code Online (Sandbox Code Playgroud)
After execute entries.clear()
Run Code Online (Sandbox Code Playgroud)
我知道为什么a.get()返回20,这是因为动态绑定,因为B对象是在运行时创建的,所以它get()在类B中调用了
但是为什么要a.x打印10?
class A {
int x = 10;
int get() {
return x;
}
}
class B extends A {
int x = 20;
int get() {
return x;
}
}
class Main {
public static void main(String[] args) {
A a = new B();
System.out.println(a.get()); //20
System.out.println(a.x); //10
}
}
Run Code Online (Sandbox Code Playgroud)
如果您还可以在此处解释用于存储对象的内存。
实例设置为null后,实例委托方法如何仍可访问?下面的代码中的操作委托指向实例方法,但在将实例设置为null后,该方法仍然可用.
public class Work { public string Name{get;set;} }
public class Worker
{
public void Do(Work work)
{
Console.WriteLine("'{0}' is done.", work.Name);
}
}
public class BatchWorker
{
private List<Work> _works;
public readonly Action<Work> Worker;
public BatchWorker(Action<Work> worker)
{
_works = new List<Work>();
Worker = worker;
}
public void AddWork(Work work)
{
this._works.Add(work);
}
public void DoWorks()
{
this._works.ForEach(this.Worker);
}
}
public class Program{
public static void Main(){
var worker = new Worker();
var batchWorker = new BatchWorker(worker.Do);
batchWorker.AddWork(new Work …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }
}
public class LinqValueCalculator
{
public decimal ValueProducts(IEnumerable<Product> products)
{
return products.Sum(p => p.Price);
}
}
public class ShoppingCart
{
private LinqValueCalculator calc;
public ShoppingCart(LinqValueCalculator calcParam)
{
calc = calcParam;
}
public IEnumerable<Product> Products { get; set; }
public decimal …Run Code Online (Sandbox Code Playgroud) 假设我们有以下定义.
interface Vessel{}
interface Toy{}
class Boat implements Vessel{}
class Speedboat extends Boat implements Toy{}
Run Code Online (Sandbox Code Playgroud)
主要是,我们有这些:
Boat b = new Speedboat();
Run Code Online (Sandbox Code Playgroud)
并(b instanceof Toy)评估为true?为什么?我的理解是,引用类型b是Boat,但Boat与之无关Toy,所以它应该是,false但答案是true.
我知道字符串是不可变的,无法重新定义,但是在此foreach循环中,通过添加数组元素多次更改了字符串。
var stringOfNames = "";
var arrayOfNames = new string[5] { "jack", "marry", "joe", "jimmy", "bud" };
foreach (var item in arrayOfNames)
{
stringOfNames += item;
Console.WriteLine(stringOfNames);
}
Run Code Online (Sandbox Code Playgroud)
预期:
出现错误,指出“此范围中已经定义了变量”。
实际:
通过添加其他名称来更改字符串。
另外,这两者之间有什么区别:
1)
var a = "something";
var a = "something else";
Run Code Online (Sandbox Code Playgroud)
2)
var a = "something";
a+= "asdf";
Run Code Online (Sandbox Code Playgroud)
为什么第二个选项起作用?
我遵循了www.patrickvideos.com上的 java SE 教程,并在有关“类和对象”的章节中遇到了问题。制作教程的人给出了以下代码示例:
Employee alex;
Employee linda;
Employee john;
Run Code Online (Sandbox Code Playgroud)
他明确表示"alex, linda, john are what you call objects"。但是,如果我没有记错的话,变量本身不能是对象,而只是保存对对象或类实例的引用。那么,alex, linda, john对象是对象还是可以安全地假设它们是对象的引用并且该人正在传播错误信息?
它不是重复的,我想在C#的上下文中了解这一点.
我有这样的课程:
public class A
{
public List<string> AList { get; set; }
}
public class B
{
public List<string> BList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
想象一下,row 6发生垃圾收集:
1 row: A objectA = new A();
2 row: B objectB = new B();
3 row: objectA.AList = new List<string>() { "1", "2", "3" };
4 row: objectB.BList = objectA.AList;
5 row: objectA = null;
6 row: //Garbage collection occurs
Run Code Online (Sandbox Code Playgroud)
我有两个问题: