Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

why String.getBytes() truncates byte[]

Thread view: 
Madhur Ahuja - 28 Sep 2004 09:30 GMT
Hello

Can the length of primitive data types like byte[], be dynamically
reduces in java. For ex in the following code

class test
{

public static void add()
 {

   file://create a 20 byte array
   byte []mname=new byte[20];

    String s="madhur";

   file://initialize all the fields
    for(int i=0;i<20;++i)
    {
     mname[i]=3;
    }

   file://convert a string to byte[]
    mname=s.getBytes();

/* rest of the fields with 0*/

    for(int i=s.length()-1;i<20;++i)
    {
      try
      {

       mname[i]=0;
      }
      catch(Exception e)
      {
       System.out.println("Exception at:" + i);
      }
    }
   }

   public static void main(String args[])
   {
    add();
   }
}

In the above procedure, when I am filling the mname array with 0, it
produces
the *ArrayIndexOutOfBoundsException* starting from location 6.

Why does this exception occurs since I have specified the array size to be
20 and even initialized the all the fields.

--
Madhur Ahuja [madhur<underscore>ahuja<at>yahoo<dot>com]

Homepage
http://madhur.netfirms.com
Gordon Beaton - 28 Sep 2004 09:44 GMT
> Can the length of primitive data types like byte[], be dynamically
> reduces in java. For ex in the following code

No.

mname is a *reference* to a byte array. Initially it refers to an
array of 20 bytes that you created with "new". After assigning the
return from String.getBytes(), it refers to a *different* byte array,
and the initial array becomes eligible for garbage collection.

There is another detail that you take for granted: the array returned
by String.getBytes() might not be the same length as the String.
Depending on the character encoding used, it could be the same or
longer. Since you didn't specify an encoding, the system default is
assumed.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.