• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unicode charector as String literal; finalize() method

 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ram Manoj
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic