Notes by Dr. Xi
Displaying notes 31 - 40
Created by Dr. Xi on July 14, 2011 07:54:32
Last update: July 14, 2011 07:54:32
This is how to split a string using java.util.StringTokenizer :
StringTokenizer tok = new StringTokenizer("this \...
But don't do it ! According to JavaDoc :
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Using String.split , the equivalent code is:
String[] tokens = "this \tis\n a\t test".split("\...
Also note that the following yield different results:
String[] tokens1 = "this \tis\n a\t test".split("...
Created by Dr. Xi on September 04, 2008 18:34:25
Last update: July 14, 2011 07:31:48
The one argument version of String.split omits trailing empty strings. To get all parts, including trailing empty strings, you need to use the two argument version with a negative limit :
public class TestStringSplit {
public stati...
Output:
Length of array: 8
Part[0]: '1'
Part[1]: '2'...
Created by Dr. Xi on August 13, 2007 20:27:11
Last update: July 13, 2011 16:20:28
Sample code:
import java.util.*;
public class TestArrayL...
If you use iterators, the for loop is equivalent to:
for (Iterator<String> i = l.iterator(); i.hasNext(...
The simplified for loop (or, for-each loop) can be used for arrays or objects that implement java.lang.Iterable .
Note that by using generics, there's not need to down cast. But new for loop syntax doesn't down cast either. If List<String> is changed tp List<Object> , the code doesn't compile.
Created by Dr. Xi on July 13, 2011 16:18:05
Last update: July 13, 2011 16:18:05
The goal is to read a file like this:
for (String line: textFileReader) {
// do s...
This is the code:
import java.io.*;
import java.util.Iterator;
...
Created by Dr. Xi on July 11, 2011 12:24:10
Last update: July 11, 2011 12:25:44
This code snippet
import java.util.*;
public class UncheckedCast ...
fails with a compilation error and a warning:
$ javac -Xlint:unchecked UncheckedCast.java
Unc...
Because List<String> is not a reifiable type, the Java Runtime does not have enough information to verify the type or do the type casting. This is fixed by changing List<String> to List<?> (or to the raw type List ):
public static void main(String[] args) {
Ob...
Created by Dr. Xi on July 11, 2011 12:04:52
Last update: July 11, 2011 12:04:52
From the Java Language Specification : Because some type information is erased during compilation, not all types are available at run time. Types that are completely available at run time are known as reifiable types . A type is reifiable if and only if one of the following holds: It refers to a non-generic type declaration. It is a parameterized type in which all type arguments are unbounded wildcards. It is a raw type. It is a primitive type. It is an array type whose component type is reifiable. For example: int is a reifiable type (primitive type) List is a reifiable type (raw type) List<?> is a reifiable type (parameterized type with unbound wildcards) List<String> is not a reifiable type (generic type) Class<?> is...
Created by Dr. Xi on July 08, 2011 09:37:03
Last update: July 08, 2011 09:37:03
A security manager is automatically installed when you run an applet, but not so when you run an application. Setting the java.security.manager property enables the default Security Manager for the application.
For example, you can bind to a "privileged" port without security manager:
$ java SocketBind localhost:83
Binding to local...
With security manager, it fails:
$ java -Djava.security.manager SocketBind loca...
Created by Dr. Xi on June 27, 2011 15:49:00
Last update: June 28, 2011 11:13:35
You should drop the generics notation when looking up a method by signature:
import java.util.*;
import java.lang.reflect.Me...
In fact, Class.forName("java.util.List<java.lang.String>") fails with ClassNotFoundException !
However, all information about parameterized types are not lost at runtime. The Java reflection API does provide these methods to get information about parameterized types at runtime:
Method.getGenericExceptionTypes
Method.getGenericParameterTypes
Method.getGenericReturnType
for which the non-generic counterparts are:
Method.getExceptionTypes
Method.getParameterTypes
Method.getReturnType
Created by Dr. Xi on June 27, 2011 13:12:36
Last update: June 27, 2011 13:12:36
Parameterized types are considered different at compile time but not at runtime. This program fails compilation because List<Foo> is not the same as List<Bar> :
import java.util.ArrayList;
import java.util.Li...
Error:
$ javac SameErasure.java
SameErasure.java:8: do...
This also fails because List<Foo> and List<Bar> are considered the same:
import java.util.ArrayList;
import java.util.Li...
Error:
$ javac SameErasure.java
SameErasure.java:11: n...
Created by Dr. Xi on June 22, 2011 15:15:15
Last update: June 22, 2011 15:15:15
There's no such thing as an immutable byte array in Java. What we are trying to achieve here is to protect the byte array inside the object from which it is being returned. We want to caller to have the contents of the byte array, but not be able to change the contents inside the object. The trick is to return a copy instead of the internal reference:
public class ReturnByteArray {
public stati...