• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

few scenarios in JAVA where instinct doesn'n play right

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
case can only take integers or enum constants...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Screaming fools! It's nothing more than a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic