• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

public class Test8{
public static void main(String a[]){
byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}
Which of the following are correct?

A1 Error at mark 1
A2 Error at mark 2
A3 Error at mark 3
A4 Error at mark 4
The answer is error at mark 1 and 4
Again,

public class Test9{
public static void main(String a[]){
final byte b = 1;
char c = 2;
short s = 3;
int i = 4;

c = b; // 1
s = b; // 2
i = b; //3
s = c * b; //4
}
}
Which of the following are correct?

A1 Error at mark 1
A2 Error at mark 2
A3 Error at mark 3
A4 Error at mark 4

The answer is error at mark4.Please can anybody explain what difference the modifier final makes? Also i often get confused about the casting and conversion,can anybody elaborate in simple language?

Thankyou
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main point of this question is, what does the compiler know?

Consider this declaration.



1 is an int, but because 1 is a constant at compile-time, its value can be placed into a byte because it is known that 1 is small enough to be stored in a byte.

But then if you say

,

then the compiler will complain. Because all that it knows about b is that b is of type byte. A general byte cannot be stored directly in a char. The byte must first be converted to an int before it is stored in the char. This
requires a cast.

However, if you have the declaration

,

then the compiler knows the value of b, not just that it is of type byte.

Since the value of b is a constant at compile-time, the compiler knows it can place the value of b into a char.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your first question, you get loss of precision errors at both marks 1 and 4 in the following code.



The reason for this is the implicit casting that occurs when you use the assignment operator.

Mark 1 is a widening conversion, so you might expect it to be fine. The issue is that byte (and short) are signed in Java; char is unsigned, so it is possible to lose information during this conversion.

Mark 4 has a slightly different issue. The result of multiplying a char will be an int. Trying to assign the result to a short is a narrowing cast.
reply
    Bookmark Topic Watch Topic
  • New Topic