This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conversion pblm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conversion pblm" Watch "conversion pblm" New topic
Author

conversion pblm

sunitha thejaswi
Ranch Hand

Joined: Nov 14, 2005
Posts: 31
class Black {
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
}}

in this example compiler error is thrown only atline 3 and 4

whynot at 7 and 8 please explain.

Thanks in advance
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Because ...they (s1 and c1) are not compile time constants and so have to be explicitly casted

Here s2 and c2 are....

You can remove the error..by explicit casting..

There are certain rules for implicit casting...


[ December 07, 2005: Message edited by: A Kumar ]
sunitha thejaswi
Ranch Hand

Joined: Nov 14, 2005
Posts: 31
thank you kumar for such a fast reply..
so if the the variables are compile constants you assign
a short variable which is final to a byte...doesn't this need explict casting..i am little confused in this topic please expalin..

thank you,
sunitha
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Check this thread...Hope it helps you..

http://www.coderanch.com/t/251301/java-programmer-SCJP/certification/byte
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Implicit narrowing primitive conversions on assignment can occur in cases where all of the following conditions are fulfilled:

1. The source is a constant expression of either byte, short, char , or int type
2. The destination type is either byte, short, or char type.
3. the value of the source is determined to be in the range of the destination type at compile time.



class Black {
public static void main(String[] args) {
short s1 = 1; //1
char c1 = 1; //2
byte b1 = s1; //3 here value of source(s1) is in the range of destination(byte). but (s1) is not a constant ( not final),no implicit casting
byte b2 = c1; //4 same
final short s2 = 1; //5
final char c2 = 1; //6
byte b3 = s2; //7 source s2 is a constant (final) and value of source(s2) is in the range of destination (b3-byte)
byte b4 = c2; //8 source c2 is a constant (final) and value of source (c2-char, promoted to int) is with in the range of destination (b4-byte)
}}

Hope this helps u
Regards
Naresh
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: conversion pblm
 
Similar Threads
mock exam Q compile time constants
Data Types
final variables
Dan Chisholm Mock #1
Compile time constant