• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubt

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Don't touch me. And dont' touch this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic