1.怎么输出String类型的存放地址?
2.主方法中main(String [] args) 加String [] args有什么现实意义,我知道可以这样输出一些东西
for(int i=0;i!=args.length;++i)
System.out.println("args"+i+" : "+args[i]);
但这有什么意义?
2.主方法中main(String [] args) 加String [] args有什么现实意义,我知道可以这样输出一些东西
for(int i=0;i!=args.length;++i)
System.out.println("args"+i+" : "+args[i]);
但这有什么意义?
2 传递参数
hashCode是存放地址? 怎么看出来的?哪有文档说明?
String中hashCode的源码是这样的: public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count; for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
哪看出来是取地址了?
对于没有实现hashCode方法的对象,通过hashCode方法取得的值和通过System.identityHashCode取得的值是一样的(这个值也不一定是地址,具体的要看jvm的实现是怎么样的)
楼上几位不要误人子弟
li si
如果直接这样输入:
java Test zhang san li si
会被视为4个参数输出:
所以应该如下输入:
java Test "zhang san" "li si"
1:Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以hashcode可以做到尽可能的不一样。
可以下载openjdk的源码看下,object的hashcode调用的是JVM_IHashCode(), 在 hotspot\src\share\vm\prims\jvm.cpp:JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
JVMWrapper("JVM_IHashCode");
// as implemented in the classic virtual machine; return 0 if object is NULL
return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
JVM_END再看ObjectSynchronizer::FastHashCode方法:intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
if (UseBiasedLocking) {
// NOTE: many places throughout the JVM do not expect a safepoint
// to be taken here, in particular most operations on perm gen
// objects. However, we only ever bias Java instances and all of
// the call sites of identity_hash that might revoke biases have
// been checked to make sure they can handle a safepoint. The
// added check of the bias pattern is to avoid useless calls to
// thread-local storage.
if (obj->()->has_bias_pattern()) {
// Box and unbox the raw reference just in case we cause a STW safepoint.
Handle hobj (Self, obj) ;
// Relaxing assertion for bug 6320749.
assert (Universe::verify_in_progress() ||
!SafepointSynchronize::is_at_safepoint(),
"biases should not be seen by VM thread here");
BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
obj = hobj() ;
assert(!obj->()->has_bias_pattern(), "biases should be revoked by now");
}
} // hashCode() is a heap mutator ...
// Relaxing assertion for bug 6320749.
assert (Universe::verify_in_progress() ||
!SafepointSynchronize::is_at_safepoint(), "invariant") ;
assert (Universe::verify_in_progress() ||
Self->is_Java_thread() , "invariant") ;
assert (Universe::verify_in_progress() ||
((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ; ObjectMonitor* monitor = NULL;
Oop temp, test;
intptr_t hash;
Oop = ReadStableMark (obj); // object should remain ineligible for biased locking
assert (!->has_bias_pattern(), "invariant") ; if (->is_neutral()) {
hash = ->hash(); // this is a normal header
if (hash) { // if it has hash, just return it
return hash;
}
hash = get_next_hash(Self, obj); // allocate a new hash code
temp = ->copy_set_hash(hash); // merge the hash code into header
// use (machine word version) atomic operation to install the hash
test = (Oop) Atomic::cmpxchg_ptr(temp, obj->_addr(), );
if (test == ) {
return hash;
}
// If atomic operation failed, we must inflate the header
// into heavy weight monitor. We could add more code here
// for fast path, but it does not worth the complexity.
} else if (->has_monitor()) {
monitor = ->monitor();
temp = monitor->header();
assert (temp->is_neutral(), "invariant") ;
hash = temp->hash();
if (hash) {
return hash;
}
// Skip to the following code to reduce code size
} else if (Self->is_lock_owned((address)->locker())) {
temp = ->displaced__helper(); // this is a lightweight monitor owned
assert (temp->is_neutral(), "invariant") ;
hash = temp->hash(); // by current thread, check if the displaced
if (hash) { // header contains hash code
return hash;
}
// WARNING:
// The displaced header is strictly immutable.
// It can NOT be changed in ANY cases. So we have
// to inflate the header into heavyweight monitor
// even the current thread owns the lock. The reason
// is the BasicLock (stack slot) will be asynchronously
// read by other threads during the inflate() function.
// Any change to stack may not propagate to other threads
// correctly.
} // Inflate the monitor to set hash code
monitor = ObjectSynchronizer::inflate(Self, obj);
// Load displaced header and check it has hash code
= monitor->header();
assert (->is_neutral(), "invariant") ;
hash = ->hash();
if (hash == 0) {
hash = get_next_hash(Self, obj);
temp = ->copy_set_hash(hash); // merge hash code into header
assert (temp->is_neutral(), "invariant") ;
test = (Oop) Atomic::cmpxchg_ptr(temp, monitor, );
if (test != ) {
// The only update to the header in the monitor (outside GC)
// is install the hash code. If someone add new usage of
// displaced header, please update this code
hash = test->hash();
assert (test->is_neutral(), "invariant") ;
assert (hash != 0, "Trivial unexpected object/monitor header usage.");
}
}
// We finally get the hash
return hash;
}
2:String类的hashCode是经过重写的,完全使用java实现的.根据String类包含的字符串的内容,根据一种特殊算法返回哈希码,只要字符串内容相同,返回的哈希码也相同。 public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count; for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}更不可能是对象地址了!!
在Java中地址的概念不是很重要了吧,指针都没有了还要地址干嘛?
我目前知道的唯一一种打印出地址的方法是打印数组名,其它的就不知道了int[] a = new int[8];
System.out.println(a);