This question already has answers here:
hashCode uniqueness
(7 answers)
What is an object's hash code if hashCode() is not overridden?
(12 answers)
Does hashcode number represent the memory address? [duplicate]
(9 answers)
How can I get the memory location of a object in java?
(4 answers)
Closed 3 years ago.
I wrote this code and want to see memory location of 2 objects that i create from one class and make instance of one specific variable.
public class StaticFields {
int a = 12;
int b = 235;
public static void main(String[] args) {
StaticFields obj = new StaticFields();
int a = obj.a;
StaticFields obj2 = new StaticFields();
int c = obj2.a;
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(c));
}
}
why the "identityHashCode" of "a" and "c" is the same ?
Thanks.
Both the integers carry the same value, 12.
Since Integers are cached (for values up to 127 from -128), the hash code value of both objects returned are same.
This is not true for b since its value is greater than 127.
Related
This question already has answers here:
How does the "final" keyword in Java work? (I can still modify an object.)
(20 answers)
What is the point of "final class" in Java?
(24 answers)
Closed 3 years ago.
Even though String is a final class we can change the value of it like:
String A = "hello";
And in next step:
A = "World";
Here A will be changed.
Whereas in case of a final variable we can't do it like:
final int a =10;
a = 13; //This Will Give Error
This would be a contradiction.
This is because when the class is final it means the methods of the class cannot be changed or overridden. If a field is final, then the value cannot be changed after the initial value has been assigned.
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
How to initialize an array in Java?
(11 answers)
Closed 3 years ago.
I am having trouble initializing an array element inside my class. How can I assign an initial value at a specific index of my array field?
public class Account{
int num[] = new int[50];
// I can't assign a value like this:
num[0] = 12345;
}
You can initialize the array in the constructor.
Just write:
public Account(){
num[0]=12345;
}
inside your class.
The other possibility is to use an initialisation block(but this is less flexible and difficult du document):
{
num[0]=12345;
}
(Also in your class)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
How many Strings are created in memory?
(4 answers)
Closed 4 years ago.
How does Java implement the below string comparisons
public class MyClass {
public static void main(String args[]) {
String a = "Chaipau";
String b = "pau";
System.out.println(a == "Chai"+"pau"); //true
System.out.println(a == "Chai"+b); //false
}
}
This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.
"Chai"+"pau" is semantically identical to "Chaipau", and thus is the same instance that a refers to.
"Chai"+b is evaluated at runtime (because b is not a compile-time constant expression), creating a new instance of String, and thus is not the same instance that a refers to.
This question already has answers here:
Integer wrapper objects share the same instances only within the value 127? [duplicate]
(5 answers)
Wrong output for integer comparison values
(2 answers)
Why is == true for some Integer objects? [duplicate]
(4 answers)
Closed 9 years ago.
public class Test {
public static void main(String[] args) {
Integer i=555,j=555;
System.out.println(i==j); //false
Integer l=5,n=5;
System.out.println(l==n); //true
}
}
Why, Java? How is that even possible?
You're comparing the references of two different Integer class instances with the same value, so you must use the equals method (as it must be to compare equality between objects):
Integer i=555,j=555;
System.out.println(i==j); //false
Integer i=555,j=555;
System.out.println(i.equals(j)); //true
But Integer has a pool of Integer object instances for int values between -128 and 127. So when you do
Integer l=5,n=5;
System.out.println(l==n); //true
You receive true since l and n points to the same object reference.
This question already has answers here:
Creating a hashCode() Method - Java
(4 answers)
Closed 9 years ago.
How do I generate a hashCode from two fields in my class?
For example, I want Pair classes with the same objects V to have the same hashCode:
public class Pair<V> {
V from, to;
}
Should I multiply their hashCodes together? Add them? Multiply them with a prime?
One way to do it is adding the hash code of the first field to hash code of the second field, multiplied by a small prime number, like this:
public int hashCode() {
return 31 * from.hashCode() + to.hashCode();
}