aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the output for First Snippet and Second Snippet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the output for First Snippet and Second Snippet?" Watch "What is the output for First Snippet and Second Snippet?" New topic
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: 41865
    
  63
Welcome to JavaRanch.

That's quite easy to find out, isn't it?


Ping & DNS - my free Android networking tools app
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: 6661
    
    5

Is that a quiz or a question ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the output for First Snippet and Second Snippet?