What is Java?



Java Basics for Android DevelopmentWhat is Java?Android applications are developed using the Java language. As of now, that’s really your only option for native applications. Java is a very popular programming language developed by Sun Microsystems (now owned by Oracle). Developed long after C and C++, Java incorporates many of the powerful features of those powerful languages while addressing some of their drawbacks. Still, programming languages are only as powerful as their libraries. These libraries exist to help developers build applications.Some of the Java’s important core features are:It’s easy to learn and understandIt’s designed to be platform-independent and secure, usingvirtual machinesIt’s object-orientedAndroid relies heavily on these Java fundamentals. The Android SDK includes many standard Java libraries (data structure libraries, math libraries, graphics libraries, networking libraries and everything else you could want) as well as special Android libraries that will help you develop awesome Android applications.Note: Java is Not Related to JavaScriptOne of my favorite sayings about this topic is “Java is to JavaScript as Car is to Carpet.” There is absolutely no relationship between the two languages. However, knowing JavaScript can help you understand Java, because some of the basic components and ideas are similar. The two languages are written differently and work very differently, but both allow us to work with programming features like variables, methods, operators, and even objects. And the logic behind the language is the same, so knowing how to use variables, methods (or functions), and loops will make learning Java much easier; at that point you just need to learn the?syntax?of Java, which is how you explicitly need to write certain things like variable declarations and method ponents of the Java Programming LanguageLet’s take a look at how the language of Java is expressed in the code we write. The language itself is a collection of keywords and symbols that we put together to express how we want our code to run. Each line of code has certain rules (or grammar) about how it needs to be constructed and what is or isn’t allowed.Remember, programming languages are?abstractions, meaning that they abstract the true underpinnings of how a computer operates into things we can more easily understand. Behind the scenes it really is all ones and zeroes, but we don’t need to concern ourselves with that. We can think in these abstract terms to express our ideas and commands, and these abstractions apply across all different types of programming.Basic Data TypesIf programming really is about working with data, then we’d better familiarize ourselves with the basic data types used in Java. These are some of the keywords that indicate the type of data we’ll use and store in variables.int?– An integer value, i.e. a whole number (no decimals) that includes zero and negative numbers.float?– A floating point value that includes as many decimal places as it can hold. Because?the decimal place can change, or float, it’s important to know that these values may technically be imprecise. When precise decimals are needed, like for currency, we should use the?BigDecimal?data type.boolean?– A 1-bit?true?or?false?value that can only be in one of those states. The Java keywords for the values are “true” and “false”, which represent 1 and 0, respectively.char?– A single character, such as the letter “A” or the symbol “#”. Note that lowercase and uppercase characters are different, so “a” and “A” are two different characters.String?– String data is a bunch of characters strung together to make text, like a banner strung up at a party.The first four data types in the list above,?int,?float,?boolean, and?char, are primitive data types, which means that they are relatively simple and straightforward. Other primitive data types include?byte,?short, and?long. For more information about Java’s primitive data types, visit the?documentation from Oracle.The “S” in?String?is capital because?String?is a more complex data type. Strings are?actually objects, and the naming convention in Java is that object names should start with a capital letter. An object is different than a primitive data type because it has more complex properties and methods available to it, whereas the primitive data types are limited and straightforward. For example, the?Stringobject has a method called?length()?that tells us how many characters are in the?String, but?the?int?data type does not have any methods like that.VariablesA variable is basically a container used to hold data. The data itself can be anything from a simple number to coordinates for a location on a map to a URL for a video on the web.?As we just learned, Java is a statically-typed language, which means that we need to explicitly declare what type of data a variable is supposed to hold. Let’s take a look at an example.The line above is a?statement?that declares a variable named “title” that holds String, or text, data. It also assigns the text “Java Basics for Android” to the variable. Let’s examine each numbered part:The first word in a variable declaration is the data type, which tells us what kind of data the variable will hold.The second word is the name of the variable, which can be anything you want following a few basic rules. Variable names must not contain any spaces or special characters; they can only have letters, numbers, and underscores. They must not start with a number, though.The equals sign (=) is an?operator, meaning it performs a specific operation for us. This is the?assignment?operator, meaning that we use it to assign values to variables. In this example it is assigning the text value on the right (#4) to the variable “title” on the left (#2).The text in green is the String value we are working with. In Java, Strings are surrounded with double quotes to differentiate them from regular text used in the code.Finally, the last character in this line is the semicolon, which is used to finish this statement. Semicolons in Java are like periods in sentences: we use them to indicate when we’re done saying something. Every statement in Java must end with a semicolon (note that a statement may be displayed on multiple lines for better readability).Some other examples of variable declarations using some of the basic data types we covered are as follows:int playerNumber = 24;float distance = 11.72011f;boolean isEmpty = true;char firstLetterOfName = 'B';Note the “f” at the end of the float value that indicates the number is a floating-point value.?Also note that?char?values are surrounded with single quotes to differentiate them from?String?values, which use double quotes.MethodsA method is a section of code that we can call from elsewhere in our code, and the method will perform some action or return some kind of result that we can use. Methods are used to organize our code into reusable (and understandable) chunks that save us a lot of time and energy.Let’s use a simplified example of the?length()?method mentioned above for the?String?data type to see how a method is defined and called and how it helps us.01 public int length() {02 int numberOfCharacters = 0;03 ... code to calculate the number of characters ...04 return numberOfCharacters;05 }Line 1 declares the method named?length. The first word declares the?visibility?of the method and is often either?public?or?private?(though a few other options are available). “Public” means that it’s publicly available to call from anywhere in our app. “Private” methods are only available within the class they are defined in (more on classes in little bit).The second word in the method is the data type that will be returned. In this case the method will return a number, or?int. If the method will run some code but not return any kind of data, then the keyword?void?is used instead of a data type.Next up is the name of the method. Method names follow roughly the same rules as variable names: letters, numbers, and underscores, but they cannot start with a number.Directly after the name we see two parenthesis with nothing in between them. The parenthesis are required, but when empty like this it means we do not pass any data or variables when we call the method. If one or more variables (with data types) were included between the parenthesis, then we would need to pass in appropriate values or variables inside the parenthesis when we call this method.Line 1 ends with an opening curly brace, and a matching closing curly brace is on line 5. In Java, blocks of code, like the code that make up a method, are often surrounded by curly braces to designate all the code that should be run. In this case it means that all the lines of code between the curly braces will be run each time we call the?length()?method.The last thing I want to mention from this example is the code on line 4. The?return?keyword designates that the variable (or data) on the line after the?returnkeyword is what will be returned to whoever called this function. In this example we’ve calculated the total number of characters that make up the text of the String and stored the value in an?int?variable named?numberOfCharacters. This variable is returned, meaning that the actual number stored in the variable will be the return value of the?length()?method.Calling a MethodTo use a method, we need to call it from our code and, if it returns a value, store the return value somewhere. ?Here’s an example of calling the?length()?method:String sampleText = "Java is fun";int textLength = sampleText.length();First we have a String value that has 11 characters (because white space is included). We call the method on the next line using?dot notation, which uses a period after a Class or variable name to call the method from. We can’t just call it like?int textLength = length();?because in this incorrect example, there is no point of reference for where the?length()?method is defined or what text it should be checking. By chaining it to the?sampleText?String variable, we are saying to use the?length()?method defined by the?String?class, and to use it on the text data held in the?sampleText?variable.In this case the?length()?method calculates a value of 11 and returns it to this code where it was called from. We do something with the return value by storing it in a new int variable named?textLength.Major Benefit of Using MethodsImagine that we have five different String variables that we need to know the lengths of. If we didn’t have a?length()?method to use, we’d have to write the code to calculate the length five different times. But be creating a reusable method, now we only need one line of code each time we want to determine the length of a String value. Programming is all about efficiency, so use methods whenever possible to organize your code and save yourself mentsIn some of the code samples below I’m going to make notes in the code using comments. Comments in Java code are lines of code that don’t have any effect on how the program runs. They are purely informational, meant to help us understand how the code works or is organized.Single Line CommentsSingle line comments begin with two forward slashes:?//. Everything on the line after the forward slashes makes up the comment and is completely ignored by the program.// This is a single-line commentString code = "4-8-15-16-23-42"; // Only this part is a commentMulti Line CommentsSometimes we want longer comments that span more than one line. In that case we use an opening marker to mark the start of the comment:?/*, and a closing marker to mark the end of the comment:?*/./* This is a multi-line comment. Though only the opening * and closing markers are required, we often add an * asterisk at the beginning of each line to make it * more readable, and end the comment with the ending * marker on a new line. */Classes and ObjectsIn general, files of code in Java are organized as classes. A file (with the file extension .java) usually contains one class definition, and it gets compiled into a machine-readable class file (with the file extension .class).Remember from?Part 1?that Java is an object-oriented programming language, meaning that it is built around the concept of objects. Objects can be representations of physical objects, like a mug, or more abstract objects, like a connection to a website. A class is meant to define an object and how it works. In this way, a class is like a blueprint of an object. Classes define things about objects as properties, or member variables of the class. And abilities of the object are defined as methods.Let’s take a look at a class that could be used to represent a simple connection to a website:public class UrlConnection { // Member variables (properties about the object) private String mUrl; // Methods (abilities of the object) public void setUrl(String url) { mUrl = url; } public void connect() { // Code to make an HTTP connection goes here }}In this example we have one member variable?mUrl?which represents the URL that this object is intended to connect to. The lower-case “m” prefix is a convention to indicate?member variable. This helps with scoping issues as we can see in the setUrl() method where a different variable named “url” is clearly different from the member variable for URL. We also have two methods, or things this object can do. The first is called?setUrl()?and is used to set the private member variable (more on “private” in a minute), and the second is called?connect()?and would be where code to establish a connection to the URL would go.A simple example demonstrating how a robot could be represented as an objectACCESS MODIFIERSFor classes, member variables, and methods, we usually want to specify who can access them. In some cases we want things to be publicly available, meaning that anyone can see and do things with them. Other times, we may want to keep things private to protect data from the outside world or accidental changes. The keywords?public,?private, and?protectedare access modifiers that control who can access the class, variable, or method.Access ModifiersSame ClassSame PackageSubclassOther PackagespublicYYYYprotectedYYYNprivateYNNNno access modifierYYNNPackagesClasses are often grouped together along some kind of criteria into packages. A package is simply a collection of related classes and are usually named in a format that follows?reverse domain notation, meaning you take the URL for your website and reverse it, i.e. becomes com.teamtreehouse.A Note on CastingIf you’ve worked through?Build a Simple Android App?or?Build a Blog Reader App, or if you’ve done any Android development at all, then you’ll be familiar with the concept of “casting”. We often see it when getting Views from a layout in code:Button saveButton = (Button) findViewById(R.id.saveButton);The?findViewById()?method returns a generic View object, which is useful because it can return anything from the layout. All widgets like Buttons and TextViews are subclassed from the View base class. But since each widget behaves differently and has different properties, we want to declare it with the most detailed data type we know it to be. In most cases we will know the type of widget being returned from the?findViewById()?method, so we specify its exact type with a?cast.View myView = findViewById(R.id.saveButton); // validButton saveButton = findViewById(R.id.saveButton); // invalidCasting in Java, like the cast to Button in the example above, is done by declaring the type we are casting to inside parenthesis in front of the method that’s returning an object. For more details about casting in Java, please visit?Oracle’s documentation on classes and inheritance.ConditionalsOne of the basic building blocks of all programming languages is the power to check a condition and then do something if the condition is met. In Java this achieved using the?if?conditional, which checks if a condition evaluates to boolean values?true?or?false.if (name.equals("Bruce Wayne")) { isBatman = true;}We start with the keyword?if, and the condition to test is surrounded by parenthesis. If the condition is met, then the code inside the curly braces is executed. Java, like many other languages, uses curly braces to start and finish blocks of code like this. If we want to take some action if the condition is not met, then we can use the?elsekeyword to specify what code should run, as in the following example:if (name.equals("Bruce Wayne")) { isBatman = true;}else { isBatman = false;}Further, we can chain as many conditions together as we want using?else if, like this:if (name.equals("Bruce Wayne")) { isBatman = true;}else if (name.equals("Clark Kent")) { isSuperman = true;}else { isRegularHuman = true;}Extra CreditDepending on your experience with programming, you may be wondering why the examples above use the equals() method instead of double-equals, like if (name == “Bruce Wayne”). The reason is because in Java, applying the double-equals operator to String objects?does not compare the actual values of the objects, just whether or not the references are equal, which usually isn’t what we’re looking to compare.ArraysArrays are structures used to organize multiple pieces of data or other variables. Basically they are structured lists where each piece of data is referenced by its position in the array.Android App Drawer as ArrayTake this example of an App Drawer from an Android phone. We can think of the app icons in the drawer as items in an array. The first item in the array, Gmail, is at index 0. The second, Go SMS Pro, is at index 1, and so on to the last, Camera, at index 4. The length of this array (or the number of items in it) is 5.When we declare an array variable, we need to specify the type of the items of data we’ll be holding in the array. For example, if we’re going to use an array to store a list of names, then we will need to declare it as a String array (since String is the data type for text).Declaring arrays is similar to declaring variables; we simply need to add square brackets after the data type, like this:String[] appNames;We can initialize an array in one step or multiple steps. In one step, we simply define all the elements of the array inside curly braces, like these two examples:String[] colors = { "Red", "White, "Black" };int[] ages = { 18, 24, 33 };If we want to load data in the array at a later time, the we still need to specify how big the array will be. (We can’t change the number of items in an array–if we want that we need to use a different data structure like an?ArrayList.) Specific items in the array are accessed and/or assigned using the index of the array. The index is specified inside square brackets following the name of the array. Arrays in Java start with an index of zero, so the first item of an array is at position zero and is referenced like this:String[] appNames = new String[5];appNames[0] = "Gmail";appNames[1] = "Go SMS Pro";appNames[2] = "Chrome";appNames[3] = "Falcon Pro";appNames[4] = "Camera";String firstApp = appNames[0]; // firstApp will be "Gmail"The amount of items an array can hold is known as its length. The Array object has a property called?length?that returns this number:int numberOfApps = appNames.length; // will be 5LoopsLooping through collections or sets of things is another basic building block of programming. For example, let’s say we want to loop through that list of app names from the Arrays section above and output them to a log. If we did it one by one, the code would look like this:Log.d(TAG, appNames[0]);Log.d(TAG, appNames[1]);Log.d(TAG, appNames[2]);Log.d(TAG, appNames[3]);Log.d(TAG, appNames[4]);What if we have hundreds of apps? Or what if we’re dealing with millions of records from a database? What if we run 20 lines of code for each record in the database? That’s a lot of code to write, and most of it is repeating ourselves, which violates the?“Don’t Repeat Yourself (DRY)” principle.The solution is to loop through an array (or whatever collection you’re dealing with) to run code for each item in the collection. Let’s rewrite the code above as a few different loops.The “for” Loopfor (int i = 0; i < appNames.length; i++) { Log.d(TAG, appNames[i]);}Let’s walk through this syntax, because there’s a lot here if you’ve never seen it before. The basic idea is this: “for (some conditions) do the code inside these curly braces”:for (initialize statement; terminate statement; step statement) { // code to be executed each step of the loop}This type of for loop often utilizes a counter variable (named “i” for “index” in our example above) that keeps track of our position in the collection.initialize statement?– The first statement inside the parenthesis sets up the counter variable or whatever mechanism will be used to keep track of our position in the loop. In our example the variable “i” is set to 0, the first index of the appNames array.terminate statement?– The next statement inside the parenthesis (separated by a semicolon) tells us when this loop should stop processing and exit to the next line of code after the closing curly brace. In our example we want to keep going as long as “i” is less than the number of items in the array. Since there are 5 items in the array, but the last item is at index 4 (because it starts at 0), we want to stop once i surpasses 4. Otherwise we will get an?ArrayIndexOutOfBoundsException.step statement?– This last statement (again separated by a semicolon) defines how our counter will change after each step is executed. In our example we are using a special “increment” shortcut that adds one to an integer value.?++?can be used to increment a variable by one, and?--?can be used to decrement a variable by one.We must be careful in defining these conditions. What if we decrement our counter instead of incrementing it, and our terminate condition is never met? In such cases our program or app will get caught in what is known as an “infinite loop”. The loop will never exit and our program will use up resources indefinitely, which will lead to a crash, freeze, or “Application Not Responding (ANR)” in Android.The “while” LoopAn alternative to the?for?loop is a?while?loop, which executes code while a condition is met. The biggest difference is that we need to define how the loop steps through the collection in the code between the curly braces instead of being a statement inside the parenthesis where the loop is started.01 int i = 0;02 while (i < appNames.length) { 03 Log.d(TAG, appNames[i]);04 i++; 05 }Note that the three statements inside the for loop are here as well: line 1 is the initialize statement, line 2 contains the terminate statement, and line 4 is the step statement. The general syntax is as follows:while (condition) { // code to execute}Each type of loop can often be rewritten as another, so basically we can choose whichever makes the most sense for how we want to loop. Sometimes it’s easier to write one or the other based on the data we’re working with.The do-while LoopSimilar to the?while?loop, the?do-while?loop is different in that it checks the condition at the bottom of the loop instead of the top. This guarantees that the code inside the loop will get executed at least one time. If the condition is not met in the first pass of a loop, the?while?loop will skip the code inside the curly braces, but the?do-while?loop will process the code and then move on.int i = 999;do { Log.d(TAG, "Just looking at app names");} while (i < appNames.length);The for-each LoopSometimes those iterators and conditions can get plain ugly. Fortunately, Java contains yet another way of writing a loop when you want to iterate over a collection of items. The idea behind a?for-each?loop is that you want to run some code?for each?item in the collection.for (String name : appNames) { Log.d(TAG, name);}The?for-each?loop replaces the three conditions of the for loop with a simplified declaration. The right side of the colon is the array or collection to be looped through, and the left side is a temporary variable used to hold the item in the current iteration of the loop. So in this example we get a String from each item in the appNames array and the String is stored in a temporary variable named “name”. We can read this as “for each name in appNames, do the code in these curly braces”.Error Handling: try-ing Code and catch-ing ExceptionsRemember that?ArrayIndexOutOfBoundsException?I mentioned in passing above? Java provides us with a framework for gently handling all sorts of errors in our programs. The basic idea is that if something bad happens in our program or app, the Java runtime system will kind of raise a little flag that says something is wrong. If we are on the lookout for these flags we can handle them appropriately and save our users from a crash or bad experience.The act of raising that little flag is called throwing an exception, and we must catch them in code in our app. Let’s use an example to illustrate:01 String url = "";02 try {03 URL blogUrl = new URL(url);04 }05 catch (MalformedURLException e) {06 Log.e(TAG, "Error creating a URL object with " + url);07 }Okay, so what’s going on with these blocks? Up til now, we’ve seen code that executes normally in the main thread of the program. If any exceptions are thrown in that type of code, then they are very serious and the app will crash. But oftentimes there are methods we want to use that throw less serious exceptions, and we are not allowed to ignore them. In such cases, we need to put that code inside a?try?block, like lines 2-4 above. In this example, the URL constructor throws a?MalformedURLException?if a bad URL is given as the input parameter. If we don’t catch that exception (or some superclass of it, like?Exception) then our code will not compile.The?catch?block requires the type of exception to be caught to be declared as a variable inside the parenthesis after the?catch?keyword. Then, as usual, the code to be executed in case of that exception is written between the curly braces.If we need to check for multiple exceptions, like if we tried to open an HTTP connection using blogUrl, which throws an?IOException, then we can chain?catchstatements on after another, like this:try { // some code}catch (MalformedURLException e) { Log.e(TAG, "There was an error creating a URL object.";}catch (IOException e) { Toast.makeText(this, "Error connecting to site.", Toast.LENGTH_LONG).show();}*Note that no other code can go between the ending curly brace of one?catchstatement and the?catch?keyword of the next.Finally, there is one more block of code that can be chained to this sequence: the?finally?block. This is where we put code that we want to execute even if an exception is thrown. In the example below, we open a connection to a file for writing and want to make sure we close it no matter what happens. Otherwise that connection might be open indefinitely:Writer writer;try { writer = new OutputStreamWriter(new FileOutputStream("out.txt")); writer.write("Ben was here.");}catch (IOException e) { Log.e(TAG, "Error writing to file.");}finally { if (writer != null) { writer.close(); }}*Note that the?writer?variable will be null if an exception was thrown trying to initialize it. Hence we need to check if it’s null or not, because we only want to close it if it was initialized. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download