This discussion is archived
1 2 Previous Next 15 Replies Latest reply: May 10, 2013 1:40 AM by EJP RSS

Logic of Two Different Outputs for Same Code

profdeepaks Newbie
Currently Being Moderated
Kindly read the following two classes.
Why the same code generates two different outputs:

Class 1:
iimport java.util.Scanner;
public class Text1
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter month");
String m = sc.next();
switch(m)
{
case "JAN" : System.out.println(1);
break;
default : System.out.println("Wrong Input");
break;
}
if(m=="JAN")
System.out.println(1);
else
System.out.println("Wrong Input");
}
}
Output:
1
Wrong Input

Class 2:
public class Text2
{
public static void main(String s)
{
switch(s)
{
case "JAN" : System.out.println(1);
break;
default : System.out.println("Wrong Input");
break;
}
if(s=="JAN")
System.out.println(1);
else
System.out.println("Wrong Input");
}
}
output :
1
1

Edited by: profdeepaks on May 3, 2013 5:39 AM

Edited by: profdeepaks on May 3, 2013 5:40 AM
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points