Dave's Brain

Browse - Programming Tips - How to I translate these C/C++ "if" statements to Java?

Date: 2013oct10
Language: Java

Q.  How to I translate these C/C++ "if" statements to Java?


1.
// Question 1 in C/C++ if (pObject) { // ... something }
2.
// Question 2 C/C++ if (nItems) { // ... something }
3.
// Question 3 in C/C++ if (!nItems) { // ... something }
A. 1. Assuming usual naming conventions this C/C++ code is checking if a pointer is non-null. So in Java this is done:
// Answer 1 in Java if (pObject != null) { // ... we can use pObject }
2. Assuming usual naming conventions this C/C++ code is checking if an integer is nonzero. So in Java this is done:
// Answer 2 in Java if (nItems != 0) { // ... there are items }
3. Assuming usual naming conventions this C/C++ code is checking if an integer is zero. So in Java this is done:
// Answer 3 in Java if (nItems == 0) { // ... there are no items }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.