| |

VerySource

 Forgot password?
 Register
Search
Author: 阿迪雅

How to see pointer address in java

[Copy link]

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-9-30 14:15:02
| Show all posts
It’s impossible to compare with main to be true, and it should always be false.
Reply

Use magic Report

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-9-30 14:30:01
| Show all posts
== compares the address of the object. do not you know?
-----------------------------------
SB, please read the code clearly and talk about it:

if (str== ???) //Here I want to make a == comparison with String str in the main function
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-9-30 14:45:01
| Show all posts
First of all, you should be clear that String in Java is immutable. That is to say, when you assign "aaa" to str in main, and then assign "bbb" to str in changeStr, the virtual machine implicitly creates a String and connects your str reference to the new The String. The original "aaa" has been discarded, and the JVM is responsible for recycling and destroying it.

Then, if you want to compare the addresses of two objects, just use == and that's it. a==b, true if the addresses are equal, it's that simple. As for how to look at the pointer address, I don't know if toString() comes out, but it is completely unnecessary in Java. Abandon the pointer, or you might as well use C++.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-9-30 15:00:01
| Show all posts
The address is no longer visible.
To determine whether two reference type variables are referencing the same object, use "==".
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-9-30 15:15:01
| Show all posts
To learn Java, you must learn to think about problems in the Java language. If you just want to use Java as a tool for expressing C++ ideas, you may be very awkward and make yourself very confused.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 India

Post time: 2020-9-30 15:30:01
| Show all posts
== compares the address of the object. do not you know?
-----------------------------------
SB, please read the code clearly and talk about it:

if (str== ???) //Here I want to compare with String str in the main function ==
================================================= ===========
Host, you don’t understand basic programming. You pass the compared object as a parameter, how do you compare? Even in C, the same is true, unless you use global variables, why don't you get the hang of it? Just turn it a little bit.
public class StringTest {
public static void changeStr(String str){
String str2=str;
String str="bbb";
if (str==str2){
    System.out.println("==");
}
}
public static void main(String[] args) {
String str="aaa";
changeStr(str);
System.out.println(str);
}
}
Also, the concept of references in Java and pointers in C language are equivalent. It seems that you are just learning java. Many things need to be used to it. Although the reference in java is equivalent to the C language pointer, it is an incalculable pointer and can only be compared for equality and inequality, and cannot be added or subtracted. In addition, the referenced physical memory address cannot be obtained, although it has an actual image in the memory.
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-9-30 15:45:01
| Show all posts
>>SB, please read the code clearly before speaking:
>>if (str== ???) //Here I want to make a == comparison with String str in the main function

Please use your brain to think clearly before speaking.
Don't always reveal your ignorance of two languages ​​in SB way.
Reply

Use magic Report

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-9-30 17:00:01
| Show all posts
In addition, the referenced physical memory address cannot be obtained, although it has an actual image in the memory.
---------------
That’s enough. I don’t need his physical memory. In fact, C is also the virtual memory mapping of windows.
I just want to see his mapped address in memory
public static void changeStr(String str){
String str2=str;
String str="bbb";
if (str==str2){

I've thought about this a long time ago, but I can't always add a temporary one like this. It's awkward.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-9-30 17:30:01
| Show all posts
If you want to say this, you have to say "C language does have its convenience."
Under normal circumstances, the hashCode method of the Object class in Java can guarantee that different objects return different values, so it can be used as an ID-like thing, but that is only the practice of general language implementers, not the Java language itself Hard requirements.
Moreover, Object.hashCode can be overloaded, so even if that is done in Object, it is still not necessarily in derived classes.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-9-30 17:45:01
| Show all posts
In addition, when debugging a Java program in Eclipse, you can observe the object ID from the Variables window.
(I use Sun's own JDK, and I don’t know if other vendors’ implementations will actually have an ID for each object)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list