9/24/2020 1 CSE 476 Dennis Phillips Mobile Application Development 1 Java Stuff Things you need to know about Java Or just might find handy… CSE 476 Dennis Phillips Mobile Application Development 2 Are you sure what this does? int a1 = 1; int b1 = 1; String a = "This is string " + a1; String b = "This is string " + b1; textView1.setText(a); textView2.setText(b); if(a == b) { textView.setText("Equal"); } else { textView.setText("Not equal"); } Output: This is string 1 This is string 1 ??? 1 2
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
9/24/2020
1
CSE 476 Dennis PhillipsMobile Application Development11
Java Stuff
Things you need to know about Java
Or just might find handy…
CSE 476 Dennis PhillipsMobile Application Development22
Are you sure what this does?
int a1 = 1;int b1 = 1;
String a = "This is string " + a1;String b = "This is string " + b1;textView1.setText(a);textView2.setText(b);
CSE 476 Dennis PhillipsMobile Application Development33
String equality tests
if(a.equals(b)) {textView.setText("Equal");
}
a == b tests if the objects are the same, not if the contents are the same
CSE 476 Dennis PhillipsMobile Application Development44
Inheritance
Inheritance creates new classes based on classes that have already been defined. The capabilities of the base class are inherited by the derived class.
CSE 476 Dennis PhillipsMobile Application Development55
Inheritance
We also say:
HatterView is derived from ViewView is a superclass of HatterViewView is a base class of HatterView
CSE 476 Dennis PhillipsMobile Application Development66
Polymorphism
The ability of an object of one type to appear as and be used as another type.
Lines lines = new Lines();Drawable item = lines;
The variable lines is a reference to an object of type Lines.The variable item is a reference to the same object, but only knows it as a Drawable (it just knows that much about the object)
Polymorphism applies when classes are derived from other classes
CSE 476 Dennis PhillipsMobile Application Development77
The Key Idea of Polymorphism
An object can be known by its class or any class it is derived from.
Example:
Screen – A screen in a computer game. Can be 2D or 3D. Scene – A scene in a computer game. 3D only. Derived from Screen.CityScene – A scene set in a city. Derived from Scene.
We create an object for a city scene. We can pass that object to functions that expect a reference to a Scene or Screen.
Think of it this way: A CityScene is a Scence. A Scene is a Screen; A CityScence is a Screen;
CSE 476 Dennis PhillipsMobile Application Development88
Polymorphism in Java
Suppose we have this code:
The variable item is a reference to an object that is actually of type Lines(). That is the underlying type. We can get it back using a down cast:
Lines lines = new Lines();Drawable item = lines;
Lines linesObj = (Lines)item;
But, if item is not a reference to a Lines object, you will get a runtime exception
CSE 476 Dennis PhillipsMobile Application Development1717
Synchronous Tasks
public class Client {
public Client() {worker = new Worker();
worker.doSomeWork();}
}
public class Worker {
public Worker() {
}
public int doSomeWork() {// Do somethingreturn 0;
}
}Worker completes all if its work before it returns. This is a synchronous task You use synchronous tasks all
of the time…
CSE 476 Dennis PhillipsMobile Application Development1818
Asynchronous Tasks
public class Client {
public Client() {worker = new Worker();
worker.doSomeWork();}
}
public class Worker {
public Worker() {
}
public void doSomeWork() {// Start doing something//...
}
private void workIsDone() {// Tell the client we are done...//...
}}
I have a worker class. I ask it to do some work. doSomeWorkimmediately returns. Worker continues to do the work and later needs to tell Client it is done. This is called an asynchronous task.
CSE 476 Dennis PhillipsMobile Application Development2121
UML
Client has a reference to the worker, so it can call functions on the worker. Worker has a reference to the client, so it can call functions on the client.
Classic bidirectional association
CSE 476 Dennis PhillipsMobile Application Development2222
But…
Worker class has turned out to be handy. Client2 wants to also use it.
public class Client2 {
private Worker worker;
public Client2() {worker = new Worker(this);
worker.doSomeWork();}
public void workerIsDone() {
}}
public class Worker {
private Client client;
public Worker(Client client) {this.client = client;
}
public void doSomeWork() {// Start doing something//...
}
private void workIsDone() {// Tell the client we are done...client.workerIsDone();
CSE 476 Dennis PhillipsMobile Application Development4444
Static Nested Classes
private static class Parameters {
public String imagePath = null;public int hat = HAT_BLACK;
}
Static nested classes work like C++ nested classes. They do not have the association with the enclosing object. You can’t call member functions or access variables outside the nested class itself
They cost less than regular nested classes if you don’t need that access. You can instantiate them anywhere they are visible. And, they make some other things possible…
CSE 476 Dennis PhillipsMobile Application Development4747
Serialization
Object Serialization
Deserialization Object
Byte Stream
Byte Stream
Serialization converts a Java object into a stream of bytes. Deserialization reverses the process
CSE 476 Dennis PhillipsMobile Application Development4848
What does it take?
private static class Parameters implements Serializable {private static final long serialVersionUID = -2846716033219475583L;public String imagePath = null;public int hat = HAT_BLACK;
}
But, every class Parameters is derived from must be serializable.
Every member of Parameters must be serializable (with one exception).
You are supposed to provide a variable serialVersionUID with an integer value. Some IDE’s will generate one for you.
“If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class”. Java docs
CSE 476 Dennis PhillipsMobile Application Development5555
Using static member variables to share data
public class SomeActivity extends Activity {
private static ApplicationData data = new ApplicationData();
public static ApplicationData getApplicationData() {return data;
}
//…
You can also allocate the object in the constructor, so it does not exist until you need it. Use sparingly: you are consuming memory as long as your application exists.
In some other activity:
ApplicationData data = SomeActivity.getApplicationData();
If you need to do this much, look at extending the Application class. It has functions that will indicate if memory is getting low.
CSE 476 Dennis PhillipsMobile Application Development5656
Another approach…
public class SomeActivity extends Activity {
private static ApplicationData data = null;
public static ApplicationData getApplicationData() {if(data == null) {
CSE 476 Dennis PhillipsMobile Application Development6161
try/catchEditText editText = (EditText) dlg
.findViewById(R.id.dlgFloatOne);float x;try {
x = Float.parseFloat(editText.getText().toString());
} catch (NumberFormatException ex) {
// Value is badeditText.requestFocus();editText.selectAll();return;
}
What you put in the try block will be tried. If it throws an exception, control moves immediately to the catch block
CSE 476 Dennis PhillipsMobile Application Development6262
Multiple catch blocks
try {// Parse the URLURL url = new URL(COMMUNITY_URL);
// Create a new HTTP connection// and get a streamHttpURLConnection connection = (HttpURLConnection)url.openConnection();int responseCode = connection.getResponseCode();if(responseCode == HttpURLConnection.HTTP_OK) {
stream = connection.getInputStream();}
} catch(MalformedURLException ex) {return;
} catch(IOException ex) {return;
}
If more than one exception is possible, create a catch for each.
Some exceptions MUST be handled. You have to have a try around them.