Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock exam Q compile time constants

 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jdk 1.4.0
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
preeti i am using the same compiler but i found compile time error in 3 and 4 line
 
Tom Purl
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic