|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Easy Learn Java: Programming Articles, Examples and Tips - Page 496
Previous
1060 Stories (530 Pages, 2 Per Page)
Next
Java String is immutable, but why String Class has those methods...
|
Question: I know from Java Lessons that String is immutable. But if all strings are immutable, why the String class has different method on strings modification?
For example look at this code snippet:
| Code: |
String lc = "lowercase";
lc.toUpperCase();
System.out.println(lc);
|
produces printout:
It seems very stupid to have methods that a priori will not work. Why the String class has such methods?
Answer: That's right - strings are immutable and every time when you write "String lc" you get new immutable string. Also methods which has return type String also create absolutelly new String.
So if you rewrite your code snippet like this:
| Code: |
String lc = "lowercase";
String new_lc = lc.toUpperCase();
System.out.println(new_lc);
|
you will get printout:
LOWERCASE
In my example you assign function's result to new string and it differs from original string. Probably you understand now how you should use methods from the String Class. You have to assign their's result to new String.
Even if you assign it to the same variable name, like this (!!!!!) it works:
| Code: |
lc = lc.toUpperCase();
|
You get printout:
LOWERCASE
It is interesting result, is not it?
Reference: class String description in SUN's API: String
1152 bytes more | comments? | | Score: 0
|
Posted by aalex on Thursday, March 01, 2007 (06:33:29) (12038 reads)
|
Why java parsers allow schema validation to be disabled?
|
Question: Why SAX, DOM and JAXB allow schema validation to be disabled?
Answer: Many Java XML parsers, for example JAXB, DOM and SAX, can improve processing speed and/or to process documents containing invalid or
incomplete content.
That's why they allow schema validation to be disabled.
For example, JAXB supports these processing scenarios also. Error handling is done by the
exception handling. In
general, if a JAXB implementation cannot unambiguously complete unmarshalling
or marshalling, it will terminate processing with an exception.
If the client application does not set an event handler on its Validator, Unmarshaller,
or Marshaller prior to calling the validate, unmarshal, or marshal
methods, then a default event handler will receive notification of any errors or
warnings encountered. The default event handler will cause the current operation
to halt after encountering the first error or fatal error (but will attempt to continue
after receiving warnings).
456 bytes more | comments? | | Score: 0
|
Posted by aalex on Tuesday, February 27, 2007 (07:52:34) (2213 reads)
|
|
|