0

I have one question,in the basic concepts of java its written that int can hold whole numbers,string can hold names,then why

int=day is true in switch case

28th Jun 2017, 5:17 AM
Ashwin Sengupta
19 Respostas
+ 8
When you do switch(day), the integer stored inside day is evaluated under case. If case wherein day is 1 is fulfilled, the string is printed. It is the same as : if (day == 1) { System.out.println("Monday") } else if (day == 2) { // ... } This does not mean that the string value is stored inside day.
28th Jun 2017, 5:30 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
String, int, are datatypes (keywords). 2, 18381, -173 are values which can be stored in int variables. "Hi", "Test", "This is a string", "20", "394" are values which can be stored in String variables. E.g. int some_num = 3939; // store 3939 into integer variable some_num E.g. String some_str = "Hello"; // store "Hello" into String variable some_str
28th Jun 2017, 5:29 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
So this code isn't saying "int = day" this is evaluating what day is equal to, then running some statements depending on the value of day. That's why day is in the switch. switch(day){} // we are testing the value of day case 3: Means, do this if whatever was in the switch is equal to 3. So, if (day == 3) Then do that.
28th Jun 2017, 5:31 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
day would just be a variable of type int. It's not actually saying: is the int equal to "day".
28th Jun 2017, 5:21 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
No, var is not a keyword in java.
28th Jun 2017, 5:23 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Guessing won't really get things working. Can you show the code that made "int = day" give you true?
28th Jun 2017, 5:27 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Here's an example to follow if you need to https://code.sololearn.com/cG3t2bOHihjg/?ref=app
28th Jun 2017, 8:45 AM
Limitless
Limitless - avatar
+ 2
You can do String = Day but that means you'll need to add a more structured input that will remove as much human error as possible.
28th Jun 2017, 8:46 AM
Limitless
Limitless - avatar
+ 1
I think you should show the code in its entirety. This way you may get a better explanation.
28th Jun 2017, 5:28 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You're comparing int to int and outputting a String. This is correct.
28th Jun 2017, 5:30 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
oh,means it saying that int=3
28th Jun 2017, 5:33 AM
Ashwin Sengupta
+ 1
from what I know, int is a data type, it's a reserve word, you can't use it to hold values. it's use to identify what kind of data can a variable hold.. comparing values should be "==" not "=". single equal sign is used for assignment.. please try to be clear... thanks and, can we see the structure of your switch case? thanks
29th Jun 2017, 10:50 PM
Mr. Yoh So
Mr. Yoh So - avatar
+ 1
if what you mean about the "int = day" is that "day" is declared as String then you use it on switch case using numbers like case 1 case 2 and so on.. I guess we have to remember that numbers can be a String, there's no problem comparing it on switch case. The problem I guess takes place when day is declared as String and you use it on mathematical operations. But you can still do mathematical operations on variables such as "day" declared as String, by casting it's value to int.. do like this, (int) day. šŸƒšŸƒšŸƒ
29th Jun 2017, 11:12 PM
Mr. Yoh So
Mr. Yoh So - avatar
0
if i write var instead of int is it correct
28th Jun 2017, 5:23 AM
Ashwin Sengupta
0
im not understanding,plz tell me all these things briefly and in long
28th Jun 2017, 5:24 AM
Ashwin Sengupta
0
if we write string instead of int is it correct
28th Jun 2017, 5:24 AM
Ashwin Sengupta
0
public class Program { public static void main(String[] args) { int day = 3; switch(day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; } } }
28th Jun 2017, 5:29 AM
Ashwin Sengupta
0
why int=day
28th Jun 2017, 5:30 AM
Ashwin Sengupta
0
why string =day not
28th Jun 2017, 5:30 AM
Ashwin Sengupta