Sunday, October 19, 2014

JAVA: charset intro: new String(byte[] bytes, String charsetname)

 

http://stackoverflow.com/a/7048780

Wikipedia explains both reasonably well: UTF-8 vs Latin-1 (ISO-8859-1). Former is a variable-length encoding, latter single-byte fixed length encoding.

Latin-1 encodes just the first 256 code points of the Unicode character set, UTF-8 can be used to encode all code points.

At physical encoding level, only codepoints 0 - 127 get encoded identically; code points 128 - 255 differ by

- becoming 2-byte sequence with UTF-8

- are single bytes with Latin-1.

Wednesday, September 24, 2014

JAVA: Be careful with String class – Performance and Memory

 

1. Operation such as

- String.split()

- String.substring()

uses offset values but keep the ORIGINAL char[] value. Therefore, if you only ever want a portion of the String it will WASTE memory space by storing the original string value

http://stackoverflow.com/questions/7629208/substrings-and-garbage-in-java-1-6

2.  For String.substring()

to get just the portion of the substring, recommend to use

String sub = new String(str.substring(6,12));

http://stackoverflow.com/questions/1281549/memory-leak-traps-in-the-java-standard-api/1281569#1281569


3. General String related:


http://www.javamex.com/tutorials/memory/string_memory_usage.shtml

JAVA: Be careful with String class - General

 

1. JAVA only puts primitive and reference variable onto Stack while all objects, including String, is created and stays on the Heap, subject to GC

http://programmers.stackexchange.com/questions/65281/stack-and-heap-memory-in-java/65289#65289

 

2. String literal value assignment such as

String str1 = “abcd”;

and String variable assignment such as

String str2 = str1

will create these new String objects (which are reference objects) and both str1 and str2 will point to the same object with value “abcd” in the JVM’s internal String Pool

 

3. String Value Object itself, not the reference that points to it, is IMMUTABLE (i.e.the String Class itself is ‘final’), meaning the whenever you try to

String str = someOpToModify(str);

What happens is that another String Value Object is created on the String Pool and this ‘str’ String Reference Object is not changed to point at this new String Value Object rather than the old String Value Object that contains the old value before modification.

http://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning/17942294#17942294

 

4. Only way to make sure a new Object of String is created in the String Pool is to call: String str3 = new String(“abcd”);

5. Why to make the String immutable?

http://stackoverflow.com/questions/2068804/why-is-string-final-in-java/2069014#2069014

  1. Security: the system can hand out sensitive bits of read-only information without worrying that they will be altered
  2. Performance: immutable data is very useful in making things thread-safe.

 

 

All and all, be very careful with String in JAVA, especially when it comes to performance and if you have a lot of long Strings to load into the memory:

http://www.javamex.com/tutorials/memory/string_saving_memory.shtml

Sunday, June 1, 2014

JAVA: default values of primitive arrays are Zeros

 

http://stackoverflow.com/a/2154340

 

http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

 

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

    • For type byte, the default value is zero, that is, the value of (byte)0.

    • For type short, the default value is zero, that is, the value of (short)0.

    • For type int, the default value is zero, that is, 0.

    • For type long, the default value is zero, that is, 0L.

    • For type float, the default value is positive zero, that is, 0.0f.

    • For type double, the default value is positive zero, that is, 0.0d.

    • For type char, the default value is the null character, that is, '\u0000'.

    • For type boolean, the default value is false.

    • For all reference types (§4.3), the default value is null.

Sunday, May 4, 2014

JAVA: the good old myth of passing by ‘references’

 

http://stackoverflow.com/questions/5607773/change-a-functions-arguments-values

To clarify, the bool variable does not change in the calling method. A new copy of the variable is created in the called method. You can change it as much as u like, but this var is different from the one in calling method. Yet, if your argument was a pointer, then any changes in the called method will directly affect the object in the calling method, since a new copy of the variable is not created in the called method.

http://stackoverflow.com/a/16513496

Java functions parameters are called by reference name, meaning that when you place an object as an function argument, the JVM copies the reference's value to a new variable, and passes it to the function as an argument. If you change the contents of the object, then the original object will change, but if you change the actual value of the reference, then these changes will be destroyed when the function ends.

Therefore, the following call to the sub function will not result a list with appropriate items deleted:

 

    public static void test2() {
MySingleLinkedNode<Integer> list1 = createIntList(new int[]{1,1,1,3,4,6,6,7,7});
MySingleLinkedListUtils.deleteAllFromList(list1, 1);
list1.printRestOfList();




}


 

    public static <T> void deleteAllFromList(MySingleLinkedNode<T> head, T data){
while (head.getNext() != null){
// 1. head is the one we are deleting, loop until we delete all such occurrence
if (head.getData().equals(data)){
head = head.getNext();
}
else{
break;
}
}

// 2. head is not the one we are deleting
MySingleLinkedNode<T> prevNode = head;
while (prevNode.getNext() != null){
MySingleLinkedNode<T> currNode = prevNode.getNext();
if (currNode.getData().equals(data)){
// we found the node to delete
prevNode.setNext(currNode.getNext());
}
prevNode = prevNode.getNext();
}
}

Sunday, April 13, 2014

JAVA: Immutable String… wait for it … reference

 

Simply said, String itself is a ‘reference obj’ not a ‘data obj’

String s1 = "Mississippi";
String s2 = s1;
s1 = s1.replace('i', '!');
System.out.println(s1); // Prints "M!ss!ss!pp!"
System.out.println(s2); // Prints "Mississippi"

Tuesday, November 5, 2013

java: toArray() throws ClassCastException

 

 

The following code (run in android) always gives me a ClassCastException in the 3rd line:

final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();

 


This is because when you use

 toArray() 

it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray method only gets a

List 

and not

List<String>

as generics are a source code only thing, and not available at runtime and so it can't determine what type of array to create.

use

toArray(new String[v2.size()]);

which allocates the right kind of array (String[] and of the right size)