• 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

confused with switch statement

 
Greenhorn
Posts: 3
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks

Have some confusing points on switch statement, below code and statements are extracted from the scjp reference material



Need some explanation about the below points(if can have deep explanation much appreciated )

1. byte is implicitly cast to int?
2. second argument is (128) to large for byte?

Thank You
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check your private messages for some administrative matter...
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let we represent it in this way...



As you see below...

int a = g;

Here we do not required to cast byte to integer.
But in switch case we are passing byte and which will consider max limit upto 127 and that's why we need to set it upto limit 127.
And if it's more then 127, as shown here we need to cast it.

As well at the time of initializing the variable, if require to set more then 127, it also required to case and give us answer "hello2".

byte g = (byte) 128;
 
Adri Jamis
Greenhorn
Posts: 3
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ket bhav, Thanks a bunch for your generous effort to clarify the point . My 2nd question is solved, But i still do need bug you for the 1st question, please point me out where exactly, byte is implicitly cast to int my example(this code is taken from scjp ref. material)
;
 
Ranch Hand
Posts: 40
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

please point me out where exactly, byte is implicitly cast to int my example



byte is not implicitly cast into int in the switch statement. So this will give a compilation error if not cast into byte



ExampleByte.java:8: possible loss of precision
found : int
required: byte
case 128:
^
1 error

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

Adri Jamis wrote: please point me out where exactly, byte is implicitly cast to int my example(this code is taken from scjp ref. material)


byte is never implicitly casted to int...It is int which is implicitly casted to byte. See by default the all whole numbers like 23,55,1000,2055 etc all are of int data type by default (i.e. integer literals are of type int by default). So when you are writing 50 is of type int but since it is stored in byte it is implicitly casted to byte and now the question arises why implicit cast happened??? It happened because the value which i was storing (i.e. 50 in above example) was well within the range of byte which is -128 to 127.So the summary is whenever you are storing a number between -128 to 127(which are int by default) in a byte variable there will be an implicit cast. So when you are storing an number greater than 127 in byte you need to do an explicit cast. Example
I hope this answers your question number 1.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rameshwar Soni wrote:when you are writing 50 is of type int but since it is stored in byte it is implicitly casted to byte and now the question arises why implicit cast happened???


that is because 50 is a compile time constatnt. however below code wont compile


what is compile time constant?
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks seetharaman for finding out the mistake.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rameshwar Soni wrote:Thanks seetharaman


You are welcome
 
Adri Jamis
Greenhorn
Posts: 3
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
ket bhav
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks alot to all of you my friends...
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic