File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt 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 "doubt " Watch "doubt " New topic
Author

doubt

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
which of the following cause a compiler error?
A.float f=4096.0;
B.double d=4096.0;
C.byte b=4096;
D.char c=4096;
The key answer is A and C.why not D.
To assign a character we should include in single quotes.As 4096 is integer we need to explictily cast it to character.Please clarify.

In the exam cram book I saw the following question.
Q.The component in the east position of a borderlayout can not be wider than a scrollbar.
A.True
B.False
The key answer is B.I couldn't understand.Why is it so?Please explain.
Thanks in advance.
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
JLS 5.2 In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
1. The expression is a constant expression of type byte, short, char or int.
2. The type of the variable is byte, short, or char.
3. 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.
class Testdeepa {
public static void main(String[] args) {
char c = 4096; // Narrowing primitive conversion for Integer constants.
System.out.println(c);
}
}
Output : ?
Therefore permissible range for char is 0 to 65535
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
thanks.
could u please give me some example of the following line.
1. The expression is a constant expression of type byte, short, char or int.
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
Originally posted by deepa:
thanks.
could u please give me some example of the following line.
1. The expression is a constant expression of type byte, short, char or int.

Sure, here are your examples :
byte b1 = 42; // 42 is a constant expression of type int
------------------------------------------------------
final int i = 10; // NOTE : s is a final variable
byte b2 = i; // i is a constant expression of type int
------------------------------------------------------
short s = 25; // 25 is an integer constant
------------------------------------------------------
char c = 65; // 65 is an integer constant
------------------------------------------------------
Therefore only integer constant expressions can be used as expressions.
According to my understanding, The JLS mentions byte, short, char along with int in the following statement :
"The expression is a constant expression of type byte, short, char or int"
because they want to include identity conversion as well such as :the following types :
char c = 'c';

Note :
A byte value must be an integer value in the range -128..127.
A short value must be an integer value in the range -32768..32767.
A character literal must be a valid unicode value - i.e., a character literal enclosed in single quotes or an integer value in the range 0..65535 or
an escaped 3-digit octal value in the range \000..\377.
(note that a character literal is not an integer value)
This point is important in Java
It means an 'a' is not equvalent to integer 97 like in C or C++.
a 'a' is simply an Unicode 'a' although internally they may be stored as an unsigned 16 bit integer.
Therefore its not possible to say byte b = 'a';
but byte b = 97 is allowed because 97 is representable in a byte.

Now I have a question for u deepa,
tell me why is the following code an error :
class Testdeepa {
final short s = 42;
byte b = s;
}
The above code is as per the JLS specificaition yet it gives an error !
Can anyone explain the reasoning that A byte value must be an integer value in the range -128..127.
Why can't it be a short like in the above example ?


[This message has been edited by Deepak M (edited July 18, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt