×

Discussion Board

Results 1 to 1 of 1
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Angry Type casting error on Nokia 6600

    These days I'm debugging a game. I was very confused that my game can run on the emulator well, but randomly crash on Nokia 6600 phone. I had no other way but to draw some variable on the screen to see whether it was normal. I wrote below code in my program.

    short s = -1; // s will be changed between -1 and 5.
    ......
    ......
    g.drawString(String.valueOf(s), 0, 0, Graphics.LEFT | Graphics.TOP);

    If the game run normally on 6600, the top left corner of the screen will show a number between -1 and 5. When the program crashed, some strange number will show on the screen, such as -65536, -65533 and so on. It's absolutely impossible that a short variable has a value of -65536. So I checked the defination of String.valueOf(). It is:

    String valueOf(int i);

    Variable s in my program is a short type. So when String.valueOf(s) was called, an implicit type casting from short to int will be performed. I began to think that it was somthing wrong with type casting. So I changed my code as follow:

    short s = -1; // s will be changed between -1 and 5.
    ......
    ......
    g.drawString(String.valueOf(s), 0, 0, Graphics.LEFT | Graphics.TOP);
    Short st = new Short(s);
    g.drawString(st.toString(), 0, 15, Graphics.LEFT | Graphics.TOP);

    Now when the program run normally, two same number will be shown on the top left corner of the screen. But when the program crashed, the two number will be different, such as one is -65536 and another is 0, or one is -65533 and another is 3.

    The binary of 0(short) and -65536(int) are:
    0000000000000000
    11111111111111110000000000000000

    The binary of 3(short) and -65533(int) are:
    0000000000000011
    11111111111111110000000000000011

    If the type casting run correctly, the high 16 bits of the int value will be same as the 15th bit of the short value. But on Nokia 6600, sometimes the high 16 bits will be set to 1 no matter the 15th bit of the short value is.

    Now it is not hard to explain why my program will crash. In my program, there are below code:

    short s = -1; // s will be changed between -1 and 5.
    int[] array = new int[array_len];
    ......
    ......
    int i = array[s];

    The index of an array should be a int value. However, I used a short value. So an implicit type casting from short to int occured. When the type casting run incorrectly, the index of the array will be a negative value. Then my program crashed.

    Is there anybody who can tell me what on earth the JVM's behavior is?
    Last edited by spanee; 2006-04-30 at 03:00.

Similar Threads

  1. ###Upgrading Firmware###
    By zahid44 in forum General Development Questions
    Replies: 27
    Last Post: 2008-10-21, 07:17
  2. Infra-red capability
    By Symbian_Challenge_0412 in forum General Development Questions
    Replies: 1
    Last Post: 2005-08-16, 18:24
  3. Java Compability
    By emax73 in forum General Development Questions
    Replies: 0
    Last Post: 2004-07-12, 14:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×