welcometotalk
BAN USERpublic class StringTest {
public static void main(String args[]){
String s = new String("test213");
try {
int count = (Integer)getFieldValue(s, "count");
int offset = (Integer)getFieldValue(s, "offset");
System.out.println("Size :"+(count-offset));
} catch (Exception e) {
e.printStackTrace();
}
}
static Object getFieldValue(String s,String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Object chars = null;
Field fieldValue = String.class.getDeclaredField(fieldName);
fieldValue.setAccessible(true);
chars = fieldValue.get(s);
return chars;
}
}
Use Reflection to get values of count and offset.
- welcometotalk December 02, 2013@Jason, External_Sorting provides solution in general , where we have limited memory comparately the data size. So i gave one vote for that.
But it seems interviewer like to use the bitwise operator .. that piece still not answered.
private static String revereseWords(String inputString,char spaceChar){
char[] charArray = inputString.toCharArray();
int totalLength = charArray.length;
char[] destArray = new char[totalLength];
int spaceCount = 0;
for (int i = totalLength , length = 0; i > 0; i--,length++) {
if (charArray[i-1] != spaceChar){
spaceCount ++;
}else{
System.arraycopy(charArray, i, destArray, length-spaceCount, spaceCount);
destArray[length] = ' ';
spaceCount = 0;
}
}
if (spaceCount > 0){
System.arraycopy(charArray, 0, destArray, totalLength-spaceCount, spaceCount);
}
return String.valueOf(destArray);
}
My Solution without Split to avoid regex cost
System.out.println(revereseWords("This is test",' '));
Decimal can convert into base by divide and collecting the remainders,
For example:
Binary value of decimal 12
12 / 2 = 6 with remainder 0
6 / 2 = 3 with remainder 0
3/ 2 = 1 with remainder 1
1 / 2 = 0 with remainder 1
So the Binary value is 1100
Same formula can be applied to find Negative Base, but remember remainders have to be positive.
Like the sample found in wiki
Note, here -5/-3 = 2 with remainder 1 ,
If a/b = c with remainder d then bc + d = a , thus the c can be found like c = (a-d) / b
Let’s try to write Java method to calculate this.
private static void convertBase(int decimalVal,int base){
StringBuffer buffer = new StringBuffer();
while (decimalVal != 0){
int remainder = Math.abs((decimalVal % base)); //find positive remainder
decimalVal = (decimalVal - remainder) / base; // c = (a-d) / b
buffer.insert(0, remainder);
}
System.out.println(buffer);
}
convertBase(7,-2) = 11011
convertBase(6,-2) = 11010
convertBase(7,2) = 111
convertBase(6,2) = 110
This code will allocate memory to store string constant "ABC" and will create One "String" Object ( as mentioned in byte code - astore_1 local stack varaible ) and assign the String Constant Reference to String Object's instance varaiable "value" char array.
So the answer is It did creates one String Object ( allocates memory to keep the String Object's instance varaible such as count , offset , hash and the "value" char array will point to the String Constant Pool.
Assume , String are mutable , in other words the value char can be changed . So the Compiler cannot be used the same memory for all similar values.
for example:
String s = "ABC"
String a = "ABC"
If the String are mutable , then the jvm should allocate two value char[] memory to hold same char sequence ,
if the Strings are immutable , then the jvm can use the property and save the memory allocation. Note** When we do substring / any method invocation in String it is allocating memory for all other instance variables, but for value char array it try to look the constant pool and if exists it picks from there.
One more info, Strings can be mutable if you are using Reflection
See the sample for how to make mutable and risk and why it is not mutable by default.
Results
- welcometotalk December 02, 2013Before Modification:test213
Before Modification:test213
After Modification:tzst213
After Modification:tzst213