Java notes: Data types, variables, and arrays

Source: Internet
Author: User

First, the basic type

The basic types of Java can be divided into four groups:

① integer: Byte, short, int, long (width in order 8, 16, 32, 64, unsigned type not supported)

② floating-point type: float, double (32, 64 width, unsigned type not supported)

③ character Type: char (width 16, support Unicode, Ascⅱ representation)

④ Boolean Type: Boolean

Integer literals:

classSolution { Public Static voidMain (string[] args) {intbin = 0b11;//Binary starts with OB        intOct = 011;//octal begins with 0        intHex = 0x11;//hex starts with 0xSystem.out.println (BIN);//3SYSTEM.OUT.PRINTLN (Oct);//9System.out.println (hex);// -    }}

Floating-point literals:

class Solution {    publicstaticvoid  main (string[] args) {        double a = 1e-5; // Representation of scientific notation        double B = 1_000.5; // underline only enhances readability and does not actually work        System.out.println (a); // 1.0E-5        System.out.println (b); // 1000.5     }}

Second, type conversion

① Automatic type conversion occurs when two types are compatible and the destination type is larger than the source type.

② when two types are incompatible or the target type is less than the source type, an explicit cast is required. Accuracy may be lost.

③ the operand type is automatically promoted when an expression evaluates to an intermediate value that requires more precision than the operand.

Three, array

Arrays in Java are treated as reference types rather than as primitive types.

classSolution { Public Static voidMain (string[] args) {int[] A = {0, 1, 2};//creating a one-dimensional array statically        int[] B =New int[3];//dynamically creating one-dimensional arrays         for(inti = 0; I < 3; i++) B[i]=i; int[] C = {{0, 1, 2}, {0, 1, 2}, {0, 1, 2}};//creating a multidimensional array statically        int[] D =New int[3] [3];//creating multidimensional arrays dynamically         for(inti = 0; I < 3; i++)             for(intj = 0; J < 3; J + +) D[i][j]=J; int[] e = {{0}, {0, 1}, {0, 1, 2}};//static creation of multidimensional unequal arrays        int[] f =New int[3] [];//dynamic creation of multidimensional unequal-length arrays         for(inti = 1; I <= 3; i++) {F[i]=New int[i];  for(intj = 0; J < I; J + +) F[i][j]=J; }    }}

Iv. pointers

Pointers are not allowed in Java (pointers are present but not directly accessible and modified by the user), and supporting pointers can cause Java programs to break through the Java execution environment. Java does not support pointers is not a flaw, as long as in the Java execution environment will never need to use pointers, in other words, the use of pointers to Java programs do not bring any benefit.

Java notes: Data types, variables, and arrays

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: 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.