File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

few scenarios in JAVA where instinct doesn'n play right

 
Arijit Daripa
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case can only take integers or enum constants...
 
Abhinav Das
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic