A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
What is the output for First Snippet and Second Snippet?
Joined: Jan 03, 2007
Jan 03, 2007 07:05:00
public static void main(
final int i = 100;
byte b = i;
public static void main(String args)
int i = 100;
byte b = i;
Joined: Mar 22, 2005
Jan 03, 2007 07:21:00
Welcome to JavaRanch.
That's quite easy to find out, isn't it?
Ping & DNS, my free Android app for network diagnostics
Joined: Jun 29, 2006
Jan 03, 2007 09:26:00
The First Snippet will compile, run and print 100.
The second Snippet will not compile.
In the First Snippet, i is cmpile time constant and its value 100 is in the range of byte so no explicit casting is required.
In the Second Snippet, i is not compile time constant. So, you have to explicitly cast the value of integer i to byte.
byte b = (byte)i will work.
[ January 03, 2007: Message edited by: Neel Joshi ]
Joined: Feb 24, 2006
Jan 03, 2007 09:40:00
Is that a quiz or a question ?
SCJP 6 articles
SCJP 5/6 mock exams
More SCJP Mocks
It is sorta covered in the
JavaRanch Style Guide
subject: What is the output for First Snippet and Second Snippet?
Rule about final?
how about this test?
automatic primitive conversion from higher to lower precision?????
Can't figure please help!
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2013