• Post Reply Bookmark Topic Watch Topic
  • New Topic

String in Switch Statement datatypes JDK 8  RSS feed

 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code will not compile due to many errors. This code is in OCA Java SE 8 study guide by Jeanne Boyarsky and Scott Selikoff Page 75
1) My question is we are passing value of firstName and how we are comparing that to lastName,middleName, and other stuff.

2) Also in solution it is mentioned that case middleName will not be able to compile because it is not final variable. Is final missing in method declaration(String firstName) as firstName is also not marked final how it's datatype is different from middleName?

3) 3rd case statement will compile without issue because suffix is final constant variable. Same question here how it is comparable to firstName while firstName is not marked final?

4) In the forth case statement, despite lastName being final, it is not constant variable. <- please explain this point i am not able to understand this.



I may be stupid in not understanding simple words. Please enlighten me with your knowledge.

Moderator edit: Please UseCodeTags. I have added them for you this time.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the value of lastName? You must be able to tell the compiler such a value before it can compile a switch statement.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use variables as a case in a switch statement? The answer to this should help you understand why some of the examples do not compile.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Bye wrote:Can you use variables as a case in a switch statement? The answer to this should help you understand why some of the examples do not compile.


I know the answer to case 5 and 6 as they are not same datatype, therefore it will not compile. One is int and other is char and we are comparing it to String that's why it will not compile.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what is the value of lastName? You must be able to tell the compiler such a value before it can compile a switch statement.


I did but compiler is saying constant string expression is required in both 2nd and 4th case of switch statement. What is constant string expression ? and how it is different from final String datatype?
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linkon Manwani wrote:I did but compiler is saying constant string expression is required in both 2nd and 4th case of switch statement. What is constant string expression ? and how it is different from final String datatype?


My previous comment may not have been as helpful as I first thought now I re-read it again. Look at the following example:



Is that legal? Why?

How about this:



Is that legal? Why?

And finally, this:



Is this legal? Why?

From your answers to the above 3 examples, what do you learn about the final keyword and about parameters declared in method signatures?

Are you able to answer your own question now?
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jdk version 1.8.0_31 and i have tried the following code and it is running fine.

package experiments;

public class Exper1{
public static void main(String...strings){
String name = "Hello";
String hh = "There";
foo(name);
}

static void foo(String str){
switch(str){
case "world":
System.out.println("No Matc found");
break;
case "Hello":
System.out.println("Found match");
break;
case "There":
System.out.println("There");
break;
}
}
}

as per my knowledge, Strings are immutable. So they are implicit final variables. and in a switch statement we can only put constant values in case statement.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil kumar sharma wrote:as per my knowledge, Strings are immutable. So they are implicit final variables. and in a switch statement we can only put constant values in case statement.


Correct. What happens if you want to use a String object from a variable as a case within the switch statement instead of a String literal?
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The effect will be the same. It doesn't matter if you are using a String literal or a String object created by new operator.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are missing the point of the question.

What if we do:

instead of:


Is this still legal?
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it is not legal. Because here in this code you are putting a String reference in case statement, which is variable by nature.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linkon Manwani wrote: . . . compiler is saying constant string expression is required in both 2nd and 4th case of switch statement. . . .
The compiler creates a lookup table for a switch statement with the values and the code in. It must know which String you are going to use. It needs to be able to include "hello" and "there" in the bytecode. So it absolutely must know what the value of the String after case is. If the String can be followed back to literals and the compiler can be sure there have been no changes, it can then work out the actual String.The compiler can be certain that name1 and name2 have not been changed and can confidently work out what to put after case. In fact it probably records the values of all final variables until they go out of scope. But what if you didn't mark either of those Strings as final. The compiler cannot be expected to go through the code and check whether any of those variables has been reassigned. Not unless you want to take twenty minutes to compile a 100‑line program So there are definitions for constant expressions in the Java® Language Specification (=JLS). Beware: the JLS can be difficult to read.

You must realise there is a difference between immutability and final‑ness. Because Strings are immutable you cannot writeAll fields of Strings (except, apparently its hash code) are final but that is to ensure immutability. It does not mean a String reference is final. You can writeYou cannot expect a compiler to check for the absence of reassignments, so a non‑final String cannot constitute part of a constant expression.

Many people call constant expressions “compile‑time constants”.
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ritchie for the details. They are indeed compile time constants until we explicitly make them final.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

I think you mean they are only compile time constants if we make them explicitly final.
If they are not final then they are variables and cannot be regarded as constants.
 
nikhil kumar sharma
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we must declare a variable final or static final and initialize it to desired value to make it a compile time constant.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and it has to be one of a restricted range of datatypes.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil kumar sharma wrote:as per my knowledge, Strings are immutable. So they are implicit final variables. and in a switch statement we can only put constant values in case statement.


Just a comment on the bit in bold.
Immutable does not in any way imply final.

The following is valid:


The following is not:

 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Bye wrote:


No, this is not legal Reason : String is final and we cannot assign anything after declaration. In this case it will be null.

Ashley Bye wrote:
How about this:



This will also not able to compile same reason as above but Percy is assigned to final String name as it is not instance variable.

Ashley Bye wrote:
And finally, this:


Is this legal? Why?


Yes this is legal as Private Static String name is not final so we can change the value of this variable.
finally, this.name = "Percy" and also name= "Percy" but this will be final and cannot be modified furthur.

Thanks a ton your code really helped me getting my doubt cleared.

Yes now i can answer my own question
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Campbell-Ritchie for the detailed Explanation.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome and I shall presume thanks to everybody else who helped.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!