• 沒有找到結果。

Data Types: Primitives and Objects

在文檔中 Learning Android (頁 31-34)

Java is an object-oriented, statically typed language. Object-oriented is a programming paradigm that is based on the concept of objects. This idea is often analogous to that of the real world, where we have things (such as cars and people) and the things have properties (such as doors and legs) and behavior/actions (such as turning right and walking). What statically typed means is that Java checks the declaration of the data type of every variable in the program at compile time. This enforcement of the data type ensures that variables cannot change what they mean within the program once they have been declared (e.g., a number cannot be swapped for text or vice versa). The types of data fall into two camps: primitive data types and objects.

Following are the eight primitive data types in Java:

boolean

1-bit true (1) or false (0) value

byte8-bit signed whole number (no decimals) with values ranging from –128 to 127

short

16-bit signed whole number with values ranging from –32,768 to 32,767

int 32-bit signed whole number with values ranging from –2,147,483,648 to 2,147,483,647

long64-bit signed whole number with values ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

Single-precision 32-bit floating-point number (has decimal) double

Double-precision, 64-bit floating-point number

charA single, 16-bit Unicode character; for example, the letter “A” (note that “a” and “A”

are different characters)

The other data type that everything else falls under is an object. An object is a complex type in that within each object are a variety of properties (also called fields or vari‐

ables) and methods (also called functions). All objects are defined by a blueprint called a class (making objects an instance of a class). In many cases, the class is described as a file with the file extension of .java (such as SimpleExample.java) and is compiled into a machine-readable file with the file extension .class (see Example 2-5).

Data Types: Primitives and Objects | 13

Taking a look at the SimpleExample.java class (Example 2-4), there is only one variable that is declared within it: number (private int number). This integer number is not expressly assigned and so by default is set to 0 (all number cases default to the value 0 respective to their type). In the case of a boolean, the default value is false, whereas in the case of char it is \u0000 (in other words, zero expressed as a UNICODE value). If a variable is an object and it was not assigned anything, the default value would be something called null. null is a special value that means “not assigned” or “unknown”

(it’s a bit more complex than that, but we are trying to keep things simple).

Continuing on with the example, there is a method called setValue() that takes in int (integer) as its input and then sets the number variable to that integer. To access the value of the number variable, another method is declared called getNumber() that returns the number variable. These two are examples of what a typical method declaration may look like. A method declaration is made up of six pieces:

Modifier

This defines the access type (e.g., public) and kind (e.g., static).

Return type

This defines the data type that is returned (e.g., int). If no data type is to be returned, void is used.

NameThe name of the method.

Input parameter list

A comma-delimited list of parameters preceded with their data type (e.g., int val, String str, double num).

Exception list

A comma-delimited list of exception types that are thrown by the method.

BodyThe logic/code between the braces of the method.

There is what looks like a method using the name of the class but does not have a return type (public SimpleExample()). This is called a constructor—its purpose is to enable object instances of the class to be instantiated via the use of the new operator (Sim pleExample example = new SimpleExample()).

Modifiers

Modifiers are split into two categories: access modifiers (public, protected, private, and no-access modifier) and nonaccess modifiers (static, final, strictfp, abstract,

synchronized, volatile, transient, and native). We will cover the access modifiers and only one of the nonaccess modifiers, static.

Access modifiers define the level of access a method or variable has. It is a type of security in that a hierarchy of control is established when using them:

public

Everyone can see and access this code.

protected

The class this is defined in, classes within the same package, or classes that are subclasses of the class this is defined in can view and access this code.

default/nonaccess modifier

The class this is defined in and classes within the same package can view and access this code.

private

Only the class that this is contained within can see and access this code.

When an object is instantiated from the class blueprint, it has a distinct copy of the instance variables of its own. With the use of the static modifier, the variable is associated directly with the class and only one is ever created. This static variable becomes a com‐

mon variable across the object’s instances of the class. In the same way, static methods are only accessible at the class level. For example, the main method (public static void main(String[] args)), ensures that only one main method exists for this class and exists as an entry point to the execution of the program.

Arrays

Looking at the main method’s input, we have the variable args. args is a string array (String[] args) denoted with the []. An array is a container object that holds a fixed number of values of a specific type—in essence, it is a list of values. The declaration of the array sets the type that is held within each element of the array, and the size is fixed when it is assigned (see Example 2-8). Each element of the array is accessed by its nu‐

merical index, which is a number representing where it is located in the ordered list.

Note that the index starts at 0 and increments up by 1 until the last element is one less than the total size.

Example 2-8. Array declarations and value assigning

double[] someArray; // declaring

someArray = new double[4]; // assigning size of 4

int[] integerArray = new int[10]; // declaring and assigning size of 10

Arrays | 15

integerArray[0] = 32; // assigning the first element

integerArray[9] = 0; // assigning the last element // another way to declare and assign

// declaring and assigning 3 elements directly String[] anotherArray = {"Some String","a","strings"}

Operators

Operators are special characters that denote actions performed on a variable. Such operators include things such as basic math, boolean logic, and assignment. The oper‐

ators have an order of hierarchy as to what operations are done first. The following shows a table of operators in order of priority:

| postfix | expr++ expr-- |

Moving on to more complex forms of code logic, we now discuss control flow state‐

ments. Control flow statements are blocks of code that break up the flow of execution (the main flow being top to bottom) and provide a means for branching and looping.

The simplest control flow statements are if-then and if-then-else.

The SimpleExample program in Example 2-4 contains the following section of code:

if(i/2 <= 2) {

example.setValue(i);

在文檔中 Learning Android (頁 31-34)