As i know,a Java object contains object header,reference and it's data.  //jdk 1.7 public final class String{        private final char value[];        private int hash;        //...     }   For new String("abc") ,how many bytes will it take if we just think about 32-bit system?  totleBytes =      8//String Object header     +4 //value reference      +4 //hash     +12 //char[] object header     +3*1//value[] data   does it right,or miss something?
13148
I have an application that reads a CSV file with piles of data rows.  I give the user a summary of the number of rows based on types of data, but I want to make sure that I don't read in too many rows of data and cause OutOfMemoryErrors.  Each row translates into an object.  Is there an easy way to find out the size of that object programmatically?  Is there a reference that defines how large primitive types and object references are for a VM?  Right now, I have code that says read up to 32,000 rows, but I'd also like to have code that says read as many rows as possible until I've used 32MB of memory.  Maybe that is a different question, but I'd still like to know.
75336
Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for Strings. What happens under the hood when you do that? What does StringBuffer do differently?
16305
How do I convert a byte[] to a string? Every time I attempt it, I get      System.Byte[]    instead of the value.  Also, how do I get the value in Hex instead of a decimal?
87194
I've been told that code such as:  for (int i = 0; i &lt; x.length(); i++) {     // blah }   is actually O(n^2) because of the repeated calls to x.length(). Instead I should use:  int l = x.length(); for (int i = 0; i &lt; l; i++) {     // blah }   Is this true? Is string length stored as a private integer attribute of the String class? Or does String.length() really walk the whole string just to determine its length?
59782
I was about to reconfigure some of my code to use the newer Schema.describeSObjects(types) method that I had heard was faster than using a globalDescribe.   I made some code to quickly check this and I was dismayed to find that the newer technique is not only not faster, it's about twice as slow as using a global describe. Here's the code I used :  New DescribeSObjects() method  //each block was run separately for timing purposes String obj = 'Account'; String[] types = new String[]{obj}; System.debug('START DESCRIBE SOBJECTS');  for (Integer i = 0; i &lt; 50; i++) {   List&lt;Schema.DescribeSobjectResult&gt; results = Schema.describeSObjects(types);  } System.debug('FINISH DESCRIBE SOBJECTS');   Old getGlobalDescribe() method  String obj1 = 'Account'; System.debug('START DESCRIBE GLOBAL');  for (Integer i = 0; i &lt; 50; i++) {    Map&lt;String,Schema.SObjectType&gt; gd = Schema.getGlobalDescribe();    Schema.SObjectType sobjType = gd.get(obj1);    Schema.DescribeSObjectResult describeResult = sobjType.getDescribe();  } System.debug('FINISH DESCRIBE GLOBAL');   Over 5 runs of each, the getGlobalDescribe() method took an average of 337ms for 50 describes and the describeSObjects() method took and average of 658ms. This is basically twice as long.  Does anyone have any insight as to why this would be? Any improvements that will significantly improve the accuracy of the resulting data?  As this data stands, I'm sticking with the old method.  One final note: the old technique uses 3 times a much heap (67kb vs 27kb)
17359
What is meant by "object serialization"? Can you please explain it with some examples?
124187
I have a small hierarchy of objects that I need to serialize and transmit via a socket connection.  I need to both serialize the object, then deserialize it based on what type it is.  Is there an easy way to do this in C++ (as there is in Java)?  Are there any C++ serialization online code samples or tutorials?  EDIT: Just to be clear, I'm looking for methods on converting an object into an array of bytes, then back into an object.  I can handle the socket transmission.
13844
In Java, there is no such thing as an unsigned byte.  Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign.  What's a good way to work around this? (Saying don't use Java is not an option)
97059
How is it that Java's BigDecimal can be this painful?  Double d = 13.3D;  BigDecimal bd1 = new BigDecimal(d); BigDecimal bd2 = new BigDecimal(String.valueOf(d));   System.out.println("RESULT 1: "+bd1.toString()); System.out.println("RESULT 2: "+bd2.toString());  RESULT 1: 13.300000000000000710542735760100185871124267578125 RESULT 2: 13.3   Is there any situation where Result 1 would be desired? I know that Java 1.5 changed the toString() method but was this the intended consequence?   Also I realise that BigDecimal has doubleValue() etc, but the library that I am working with helpfully uses a toString() and I can't change that :-(  Cheers.
60150
I'm working with some example java code for making md5 hashes. One part converts the results from bytes to a string of hex digits:  byte messageDigest[] = algorithm.digest();      StringBuffer hexString = new StringBuffer(); for (int i=0;i&lt;messageDigest.length;i++) {     hexString.append(Integer.toHexString(0xFF &amp; messageDigest[i]));     }   However, it doesn't quite work since toHexString apparently drops off leading zeros. So, what's the simplest way to go from byte array to hex string that maintains the leading zeros?
2136
I am looking for a way to convert a long string (from a dump), that represents hex values into a byte array.  I couldn't have phrased it better than the person that posted .  But to keep it original, I'll phrase it my own way: suppose I have a string "00A0BF" that I would like interpreted as the  byte[] {0x00,0xA0,0xBf}   what should I do?  I am a Java novice and ended up using BigInteger and watching out for leading hex zeros. But I think it is ugly and I am sure I am missing something simple.
78162
I collect a few corner cases and  and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:  string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));   I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should.  Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)  Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.  Any other gems lurking out there?
37343
How do you find a memory leak in Java (using, for example, JHat)? I have tried to load the heap dump up in JHat to take a basic look. However, I do not understand how I am supposed to be able to find the root reference () or whatever it is called. Basically, I can tell that there are several hundred megabytes of hash table entries ([java.util.HashMap$Entry or something like that), but maps are used all over the place... Is there some way to search for large maps, or perhaps find general roots of large object trees?  [Edit] Ok, I've read the answers so far but let's just say I am a cheap bastard (meaning I am more interested in learning how to use JHat than to pay for JProfiler). Also, JHat is always available since it is part of the JDK. Unless of course there is no way with JHat but brute force, but I can't believe that can be the case.  Also, I do not think I will be able to actually modify (adding logging of all map sizes) and run it for long enough for me to notice the leak.
39649
Per the Java documentation, the  for a String object is computed as:   s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]       using int arithmetic, where s[i] is the    ith character of the string, n is the length of    the string, and ^ indicates exponentiation.   Why is 31 used as a multiplier?  I understand that the multiplier should be a relatively large prime number. So why not 29, or 37, or even 97?
59489
If we look at the Java Object class then we can find some of the methods like:  public native int hashCode() protected native Object clone()   What are these natives and how do these methods work?
36107
Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect?  To make my question more specific, I am writing an application that deals with data and the specification say that there should be a 'reset' button that brings the application back to its original just launched state. However, all data have to be 'live' unless the application is closed or reset button is pressed.  Being usually a C/C++ programmer, I thought this would be trivial to implement. (And hence I planned to implement it last.) I structured my program such that all the 'reset-able' objects would be in the same class so that I can just destroy all 'live' objects when a reset button is pressed.  I was thinking if all I did was just to dereference the data and wait for the garbage collector to collect them, wouldn't there be a memory leak if my user repeatedly entered data and pressed the reset button? I was also thinking since Java is quite mature as a language, there should be a way to prevent this from happening or gracefully tackle this.
5968
Programming language books explain that value types are created on the stack, and reference types are created on the heap, without explaining what these two things are. I haven't read a clear explanation of this.  I understand what a stack is. But,    Where and what are they (physically in a real computer's memory)? To what extent are they controlled by the OS or language run-time? What is their scope? What determines the size of each of them? What makes one faster?