Java Study Notes 2 -- data type, array, Study Notes 2 --

Source: Internet
Author: User

Java Study Notes 2 -- data type, array, Study Notes 2 --

URL: http://www.cnblogs.com/archimedes/p/java-study-note2.html.

1. Data Type

Java data types include:

Primitive Data Types)

Reference type)

Java primitive data types are also known as basic data types. They indicate the types of basic data that can be expressed in programs. There are usually eight types. The reference types include classes and interfaces. The specific content will be described in the following article.

Java primitive (basic) Data Type:

Boolean type, only true and false)

Byte 8-digit integer, value range:-128 ~ + 127

Short 16-digit integer, value range:-32768 ~ + 32767

Char 16 characters, Unicode Character Set, character code value range: 0 ('\ u0000 ')~ 65535 ('\ uffff ').

Int 32-bit integer, value range:-2147483648 ~ + 2147483647

Long 64-bit integer, value range:-9223372036854775808 ~ + 9223372036854775807

Float 32-Bit Single-precision floating point number, using the IEEE754 standard.

Double 64-bit double-precision floating point number, using the IEEE754 standard.

Default Value of the original Java Data Type Variable

Number of digits occupied by the Java raw data type

2.% (Modulo) Operator

Op1 % op2

Calculate the remainder of op1 divided by OP2.

Note:

During the modulo operation, op1 and op2 must be integers.

If op1 is negative, op2 is positive, and the result is negative.

If op1 is positive, op2 is negative, and the result is positive.

If op1 is positive, op2 is positive, and the result is positive.

If op1 is negative, op2 is negative, and the result is negative.

Run a program:

public class test{    public static void main(String args[]) {        System.out.println("4 % 3 = " + 4 % 3);        System.out.println("-4 % 3 = " + (-4) % 3);        System.out.println("-4 % -3 = " + (-4) % (-3));        System.out.println("4 % -3 = " + 4 % (-3));    }}

The running result is as follows:

4% 3 = 1
-4% 3 =-1
-4%-3 =-1
4%-3 = 1

3. for statements in strong format:

For the iteration of Arrays and Collections, there is a more compact and easy-to-read for statement form. Syntax format:

For (element type variable: array type or set type variable ){

Statement;

}

Run a program:

public class Main{    public static void main(String args[]) {        int sum = 0;        int[] numbers = {1,2,3,4,5,6,7,8,9,10};        for (int item : numbers) {            sum += item;        }        System.out.println("Sum is: " + sum);    }}
4. One-dimensional array

Declaration syntax format of one-dimensional arrays:

Array name;

Or

Type [] Name;

Example: int [] anArray; String [] agrs;

Create a one-dimensional array

The declared array variables are referenced variables. Therefore, they can be used only after they are declared. To create an array, use the new operator. There are two ways to create an array:

(1) Create an array with the new operator after it is declared. Syntax format:

Array variable = new type [length];

(2) create an array declaration using the new operator. Syntax format:

Type [] array variable = new type [length];

int[] aArray;aArray = new int[100];float[] fArray;fArray = new float[35];double[] dData = new double[23];String[] sTitle = new String[15];

Run a program:

public class test{    public static void main(String args[]) {        double[] dRandom;        int i = 0;        dRandom = new double[5];        while( i < 5) {            dRandom[i] = 9.0 * Math.random();            System.out.println("dRandom["+ i +"]=" + dRandom[i]);            i++;        }    }}

The running result is as follows:

DRandom: [0] = 7.513923470155991
DRandom: [1] = 3.3304886355341017
DRandom: [2] = 2.2258372149369214
DRandom [3] = 4.450285565380552
DRandom: [4] = 3.0354964312542174

The array replication method uses the arraycopy () method provided by the System class. Its syntax is as follows:

System. arraycopy (Object src, int srcPos, Object dest, int destPos, int length );

System. arraycopy (source array, starting position in the source array, target array, starting position in the target data, number of array elements to be copied );

Run a program:

public class test {    public static void main(String args[]){        int arr1[] = {1,2,3,4,5};        int arr2[] = new int[5];        System.arraycopy(arr1, 0, arr2, 0, 5);        for (int i = 0; i < arr2.length; i++) {            System.out.println(arr2[i]);        }    }}
5. Arrays class

Basic operations on arrays, such as sorting, searching, and comparison, are common. In java, Array is a class that can help you perform these operations. Array is a class located in the java. util package. It provides several methods that can be used directly.

Sort ()To help you sort the specified array, the quick sort method is used.

BinarySearch ()Allows you to perform binary search on sorted arrays. If a specified value is found, the index of the value is returned. Otherwise, a negative value is returned.

Fill ()After you configure an array, the default value is given based on the data type. For example, if the initial value of an integer array is 0, you can use the Arrays. fill () method to set all elements to the specified value.

Equals ()Checks whether all element values in the two arrays are equal. If yes, true is returned. Otherwise, false is returned.

Import java. util. imports; import java. util. arrays; public class test {public static void main (String [] args) {role = new role (System. in); int [] arr = {93, 5, 3, 55, 57, 7, 2, 73, 41, 91}; System. out. print ("Before sorting:"); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I] + "");} System. out. println (); Arrays. sort (arr); System. out. print ("sorted:"); for (int I = 0; I <arr. length; I ++) {System. out. print (arr [I] + "");} System. out. print ("\ n enter the search value:"); int key = bytes. nextInt (); int find =-1; if (find = Arrays. binarySearch (arr, key)>-1) {System. out. println ("locate value in index" + find + "location");} else System. out. println ("the specified value cannot be found ");}}

The execution result is as follows:

Before sorting: 93 5 3 55 57 7 2 73 41 91
After sorting: 2 3 5 7 41 55 57 73 91 93
Enter search value: 5
Locate the value at index 2

The following uses Arrays to fill and compare Arrays:

import java.util.Arrays;public class test {    public static void main(String[] args) {        int[] arr1 = new int[10];        int[] arr2 = new int[10];        int[] arr3 = new int[10];        Arrays.fill(arr1, 5);        Arrays.fill(arr2, 5);        Arrays.fill(arr3, 10);        System.out.print("arr1: ");        for (int i = 0; i < arr1.length; i++){            System.out.print(arr1[i] + " ");            }        System.out.println("\narr1 = arr2 ? " + Arrays.equals(arr1, arr2));        System.out.println("arr1 = arr3 ? " + Arrays.equals(arr1, arr3));    }}

The execution result is as follows:

Arr1: 5 5 5 5 5 5 5 5 5 5 5 5
Arr1 = arr2? True
Arr1 = arr3? False

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.