This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Potential error on page 61 chapter 2 of Sybex of OCA Java SE 8 Programmer I by Boyarsky and Selikoff

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Trying to set a short variable to an int results in a compiler error."
should be
"Trying to set an int variable to a short results in a compiler error."

Right?

Example:
int y = 5;
short x = y; // cannot convert from int to short
 
Marshal
Posts: 69035
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the book is correct. Maybe not immediately clear, but correct.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typecasting required  for convert premitive int to short




public class NewP1 {
   public static void main(String[] args) {
       int x=5;
       short y=(short)x;
       System.out.println("type caste required to int to shor  becuse int required 4 bites and shorts only 2 bits");
   }
}
 
Campbell Ritchie
Marshal
Posts: 69035
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, you can cure the compiler error with a cast. Or maybe by declaring the int with the final modifier.
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic