| Author |
What is the output for First Snippet and Second Snippet?
|
Mohammed Imran
Greenhorn
Joined: Jan 03, 2007
Posts: 2
|
|
First Snippet: class MyClass { public static void main(String []args) { final int i = 100; byte b = i; System.out.println(b); } } Second Snippet: class MyClass { public static void main(String []args) { int i = 100; byte b = i; System.out.println(b); } }
|
 |
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35258
|
posted

0
|
Welcome to JavaRanch. That's quite easy to find out, isn't it?
|
Android apps – ImageJ plugins – Java web charts
|
 |
Neel Joshi
Greenhorn
Joined: Jun 29, 2006
Posts: 13
|
|
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 ]
|
SCJA(96%)
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6592
|
|
|
Is that a quiz or a question ?
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
 |
|
|
subject: What is the output for First Snippet and Second Snippet?
|
|
|