课程: Java for All Platforms: Desktop, Web, and Mobile Development

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Conditional statements

Conditional statements

- [Instructor] Conditional statements allow you to run a block of code if a certain condition is met. Let's take a look at how that works. As an example, let's create an Integer variable. We'll just call it myInteger and we'll set it equal to null. Remember as we've talked about earlier, this particular data type, Integer with a capital I, allows null values. So let's say we're trying to print the value of this. System.out.println. myInteger.toString. So we're taking the integer, converting it to a string and printing it out. Watch what happens when we try to run the application. We get this NullPointerException because we're trying to run a method on an object that is set to null. So one way to handle this is to use conditional statements. Underneath the variable declaration line, type the keyword if to create a conditional statement and in parentheses, myInteger not equals null. So we're only going to run…

内容