wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock exam Q compile time constants 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 "mock exam Q compile time constants" Watch "mock exam Q compile time constants" New topic
Author

mock exam Q compile time constants

preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
class A {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3
byte b2 = c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c2; //8
}
}

when I compiled it gave compile error on line 3, 4, and 8
Dan exam answer says Compiler error at 3 , Compiler error at 4 only
explanation: However, variabls s2 and c2 are compile time constants that fall within the range of the left hand operand.
http://www.danchisholm.net/dec04/guide/mughal/chapter3/exam3ans.html
Question :3
So is line 8 valid?/
Monisha Talwar
Ranch Hand

Joined: Feb 11, 2003
Posts: 102
Preeti,
When I compiled the same code, I got errors on
3, 4, 7 and 8!

Incompatible type for declaration. Explicit cast needed to convert short to byte.
byte b1 = s1; //3
^
Incompatible type for declaration. Explicit cast needed to convert char to byte.
byte b2 = c1; //4
^
Incompatible type for declaration. Explicit cast needed to convert short to byte.
byte b3 = s2; //7
^
Incompatible type for declaration. Explicit cast needed to convert char to byte.
byte b4 = c2; //8
^
4 errors
However, the JLS says:
In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
The expression is a constant expression of type byte, short, char or int.
The type of the variable is byte, short, or char.
The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

So according to that 7 and 8 should be valid.
Wonder why I am getting a compilation error at both 7 and 8!!


Hey! It compiles! Ship it!
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
I am using jdk 1.4.0
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
hi,
preeti i am using the same compiler but i found compile time error in 3 and 4 line


Thanks<br />Badri
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
Hey Preeti! I also only got errors for lines 3 and 4 and I am using the 1.3.1 jdk. I think that there may be a typo in your code if you're seeing additional errors.
I think what Dan is trying to say is that the right operand in lines 7 and 8 will do an automatic narrowing conversion, so no caste is necessary. Their values are known at compile time. However, lines 3 and 4 need the caste.
[ April 02, 2003: Message edited by: Tom Purl ]

Tom Purl<br />SCJP 1.4
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
u know what guys.. I use the IntelliJ IDE and it kept showing a red error line.. (usually it means compile error)... but I compiled it and ran it and it worked fine... So in conclusion, final constants which are assigned act like compile time constants and hence can be assigned...
However the following code does not compile, gives loss of precision error at 8 and 10:
public class test25{
static final char c4;
final char c5;
static{
c4 = 3;
}
test25(){
c5 = 10;
}
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = (byte)s1; //3
byte b2 = (byte)c1; //4
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7
byte b4 = c4; //8
System.out.println(b4);
new test25().test251();
}
public void test251(){
byte b5 = c5; //10
System.out.println(b5);
}
}
I am trying to use static / non-static final member variables even though they are in the byte range
So I can assume that this applies only to compile time constants which have assigned values
thanks for all the replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mock exam Q compile time constants