Java Learning notes 2--data types, arrays

Source: Internet
Author: User

This article address: http://www.cnblogs.com/archimedes/p/java-study-note2.html, reprint please indicate source address.

1. Data type

The Java data types are:

Raw data type (Primitive Types)

Reference type (reference type)

The Java raw data type, also known as the basic data type, describes the kind of underlying data that can be represented in a program, usually in 8 ways. Reference types include classes, interfaces, and so on, which are described in the following article.

Java Raw (Basic) data type:

Boolean Boolean, only True (true) with false (false)

BYTE 8-bit signed integer, value range -128~+127

Short 16-bit signed integer, value range -32768~+32767

Char 16-bit character, Unicode character set, character code numeric range: 0 (' \u0000 ') ~65535 (' \uffff ').

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

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

Float 32-bit single-precision floating-point number using the IEEE754 standard.

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

Default values for Java raw data type variables

Number of bits occupied by the Java raw data type

2,% (modulo) operator

Op1% OP2

Calculates the remainder of the OP1 being OP2.

Description

The OP1 and op2 must be integers when the modulo operation is evaluated.

If the OP1 is negative, the OP2 is positive and the result is negative.

If the OP1 is positive, the OP2 is negative and the result is positive.

If OP1 is positive, OP2 is positive and the result is positive.

If the OP1 is negative, the OP2 is negative and the result is negative.

Run a program:

 Public class test{    publicstaticvoid  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 results of the operation are as follows:

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

3. Strong format for statement:

For iterations of arrays (Arrays) and Collections (collections), there is a more compact and readable for statement form. Syntax format:

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

Statement

}

Run a program:

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

Declaration syntax format for one-dimensional arrays:

Array type array name;

Or

type [] name;

For example: int[] Anarray; String[] AGRs;

Creating a one-dimensional array

The declared array variable is a reference variable, so an array declaration can only be used after it is created. Create the array to be implemented using the new operator. There are two ways to create an array:

(1) After the array declaration is created with the new operator, the syntax format:

Array variable = new type [length];

(2) When an array declaration is created with the new operator, the syntax format:

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

intnewint[+]; float  newfloat[+]; Double New Double [string[15new ];

Run a program:

 Public classtest{ Public Static voidMain (String args[]) {Double[] drandom; inti = 0; Drandom=New Double[5];  while(I < 5) {Drandom[i]= 9.0 *Math.random (); System.out.println ("drandom[" + i + "]=" +Drandom[i]); I++; }    }}

The results of the operation are as follows:

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

The method of array copying is to use the Arraycopy () method provided by the system class with the following syntax:

System.arraycopy (object src, int srcpos, object dest, int destpos, int length);

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

Run a program:

 Public class Test {    publicstaticvoid  main (String args[]) {        int Arr1[] = {1,2,3,4,5};         int New int [5];         0, ARR2, 0, 5);          for (int i = 0; i < arr2.length; i++) {            System.out.println (arr2[i]);     }}}
5, Arrays Class

Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java to assist you with these operations, array is a class located in the Java.util package, and he provides several methods that can be used directly.

Sort () helps you sort the specified array by using the Quick Sort method

BinarySearch () allows you to search a sorted array for $ two, and returns the index of the value if the specified value is found, otherwise a negative number is returned

Fill () When you configure an array, the default value is given according to the data type. For example, an array of integers has an initial value of 0, and you can use the Arrays.fill () method to set all elements to the specified value

equals () compares the values of the elements in the two array if they are all equal, and returns true if they are, otherwise false

ImportJava.util.Scanner;Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); int[] arr = {93, 5, 3, 55, 57, 7, 2, 73, 41, 91 }; System.out.print ("Before sorting:");  for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ " ");        } System.out.println ();        Arrays.sort (arr); System.out.print ("After sorting:");  for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+ " "); } System.out.print ("\ n Please enter a search value:"); intKey =Scanner.nextint (); intFind =-1; if(Find = Arrays.binarysearch (arr, key)) >-1) {System.out.println ("Find values in Index" + Find + "location"); } ElseSystem.out.println ("The specified value could not be found"); }}

The results of the implementation are 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
Please enter search value: 5
Find value in index 2 position

The following uses arrays to populate and compare arrays:

Importjava.util.Arrays; Public classTest { Public Static voidMain (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(inti = 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 following results are performed:

Arr1:5 5 5 5 5 5 5 5 5 5
arr1 = arr2? True
arr1 = ARR3? False

Java Learning Note 2--data type, array

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.