my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Q - char to byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Q - char to byte

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
class JSC201 {
static byte m1() {
final char c = '\u0001';
return c; // 1
}
static byte m3(final char c) {
c = 's'; //I added this line
return c;} // 2
public static void main(String[] args) {
char c = '\u0003';
System.out.print(""+m1()+m3(c));
}
}
I dont understand why line 2 is a compiler error. The method m3() is returning c, which is a compile time constant, since its declared as final,
AND I am assigning a value to it.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Cathy.
A simple name is a compile-time constant if (1) it is declared final and (2) it has a variable initializer and (3) that initalizer is a compile-time constant.
final int x1 = 10;
final int x2 = x1;
final int x3 = x1 + x2;
final String s1 = "abc";
final String s2 = s1;
A final parameter has a value at run-time - the argument of the method invocation.
final int y1; // no
final int y2 = y1; // no
final Object o1 = null; // no
final Object o2 = new Object(); // no
final String s3 = new String("abc"); // no
[ October 16, 2003: Message edited by: Marlene Miller ]
Agandhi
Greenhorn

Joined: Oct 17, 2003
Posts: 6
Hi Marlene,
I understood your explaination, except for final String s3 = new String("abc"). Why can't we call it as compile-time constant?
Thanks..
Anthony Roy
Greenhorn

Joined: Oct 08, 2003
Posts: 8
The JLS says this:
15.28 Constant Expression
ConstantExpression:
Expression
A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:
* Literals of primitive type and literals of type String
* Casts to primitive types and casts to type String
...
i.e. Objects cannot be constant expressions, and new String("abc") is an Object (and not merely a string literal).
It seems to me that a good way of thinking about strings is somewhere between primitives and Objects, String objects seem to me to be a sort of wrapper around a string literal, much like Integer is a wrapper around int, but with automatic wrapping and unwrapping enabled (much as the Integer and other wrapper classes will behave in J2SDK1.5).
Of course the technical details may be much different to this, but I find it a good way to understand what is going on in these cases.


Ant...<br /><a href="http://www.antroy.co.uk" target="_blank" rel="nofollow">www.antroy.co.uk</a>
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Thanks Marlene and Anthony.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by ArchanaG Reddy:
Hi Marlene,
I understood your explaination, except for final String s3 = new String("abc"). Why can't we call it as compile-time constant?
Thanks..

The String referenced by s3 is not created until run-time. Since it does not exist at compile-time, it can not be a compile-time constant.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
String objects seem to me to be a sort of wrapper around a string literal,
I think it would be safer to think of a string as a wrapper around a char array. Take a peek at java.lang.String.

Now a char array is also an object, they say. This object has unnamed variables of type char, they say. So here is my abstract image of these ideas (It looks better in Visio.)

And I know it is hard to get used to, but the JLS says a string literal is a reference to an instance of class String.
That is, the source code �abc� represents a (32-bit or whatever) number that identifies some object on the heap. At some point the virtual machine replaces the source code string �abc� with a real reference.
Every now and then these details help me to analyze some tricky problems.
[ October 17, 2003: Message edited by: Marlene Miller ]
pola
Greenhorn

Joined: Oct 20, 2003
Posts: 11
Hi,
I am new to this forum and i would be thankful if you could tell me what Dans'Q....is it a mock exam or ??....give me the link .....
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Hi Vamsi
Welcome to the Ranch. Here's the link to Dan's website:
http://www.danchisholm.net
Good luck !
Regards
Harwinder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's Q - char to byte
 
Similar Threads
Help in Final variables
Dan's Mock Exam
Compile-time Constants
question on unicode
Help on Dan's exam 1