Skip to content

Instantly share code, notes, and snippets.

@LukaJCB
Created September 2, 2016 16:44
Show Gist options
  • Select an option

  • Save LukaJCB/cc764a075caf177ef28dc303c1a89d3a to your computer and use it in GitHub Desktop.

Select an option

Save LukaJCB/cc764a075caf177ef28dc303c1a89d3a to your computer and use it in GitHub Desktop.
Exemplifies the lack of safety when using Java
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