5

I use red5 and setting/getting attributes using the IConnection class, but that's really not relevant.

'L' means long in java. so 0L is 0 type Long instead of just '0' which is 0 type Integer.

What is the difference between [Ljava.lang.Long and java.lang.Long in the following error message:

stack trace: java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.lang.Long

update

code sample:

 static Long getLongAttribute(IConnection conn, String attribute) {
    Long result=null;
    try {
        if (!conn.hasAttribute(attribute))
            throw new Exception(attribute +  " - Long attribute not found!");
      result = conn.getLongAttribute(attribute); // <--- ERRROR ON THIS LINE
    } catch (Exception e) {
        _handleException(e);
    }
    return result;
}
3
  • Can you show the code that causes the exception? Commented Dec 22, 2010 at 14:51
  • Know all the point of Long and array of Long, but I wonder why that line caused an exception. Here what I have from doc of red 5 dl.fancycode.com/red5/api/org/red5/server/api/… which says that method returns Long, not array of long, then why exception...... Commented Dec 22, 2010 at 15:40
  • @Vishwanath: It looks like the actual problem is somewhere inside the conn.getLongAttribute call. There is no casting in the code presented in the question, but without a full stack trace it's hard to say exactly where the problem is. If there was a type mismatch with result = conn.getLongAttribute then the compiler would catch it. Commented Dec 23, 2010 at 11:03

4 Answers 4

23

The first object is array of Long, the second is just Long. Try this

    Long l = 1l;
    Long[] l2 = {};
    System.out.println(l.getClass());
    System.out.println(l2.getClass());

Output

class java.lang.Long
class [Ljava.lang.Long;

But I do agree that [L_class_; presentation for array types is highly confusing. I wonder how it came to be that way.

Sign up to request clarification or add additional context in comments.

6 Comments

The presentation is the binary class name for an array class. It is specified in the JVM spec, and also in download.oracle.com/javase/6/docs/api/java/lang/…
The designers attempted to have internal type names that were syntactically illegal, to prevent anyone from deliberately declaring variables using those internal type names. So you're forced to declare something as Long[] instead of [Ljava.lang.long.
@Stephen This is exactly the question, why not use a bit friendlier notation (e.g., java.lang.Long[]). It's not like there's any technical difficulty associated.
@TMN Could you clarify, please? What would be a problem with 'declaring variables using those internal type names'?
@TMN - not convinced that they needed to do that ... in the context of Java 1.1 and later. @Nikita - the design decision was probably made before Java 1.0. Mistakes were made and could not be corrected. This is probably one of them.
|
2

Your code try to cast Long[] to Long which causes ClassCastException

Comments

1

[Ljava.lang.Long is a list of java.lang.Longs

EDIT: as noted below it's an array. Sorry, I typed too fast...

3 Comments

Technically, an array of longs: i.e. Long[].
@Cameron Skinner: meh, I turned around to do something else and noticed my mistake and now I'm too late... ;) But yes, you are right.
@Cameron Skinner: no bad feelings ;)
1

I had similar problem where my code was

List<Object[]> rows = criteria.list();

But my criteria has just count(*) projection and hence criteria.list() returns just List<Long> instead of List<Long[]>

I resolved it by changing it to

List<Object> rows = criteria.list();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.