Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

= and ==

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//What is the result of executing the following fragment of code:

class _2{
public static void main(String []args){
boolean flag = false;
if (flag = true) {
System.out.println("true");
}
else {
System.out.println("false");
}
boolean flag1 = false;
if (flag1 == true) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
what is the difference bet. = and == in the argument section of the if statement? what will the answer be.?
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the first case (flag1=true) you are assinging "true" to flag1.So if condition will be always true and hence prints "TRUE".
In the second case ( flag1 == true) ,here you are comparing the value of flag1 ,which is assinged to false, so if condition will be flase and hence will print "False".
Correct if wrong
thanx
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

reads as 'set a to b' or 'a becomes b', whereas

reads as 'Does a denote the same obect as b?'
Hope this helps
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic