• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

here is my doubt about Integer literal.... help

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

This is aditya makam. I am a new member of this group. I hope u ppl help me. Here is my doubt.

In java if you compile the following code.

class ByteTest {
public static void main ( String args [ ] ) {
ByteTest t = new ByteTest(10);
}
};

It will give CompileTime error.Because compiler is thinking that 10 is an int literal,but in Byte class only two constructors are there which take arguments as byte ,String.But if consider the following code,


class ByteTest1 {
public static void main ( String args [ ] ) {
byte b =10;
System.out.println(b);
}
};

It will get compiled and will print 10 as output.

My doubt is ,here also 10 is an integer literal and we are trying to store in byte variable.But it is not giving compile time error.Why is it so?

Plz anybody who can explain plz give me reply...to this message.


yours friendly
aditya
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

Assignment conversion occurs when the value of an expression is assigned (�15.25) 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 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.

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason the first example doesn't work is because there is no overloaded constructor that takes a byte. I've added the constructor in this example:


Note that when ByteTest t is created, it's argument must be cast as a byte or it won't compile, because the compiler sees 10 as an int.

Your second example works because you declare and initialize byte b and set it to 10. Since the value 10 is a valid byte, the code complies.

Hopefully that made sense and I know what I'm talking about...
 
aditya makam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic