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
19 Réponses
+ 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.
+ 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
+ 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.
+ 2
day would just be a variable of type int.
It's not actually saying: is the int equal to "day".
+ 2
No, var is not a keyword in java.
+ 2
Guessing won't really get things working.
Can you show the code that made "int = day" give you true?
+ 2
Here's an example to follow if you need to
https://code.sololearn.com/cG3t2bOHihjg/?ref=app
+ 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.
+ 1
I think you should show the code in its entirety. This way you may get a better explanation.
+ 1
You're comparing int to int and outputting a String. This is correct.
+ 1
oh,means it saying that int=3
+ 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
+ 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.
🏃🏃🏃
0
if i write var instead of int is it correct
0
im not understanding,plz tell me all these things briefly and in long
0
if we write string instead of int is it correct
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;
}
}
}
0
why int=day
0
why string =day not