"[B" 是什么 Java 类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4606864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What kind of Java type is "[B"?
提问by Xorty
I am trying to get MD5 encrypted pass from MySQL DB via Java code (Hibernate). But I cant get neither String nor any reasonable Java type.
我正在尝试通过 Java 代码(休眠)从 MySQL DB 获取 MD5 加密传递。但是我既无法获得 String 也无法获得任何合理的 Java 类型。
The only thing I am getting is this unhelpful message: java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob(or whatever Java type I try cast to).
我唯一得到的是这条无用的消息: java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob(或我尝试转换的任何 Java 类型)。
Here is my method:
这是我的方法:
public void testCrypto() {
session.beginTransaction();
// creates native SQL query
// uses native MySQL's MD5 crypto
final Blob pass = (Blob) session.createSQLQuery("SELECT MD5('somePass')")
.list().get(0);
session.getTransaction().commit();
}
Here is full stack trace:
这是完整的堆栈跟踪:
java.lang.ClassCastException: [B cannot be cast to com.mysql.jdbc.Blob
at domain.DatabaseTest.testCrypto(DatabaseTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
回答by rfeak
That my friend is an array of bytes. In JNI, [B is used to describe an array ([
) of bytes (B
). An array of ints is [I
etc.
我的朋友是一个字节数组。在 JNI 中,[B 用于描述一个[
字节数组 ( ) ( B
)。整数数组[I
等。
You can get a bit more information on field descriptors here:
JNI Types and Data Structures(Table 3-2should be what you are looking for).
您可以在此处获得有关字段描述符的更多信息:
JNI 类型和数据结构(表 3-2应该是您要查找的内容)。
回答by Sean Patrick Floyd
It's the class name of byte[].class
. Try this:
这是 的类名byte[].class
。试试这个:
System.out.println(byte[].class.getName());
Output (you guessed it):
输出(你猜对了):
[B
[乙
And if you want to access the readable name, use Class.getCanonicalName()
:
如果您想访问可读名称,请使用Class.getCanonicalName()
:
System.out.println(byte[].class.getCanonicalName());
Output:
输出:
byte[]
字节[]
回答by Evan Mulawski
As the other answers state, that is a byte array.
正如其他答案所述,这是一个字节数组。
If you want to get a string from a byte array, use the String constructor:
如果要从字节数组中获取字符串,请使用 String 构造函数:
public void testCrypto()
{
session.beginTransaction();
// creates native SQL query
// uses native MySQL's MD5 crypto
final String pass = new String(session.createSQLQuery("SELECT MD5('somePass')")
.list().get(0));
session.getTransaction().commit();
}
回答by Chris Dodd
[B
is the encoded type name for a byte array (byte[]), which should normally only appear in type signature strings, as its not a valid type name.
[B
是字节数组 (byte[]) 的编码类型名称,它通常只应出现在类型签名字符串中,因为它不是有效的类型名称。