Showing posts with label Variable. Show all posts
Showing posts with label Variable. Show all posts

How to Declare and Set a Constant Variable

A constant variable is a variable that will not change during the execution of the code.
Java does not directly support constants. However, a static final variable is effectively a constant.

1.Add the following line to add a private constant integer variable in the class level. Use final modifier which combined with static modifier. The final modifier indicates that the value of this field cannot change. The static modifier causes the variable to be available without loading an instance of the class where it is defined. The final modifier causes the variable to be unchangeable. Although these aren't syntactical rules, they are widely used as accepted conventions. Java constants are normally declared in ALL CAPS. Underscores normally separate Words in Java constants.

private static final int DATE_PICKER_DIALOG = 0;

2. Compile and run!

Resources:
http://kodejava.org/how-do-i-define-a-constant-variable/
http://www.tech-faq.com/how-to-declare-a-constant-in-java.html




How to Create a Class-Level Variable

1. Create an Android project, if you don't already have one.

2. In the MainActivity.java file,  add your new variable between the public class and the onCreate method. Below the String variable myString is declared class-level.

public class MainActivity extends Activity {

    String myString;  

    @Override
    public void onCreate(Bundle savedInstanceState) {
    ....

3. Compile and run!

Resources:

How to Declare a Boolean Variable

This will declare a Boolean Variable

boolean was added in API Level 1
The wrapper for the primitive type boolean. Boolean represents a true or false state.

1. Create an Android project, if you don't already have one.

2.In the MainActivity.java file, add the following code to the onCreate method. This creates a boolean variable named myBoolean

    boolean myBoolean;

3. Compile and run!

Next Recommended Article: Set a Boolean Variable

Resources:

Create an Integer Variable

How to Create an Integer Variable

This will declare an integer variable

Integer was added in API Level 1

An integer is a number from -2147483648 to 2147483647. The integer data type is a 32-bit signed two's complement integer. Integer is generally used as the default data type for integral values unless there is a concern about memory. The default value is 0.

1. Create an Android project, if you don't already have one.

2.In the MainActivity.java file, add the code below to the onCreate method. This will create an integer variable named myInteger

    int myInteger;

3. Compile and run!

Next Recommended Article: Set an Integer Variable

Related Articles:

How to Declare and Set a Byte Array

byte was added in API Level 1
The byte data type

1.Add the code below to the onCreate method. This will create an empty array of the data type byte named myByteArray, allocating memory for 1024 bytes.

byte[] myByteArray = new byte[1024];

2. Compile and run!

3. Another example, to create and initialize a byte array to the letters 'h e l l o'.

       byte[] myByteArray2 = {'h', 'e', 'l', 'l', 'o'};

Related Articles:

Declare and Set a String Variable

How to Declare and Set a String Variable

String was added in API Level 1

An immutable sequence of characters/code units (chars). A String is represented by array of UTF-16 values, such that Unicode supplementary characters (code points) are stored/encoded as surrogate pairs via Unicode code units (char).

1. Create an Android project, if you don't already have one.

2.In the MainActivity.java file, add the code below to the onCreate method. This creates a string named myString

      String myString = "My string contents are here";

3. Compile and run!

Related Articles:
Resources: