Created
September 2, 2016 16:44
-
-
Save LukaJCB/cc764a075caf177ef28dc303c1a89d3a to your computer and use it in GitHub Desktop.
Exemplifies the lack of safety when using Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ExampleActivity extends AppCompatActivity { | |
| private String foo = "Bar"; | |
| @Override | |
| public void onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| foo = getSomething(); | |
| doSomething(foo); //perfectly valid Java code | |
| } | |
| } | |
| // Change to this for safety: | |
| class ExampleActivity extends AppCompatActivity { | |
| private String foo = "Bar"; | |
| @Override | |
| public void onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| foo = getSomething(); | |
| if (foo != null){ | |
| doSomething(foo); //Now we know it's not null, right? Do we? | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment