Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Implicit Conversion?

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 public class As{
2 int i = 10;
3 int j;
4 char z= 1;
5 boolean b;
6 public static void main(String argv[]){
7 As a = new As();
8 a.amethod(); }
9 public void amethod(){
10 System.out.println(j);
11 System.out.println(b);
12 }
13 }
1) Compilation succeeds and at run time an output of 0 and false
2) Compilation succeeds and at run time an output of 0 and true
3) Compile time error b is not initialised
4) Compile time error z must be assigned a char value
The answer is 1, but shouldn't be answer as 4 'coz we are assigning int to char in line 4. I compiled this and i am getting results 0 and false.
Any comments?
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You are printing out
System.out.println(j);
System.out.println(b);
So what has line 4 char z= 1 got to do with the code,i think you know the class level variables get initialized to their default values in this case int j will be assigned to 0 and boolean b to false,so the given answer is correct.
Surya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but surya at time of compiling, we r assigning 1 (int) to char. it's not question what i am using in main method. How come compiler allowing to assign int to char without casting?
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think i understood your question wrongly,anyway coming to assigning int to char this happens to be a widening conversion,From JLS
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
These are all the examples of widening conversions,as long as the int value is in therange which char can accept the compiler implicitly converts it into int,but if the int value is not in the range of char the it gives the compiler error saying explicit cast needed to convert int to char.For example this is fine char c=65000 but this will result in a compile error
char c=65555.Hope i am clear this time.
Surya
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 4 is a narrowing primitive conversion (see JLS 2e 5.1.3), which normally requires a cast. There is a special case, however, that doesn't. If the left-hand side of an assignment is a variable of type <code>byte</code>, <code>char</code>, or <code>short</code> and the right-hand side is a constant expression of type <code>char</code>, <code>byte</code>, <code>short</code>, or <code>int</code> which is representable in the left-hand variable, then the assignment is allowed without an explicit cast. See JLS 2e 5.2.
Since <code>1</code> is representable in a <code>char</code> variable, the compiler allows the assignment. If you'd used a constant expression outside the allowable range for <code>char</code> then you'd get a compile-time error.
jply

[This message has been edited by Jerry Pulley (edited September 22, 2000).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic