aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes few scenarios in JAVA where instinct doesn'n play right Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "few scenarios in JAVA where instinct doesn Watch "few scenarios in JAVA where instinct doesn New topic
Author

few scenarios in JAVA where instinct doesn'n play right

Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Grammatically Java is almost perfect, i.e., if one is tought a section, one's instinct comes into play in other sections.
But in some scenarios instinct fails, like
1.

Can anyboby change the value iOb refers? No, because iOb is final and Integer objects are immutable. So following must compile.

But the above doesn't compile. The argument is that case needs constants and iOb is an object and can be changed. Well! Contents of objects reffered by final variable can change. But, but, but, if the object is not immutable.
Here nobody can change the value of iOb, so it can be considered in case. But compiler treats it as other non-immutable objects.

2.

To make the compiler happy we have make variable i a compile time constant

so not even the following works

So it is compiler's own way to work satisfying its instinct.
If anyone has such kind of examples, please post them.
Those will help Java Programmers especially SCJP aspirants.


SCJP 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Can anyboby change the value iOb refers? No, because iOb is final and Integer objects are immutable. So following must compile.


You are reading too much into the error message. The items in case must be constants -- specifically compile time constants. And the definition of a compile time constant is very specific.

The compiler is not saying that the object is mutable or not mutable. It is also not saying whether it can change or not. It is merely saying that it is not a compile time constant.

Maybe in the future this will change -- but currently a compile time constant can only be applied to primatives and to strings.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

I didn't even notice that there were two questions... sorry.

To make the compiler happy we have make variable i a compile time constant


If "i" is not a compile time constant, there is no way for the compiler to determine that the "if" expression is "always true". Hence, no way to tell if "j" has been initialized.

so not even the following works


This is because the definition of compile time constant is very specific. And splitting the expression doesn't satisfy the requirements. Maybe in the future, the compiler (and specification) will be modify to be able to detect this case -- currently, it doesn't.

Henry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

case can only take integers or enum constants...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhinav Das
Greenhorn

Joined: Aug 11, 2008
Posts: 9
Hi,

Using enum in case like the following does not make much sense:
public class SwitchTest {
public static void main (String [] args){
switch(Direction.EAST){
case EAST:
sayThis("EAST");
break;
case WEST:
sayThis("WEST");
break;
default:
sayThis("This is default");
}
}
private static void sayThis(String message){
System.out.println(message);
}
enum Direction{
EAST,WEST,NORTH,SOUTH;
}
}

Why is that one cannot use "switch(Direction)" instead?

-Thanks
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Originally posted by Henry Wong:

And the definition of a compile time constant is very specific.

Wopuld you please define what a compile time constant is.
Sandhya Bhaskara
Greenhorn

Joined: Jun 29, 2005
Posts: 23
Even I'm looking for the exact definition of compile time constant.

Consider the below example



Answer :Compilation fails . 7 needs to be explicitly cast to short


I can understand that compiler looks for an int method by seeing 7(literals are ints by default) and since it did not find matching method it will give an error.

But why cant the compiler cast 7 to short. Isn't 7 a compile time constant?We are not sending an int variable to the method in which case I totally agree with compiler error


Sandhya Bhaskara<br />SCJP 1.4,SCWCD 1.4,SCBCD 1.3,SCJP 6
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2718
    
    6

Originally posted by Sandhya Bhaskara:

Answer :Compilation fails . 7 needs to be explicitly cast to short

I can understand that compiler looks for an int method by seeing 7(literals are ints by default) and since it did not find matching method it will give an error.

But why cant the compiler cast 7 to short. Isn't 7 a compile time constant?We are not sending an int variable to the method in which case I totally agree with compiler error


Sandhya you have already asked this question in a different thread . Please dont ask the same question multiple times.Read this for more information.

The section 15.28 of this link specifies what exactly is a compile time constant.


Hope this helps
[ October 07, 2008: Message edited by: Amit Ghorpade ]

SCJP, SCWCD.
|Asking Good Questions|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: few scenarios in JAVA where instinct doesn'n play right