aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unicode charector as String literal; finalize() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unicode charector as String literal; finalize() method" Watch "Unicode charector as String literal; finalize() method" New topic
Author

Unicode charector as String literal; finalize() method

Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
I have two questions.
1.In one of the mock exams, I came across an option like "Unicode characters like '\u0048' may be used to create String literals." and was given correct in the answers.
But if tried
It printed \u0048. Is it the correct way to use Unicode charectors in String literals.

2. Is the finalize() method guarenteed to be called exactly once before an object is garbage collected?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

1.) Don't escape the backslash or it will be printed as a normal backslash!


2.) Only use finalize() if you really need to and know what you are doing. There's almost nothing guaranteed with finalize() and garbage collection. The JVM could be shut down before any object gets collected so the method wouldn't be called at all. If you'd do a long-running task in finalize() it could be interrupted before even getting finished. So it's usually better not to use finalize().

Marco
[ June 05, 2008: Message edited by: Marco Ehrentreich ]
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Thanks Marco.

But how about this

source: Valentin's Mock Exam.

The code doesn't compile.

Why cannot we use more than one Unicode charectors in String literal?
[ June 06, 2008: Message edited by: Ram Manoj ]
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Ram,

you can do what you tried to do, but I think you have an invalid Unicode thats why it wont compile. If you try,


this should work.

I hope that helps.


SCJP 1.5
Ram Manoj
Ranch Hand

Joined: Jan 12, 2008
Posts: 52
Ya thanks Robert.

Probaby must be '\u000a' is an invalid unicode charector.

But in the question of Valentin's mock exam, it was given as

'\u0048' is 'H' and '\u000a' is a linefeed (LF)).
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Ram,

I stumbled upon this useful text in the FAQ section, I think this is a more precise explanation of whats going on here.

Regards
Robert.


char a = '\u000A'. Why is this invalid?

Unicode escape characters of the form '\Uxxxx', where xxxx is a hexadecimal value, are processed very early in the translation process (see JLS 3.10.4 ). As a result, the special characters '0A' (line feed) and '0D' (carriage return) are interpreted literally as "end of line."

For example, the expression...


char A = '\u000A';
...therefore becomes...


char A =;
...which results in a compile-time error.

To avoid this error, always use the special escape characters '\n' (line feed) and '\r' (carriage return).

Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117

2. Is the finalize() method guarenteed to be called exactly once before an object is garbage collected?


There is no guarantee the method is called at all, because the garbage collector may not collect the object. But, if it is called, it is called only once. Even if you uneligiblize the object in the finalize() code by assinging a (useful) reference to that object for example, the garbage collector remembers that finalize() was already called for that object and if that object becomes eligible again for the garbage collector, finalize is not called again.
[ June 10, 2008: Message edited by: Ronald Schild ]

Java hobbyist.
 
Consider Paul's rocket mass heater.
 
subject: Unicode charector as String literal; finalize() method