Java Data Types

Source: Internet
Author: User

Java Data Types

1. Create a simple Java application

1 public class Code2 {3 public static void main (String [] args) 4 {5 System. out. println ("I can learn Java") 6} 7}

Note:

1. The public keyword is called the access modifier ). Modifier is used to control the access level of other parts of the program for this code.

2. All content in the Java application must be placed in the class.

3. standard naming rules: Class names start with uppercase letters. If multiple words are used, the first letter of each word should be capitalized. This method is called the camel naming method. (Uppercase and lowercase are very important. Java programs are very sensitive to letters, but none of them are case sensitive. In addition, good naming habits help others read and maintain code.

4. The source code file name must be the same as the public class name and use. Java as the extension.

5. the Java compiler automatically names the bytecode file as XXX. java. (You should not add the. class extension by yourself when naming)

 

2. Each Java application must have a main method. Its declaration is as follows:

1 public class Code22 {3    public static void main(String[]args)4    {5      program statements6    }7 }

Note: void indicates that this method does not return values.

{System.out.println("We can do it well")}

Note :,

1. The above Code uses the System. out object and calls its prinln method.

* If "System. out. println ();" has no parameters, only one blank row is printed.

* "System. out. print ();" at this time, print does not have ln, indicating that after the content in the brackets is promised, the output is followed by the text and does not wrap the text.

2. Click (.) to call the method. The common syntax used by Java is: object. method (parameters), which is equivalent to function call.

Iii. Data Types

Java is a strongly typed language. This means that a type must be declared for each variable. In Java, there are a total of eight basic data types (primitive type), including four integer types, two floating-point types, and one character type char used to represent the character units of Unicode encoding, and 1 is a boolean type used to represent the true value.

Note:

1. in Java, the Integer Range or the number of bytes it occupies is irrelevant to the machine that runs Java to code. This reflects the cross-platform features of Java.

2. Because Java programs must ensure that the same running results can be obtained on all machines, the value ranges of various data types must be fixed.

3. A float value has a suffix F or f (eg.1.47f ). If there is no suffix, the default value is double. Double type can be suffixed with D or d, or not added.

4. All floating point numerical calculations follow the IEEE754 standard. (IEEE 754 standard specifies the exchange, arithmetic format, and method of binary and decimal floating point numbers in the computer programming environment .)

Specifically, the following three special floating point values indicate overflow and error:

* Positive infinity

* Negative infinity

* NaN

For example, the result of dividing a positive integer by 0 is positive infinity. The square root of 0 divided by 0 or negative number is NaN (non-numeric ).

5. the literal values of the char type should be enclosed in single quotes. For example, 'A '.

6. Table: Escape Sequence of special characters (as if in comments, pay attention to the application. For example, if you comment out a path C: \ users, it will be misunderstood by the computer .)

Escape Sequence Name Unicode Value
\ B Return \ U0008
\ T Tabulation \ U0009
\ N Line feed \ U000a
\ R Enter \ U000d
\" Double quotation marks \ U0022
\' Single quotes \ U0027
\\ Backslash

\ U005c

Iv. Variables

In Java, each variable has a type ). All the letters in the variable are meaningful, including uppercase and lowercase letters.

1. Variable Initialization

Such as: int a = 12;

2. Constants

Use the keyword final to indicate constants.

Example: final double CM_YYYZZBY = 253660000f

Note: The keyword final indicates that the variable can be assigned only once. And will not be changed once assigned. Generally, all are uppercase letters.

3. Operators

Addition, subtraction, multiplication, division: + ,-,*,/

Supplement:

1) to calculate the square root, use the sqrt method.

Double x = 4;
Double y = Math. sqrt (x );
System. out. println (y); /// prints 2.0

2) You want to perform a Power Operation

Double y = Math. pow (x, a); // sets the value of y to the power of x.

4. numeric type conversion

Precision is involved in the conversion of value types. The solid arrow indicates that no information is lost after conversion; The dotted arrow indicates that information may be lost after conversion. (Note the differences in bytes)

5. Forced type conversion

For example:

Double x = 8888;

Int y = (int) x; // convert x to int type, and then assign the value to y.

6. Learn to view the API and frequently flip it over.

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.