Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsai maddukuri
9,108 Pointsif i use conditions such as if(networkInfo.isConnected() && networkInfo!=null) getting error
Sir since && first checks the left side of the operation if(networkInfo!=null && networkInfo.isConnected()) is running without any error but if i use conditions such as if(networkInfo.isConnected() && networkInfo!=null) if(networkInfo!=null & networkInfo.isConnected()) or if(networkInfo.isConnected()) is facing error at runtimeLogcat says that it is runtime exception so i want to know when to use it!!!!
1 Answer
Seth Kroger
56,413 Pointsif(networkInfo.isConnected() && networkInfo!=null) and if(networkInfo.isConnected()) are both really the same. You are calling the isConnected() method before making the null check since the two conditions of the && are evaluated left to right. If networkInfo is null you'll get a exception for calling a method on a null object. So, it you don't get the exception you already know it isn't null.
With the other case, & is a different operator than &&. && is logical-AND and takes two true/false conditions on each side. & is called bitwise-AND and manipulates the binary digits of numbers, as they are represented in the computer memory. You will rarely, if ever, use bitwise operators these days though there are some special cases.