Java Study Notes (1) Introduction to Java basics and Data Types

Source: Internet
Author: User

 

Java is an interpreted language

Each class in the source file corresponds to a class file.

There can only be one public class, and there are no restrictions on the number of other classes. If one source file has one public class, the file name must be the same as the class name of the public class.

The main method is used to execute java applications. It has a fixed writing method:

Public static void main (String args []) {...}

Java is case sensitive

A java method consists of statements. Each statement ends with a semicolon.

Java annotator has one more class than c ++. There are three types. The third class will be discussed later.

Java identifiers consist of letters, numbers, underscores, and $ dollar characters. It can only start with letters, underscores, and dollar characters

Although go and const are never used, they are also reserved as java keywords.

Java variables must be declared first and then assigned values before they can be used.

Program Execution Process:

1. Load the program to the memory from the hard disk

2. The operating system will find the main method of the Code to start execution.

Memory Management During execution:

Divided into four parts: code segment, data segment, stack, heap

Store Code in Code segment

Data segment stores static variables and string constants.

Store local variables in Stack

Heap: Heap is used to dynamically allocate memory and store new items.

Java Variable Classification:

Divided by declared location:

1. Local variables: the variables defined inside the method or statement block (including the parameters of the method)

2. member variables: External method. Variable defined inside the class

Note: variables cannot be declared outside the class.

By data type:

1. Basic data type variables

2. Reference Data Type Variables

 

Java defines four types of 8 Basic Data Types

Logical type: boolean

Text Type: char

Integer: byte, short, int, long

Floating Point: float, double

Values of the Boolean type cannot be 0 or non-0 integers instead of true or false, which is different from those of the C type.

Java characters are encoded in Unicoke format. Each character occupies two bytes and can be expressed in hexadecimal format.

Each integer type in Java has a fixed table number range and field length, which is not affected by the specific operating system, to ensure the portability of Java

Java's Integer constants also have three forms of representation: decimal, octal (starting with 0), and hexadecimal (starting with 0X)

An integer constant of the Java language is of the int type by default. To declare a long constant, add 'L' or 'l'

For example, if long L1 = 8888888888 is compiled, an error must be followed by L, because the system regards 88888888888 as the int type, and the int type cannot put down such a large number.

Data Type Byte Count table count range

Bytes 1-128 ~ 127

Short 2-2 ^ 15 ~ 2 ^ 15-1

Int 4-2 ^ 31 ~ 2 ^ 31-1

Long 8-2 ^ 63 ~ 2 ^ 63-1

All Java numbers are signed.

The number range of floating point tables in Java is the same as the field length, and is not affected by the platform.

The float type is double by default. If the float type needs to be declared, 'f' or 'F' must be followed'

Data Type Precision occupies the number of byte tables

Float, 7 digits after decimal point, 4-3.403E38 ~ 3.403E38

Double decimal point 15 digits 8-1.798E308 ~ 1.798E308

Similarly, float F = 12.3 must be followed by F

The Escape Character '\ U' indicates Unicode encoding followed.

 

Basic data type conversion:

The Boolean type cannot be converted to another type.

Integer, complex, and floating point data are converted to each other during computation. The conversion rules are as follows:

The data type with a small capacity is automatically converted to the data type with a large capacity. The size of the capacity (indicating the size of the number rather than the number of bytes in the memory) is sorted as follows:

Byte, short, char → int, long → float → double

Byte, short, and char do not convert each other. They are transferred to the int type before calculation.

To convert a type with a large capacity to a type with a small capacity, the conversion operator should be enhanced, but it may cause lower precision or overflow.

When multiple data types are used together, the system first automatically converts all data to the data type with the largest capacity, and then calculates

The default value of a real number constant is double.

The default value of an integer constant is int type.

The number of int types can be directly assigned to the byte and short types, but cannot exceed the number range of their tables.

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.