任何人都可以告诉我创建了多少个对象.s3不是从字符串池中引用相同的hello吗?有多少String对象
/**
*
*/
package agnitio;
/**
* @author admin
*
*/
public class TestString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="hello";
String s2="hello";
String s3 = new String("hello");
System.out.println(s1==s2); // true
System.out.println(s1==s3); // false
System.out.println(s2==s3); // false
}
}
Run Code Online (Sandbox Code Playgroud) 我读到.equals()比较对象的值,而==比较引用(即 - 变量指向的内存位置).请参见:Java中== vs equals()之间的区别是什么?
但请注意以下代码:
package main;
public class Playground {
public static void main(String[] args) {
Vertex v1 = new Vertex(1);
Vertex v2 = new Vertex(1);
if(v1==v2){
System.out.println("1");
}
if(v1.equals(v2)){
System.out.println("2");
}
}
}
class Vertex{
public int id;
public Vertex(int id){
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:(
没什么)
不应该打印2吗?
考虑以下情况:
String cat = "cat";
String cat2 = "cat";
out.println(cat == cat2); // true // Uses String#equals(...)
out.println(((Object) cat) == ((Object) cat2)); // true. Object#equals(...)???
// So it should be false!
Run Code Online (Sandbox Code Playgroud)
在StackOverflow中通过answer进行比较的Object 的==默认值..equals
既然,我将它们作为对象进行转换,它们不应该使用默认比较作为参考比较吗?
我正在编写一个简单的方法,将成绩作为用户的输入并计算成绩点平均值.这是我的代码:
public static double calculateGPA(){
Scanner in = new Scanner(System.in);
double totalGradePoints = 0; // total grade points
int numClasses = 0; // total classes completed
boolean doneInput = false; // returns true when use is done inputting grades
System.out.println("Enter all your grades (A,B,C,D,F) and enter 'done' if you are done entering your grades.");
while (!doneInput) {
String grade = in.next();
if (grade == "A") {
totalGradePoints += 4;
numClasses++;
} else if (grade == "B") {
totalGradePoints += 3; …Run Code Online (Sandbox Code Playgroud) 我尝试过这个实现,但是对于类x来说我是假的
x.clone().equals(x)
Run Code Online (Sandbox Code Playgroud)
X级:
public class X implements Cloneable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected Object clone()throws CloneNotSupportedException {
return super.clone();
}
}
Run Code Online (Sandbox Code Playgroud)
主要课程:
public class ObjectCloneCopy {
public static void main(String[] args) throws CloneNotSupportedException {
X x = new X();
System.out.println("x.clone().equals(x) - " + x.clone().equals(x));
}
}
Run Code Online (Sandbox Code Playgroud)
是否必须重载hashcode()和equals()以获得此True?
如果不重写这些方法,这个陈述如何给出真实的?
X x1 = x;
x1.equals(x)
Run Code Online (Sandbox Code Playgroud)
解释这可能是真的,我已经在这个链接中看到了
每当用户输入为1或Jan时,代码都不会读取并跳转到第二个语句并显示
已输入无效月份
Scanner in = new Scanner(System.in);
System.out.println("Enter a month: ");
String month=in.nextLine();
if((month == "1") || (month == "Jan")){
System.out.println("Month: January");
}
else{
System.out.println("Invalid month has been entered");
Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,其中我有N个字符串和Q查询也是字符串.目标是确定每个查询在N个字符串中出现的次数.
这是我的代码:
import java.util.Scanner;
import java.util.ArrayList;
public class SparseArrays{
// count the number of occurances of a string in an array
int countStringOccurance(ArrayList<String> arr, String str){
int count = 0;
for (int i=0; i<arr.size(); i++) {
if (str==arr.get(i)) {
count += 1;
}
}
return count;
}
void start(){
// scanner object
Scanner input = new Scanner(System.in);
// ask for user to input num strings
System.out.println("How many string would you like to enter?");
// store the number of strings …Run Code Online (Sandbox Code Playgroud) Box b1 = new Box();
Box b2 = b1;
Run Code Online (Sandbox Code Playgroud)
这里 b1 和 b2 指的是同一个对象。因此,在 2 个 String 对象的情况下,为什么我们不能使用==它们而不是.equals()方法来比较它们。
下面的代码测试字符串 myStr 和 newStr 是否包含相同的值。我期望结果是相等的,但令我惊讶的是,该行String newStr = null似乎导致 newStr 将“null”作为实际文本。它是否正确?
我预计它只是被分配了一个对任何内容的引用,导致此时没有任何价值。
String myStr = "java";
char[] myCharArr = {'j', 'a', 'v', 'a'};
String newStr = null; // not an empty reference but assigning the text "null"?
for (char value : myCharArr) {
newStr = newStr + value;
}
System.out.println(newStr == myStr); // results in false: newStr is 'nulljava' and myStr 'java'
Run Code Online (Sandbox Code Playgroud)
请注意,当更改为时,"String newStr = null"您String newStr = new String()确实会得到一个空引用,并且 newStr 最终将只是“java”。
我一直在尝试使用这个equals()方法和==两个Baby对象,但两个都给了我false.
public class Baby {
String name;
Baby(String myName) {
name = myName;
}
public static void main(String[] args) {
Baby s1 = new Baby("a");
Baby s2 = new Baby("a");
System.out.println(s2.equals(s1));
System.out.println(s1 == s2);
}
}
Run Code Online (Sandbox Code Playgroud)