Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

1.3 V/S 1.2

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all :
In JDK1.2 there is a bug which DOES NOT allow the following to compile
short s = 's'
when in actuality this should compile and does under JDK1.3 and JDK1.18 Which one do we follow in the exam ?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kedar,
According to me implicit casting of char to short is not allowed.
The graph of implicit casting is like this
byte-->short-->int-->long-->float-->double
char-->int-->long-->float-->double
There is no implicit casting between char to short or vice versa.
And u even can't cast a byte to char implicitly, even thow byte is 8 bits and char is 16 bits.
I hope this helps
Ashish
 
Kedar Jog
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have normally agreed to what you were saying, But I was giving a Mock Exam by appliedreasoning.com and got this one wrong so I compiled the code using JDK1.3 AND JDK1.2 It does compile under JDK1.3 But JDK1.2 does not allow it JLS States that if a var is byte,char,short and the values assigned to it is constant at compile time it is ok if the value is not constanty then it is not allowed ex: this is not aloowed
byte b = 'c'
char c = b;
first line compiles second does not
but
final byte b = 'c'
char c = b; is ok
both lines compile
What do we do in the exam ? ???
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar,
For the purpose of the exam, you have to suspend your disbelief and imagine there are no bugs in any of the compilers
If the JLS says an operation is legal then it is legal for exam purposes. So, in this situation, I would assume the assignment of a <code>char</code> constant or literal is assignable to a <code>short</code> or <code>byte</code> as long as it's value is within the type range ie only characters whose ASCII values are less than 127 can be assigned to a <code>byte</code>
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kedar Jog:
I would have normally agreed to what you were saying, But I was giving a Mock Exam by appliedreasoning.com and got this one wrong


same here... i also took the applied reasoning mock and got that question wrong. i also noticed that the main method works fine without the public modifier in 1.3.

------------------
aka pinboxx
[This message has been edited by Paul Michael Laborte (edited July 13, 2001).]
 
Paul Michael
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got this one from khalid's book... p.48

Narrowing conversions between char and byte (or short) values on assignment always require explicit cast, even if the value is in the range of the destination datatype.
short val = (short) 'a'; // Value in range of short but explicit cast required.

 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
The JLS says it's legal without the cast.


5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is
assigned (�15.26) to a variable: the type of the expression must
be converted to the type of the variable.
Assignment contexts allow the use of an identity conversion
(�5.1.1), a widening primitive conversion (�5.1.2), or a widening
reference conversion (�5.1.4). 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.


Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic