Java Learning Notes (i)

Source: Internet
Author: User

Java basic data Type One, Java's two big data types:
    • Built-in data types
    • Reference data type
1. Built-in data type

The Java language provides eight basic types. Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.

Byte

    • The byte data type is a 8-bit, signed, integer in binary complement notation;
    • The minimum value is -128 ( -2^7);
    • The maximum value is 127 (2^7-1);
    • The default value is 0;
    • The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;
    • Example: Byte a = 100,byte B =-50.

Short

    • The short data type is a 16-bit, signed integer with a binary complement representation
    • The minimum value is -32768 ( -2^15);
    • The maximum value is 32767 (2^15-1);
    • The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;
    • The default value is 0;
    • Example: Short s = 1000,short r =-20000.

Int:

    • The int data type is a 32-bit, signed integer with a binary complement representation;
    • The minimum value is -2,147,483,648 ( -2^31);
    • The maximum value is 2,147,483,647 (2^31-1);
    • The generic integer variable defaults to the int type;
    • The default value is 0;
    • Example: int a = 100000, int b =-200000.

Long

    • The Long data type is a 64-bit, signed integer with a binary complement representation;
    • The minimum value is -9,223,372,036,854,775,808 ( -2^63);
    • The maximum value is 9,223,372,036,854,775,807 (2^63-1);
    • This type is mainly used on systems that need to compare large integers;
    • The default value is 0L;
    • Example: Long a = 100000l,long B = -200000l.
      "L" is theoretically not case-sensitive, but if written "L" is easily confused with the number "1", it is not easy to distinguish. So it's best to capitalize.

Float

    • The float data type is a single-precision, 32-bit, IEEE 754 compliant floating-point number;
    • Float saves memory space when storing large floating-point groups;
    • The default value is 0.0f;
    • Floating-point numbers cannot be used to denote precise values, such as currency;
    • Example: float f1 = 234.5f.

Double

    • The double data type is a dual-precision, 64-bit, IEEE 754-compliant floating-point number;
    • The default type of floating-point number is double type;
    • A double type cannot also represent an exact value, such as a currency;
    • The default value is 0.0d;
    • Example: Double D1 = 123.4.

Boolean

    • The Boolean data type represents one bit of information;
    • Only two values: true and false;
    • This type is only used as a sign to record the true/false situation;
    • The default value is false;
    • Example: Boolean one = True.

Char

    • The char type is a single 16-bit Unicode character;
    • The minimum value is \u0000 (that is, 0);
    • The maximum value is \uffff (that is, 65,535);
    • Char data type can store any character;
    • Example: char letter = ' A ';.
2. Reference type
    • In Java, a variable of a reference type is very similar to a C + + pointer. A reference type points to an object, and a variable that points to an object is a reference variable. These variables are specified at the time of declaration as a specific type, such as Employee, Puppy, and so on. once a variable is declared, the type cannot be changed .
    • objects, arrays are reference data types .
    • the default value for all reference types is null.
    • A reference variable can be used to reference any type that is compatible with it.
    • Example: Site site = new site ("Runoob").

Second, Java constants

Constants cannot be modified while the program is running.

1. Use the final keyword in Java to modify constants in a way that is similar to variables:

Finaldouble=3.1415927;

2, although the constant name can also be lowercase, but in order to facilitate identification, usually use uppercase letters to denote constants.

Constants can be assigned to variables of any built-in type. For example:

byte=;  Char=' A '     

3, Byte, int, long, and short can all be represented in decimal, 16, and 8 binary ways.

When a constant is used, the prefix 0 represents 8, and the prefix 0x represents 16 binary. such as:

intDecimal=+;  int=0144;  int=0x64;           

4. The Java string constant is also a sequence of characters contained within two quotation marks. The following is an example of a string-type constant:

"Hello World" "Two\nlines" "\" This is inquotes\ "" 

5, string constants and character constants can contain any Unicode characters. For example:

char=' \u0001 ';  String="\u0001";      
Iii. type conversion 1, automatic type conversion

Integer, Real (constant), character-type data can be mixed. Operations, different types of data are converted to the same type first, and then the operation is performed.

Conversions from low to advanced.

Low   ------------------------------------>   High byte,short,Char ,int, long,float     

Data type conversions must meet the following rules:

    • 1. A type conversion cannot be made to a Boolean type.

    • 2. You cannot convert an object type to an object of an unrelated class.

    • 3. Forced type conversions must be used when converting large-capacity types to small-capacity types.

    • 4. The conversion process may result in overflow or loss accuracy, for example:

      int I =128; byte b =  ( byte i      
    • Because the byte type is 8 bits and the maximum value is 127, it causes an overflow when cast to the value 128 of the INT type.

    • 5. The conversion of floating-point numbers to integers is obtained by discarding decimals, rather than rounding, for example:

      (int)23.7==;  (int)-45.89f== -           

The automatic type conversion must meet the number of bits of the data type before the conversion, for example, if the number of bits for the short data type is 16 bits, you can automatically convert the int type with the number of bits 32, and the same number of bits for the float data type is 32. A double type that can be automatically converted to a 64-bit.

2. Forced type conversion
    • 1. The condition is that the data type of the conversion must be compatible.

    • 2. Format: (type) value type is an instance of the data type to enforce type conversion:

3. Implicit coercion type conversion
    • 1. The default type for integers is int.

    • 2. This is not the case with floating-point types, because the float type must be followed by F or F after the number.

Iv. Types of Java variables

In the Java language, all variables must be declared before they are used. The basic format for declaring variables is as follows:

type identifier [ = value][, identifier [= value] ] ... ] ;

Format Description: Type is a Java data type. Identifier is the variable name. You can declare multiple variables of the same type by separating them with commas.

The types of variables supported by the Java language are:

    • Class variable: A variable that is independent of the method and is decorated with static.
    • Instance variable: A variable that is independent of the method, but has no static adornment.
    • Local variable: A variable in a method of a class.

1. Java Local Variables

    • A local variable is declared in a method, a construction method, or a block of statements;
    • Local variables are created when methods, construction methods, or block of statements are executed, and when they are executed, the variables are destroyed;
    • Access modifiers cannot be used for local variables;
    • A local variable is only visible in the method, construction method, or block of statements that declares it;
    • Local variables are allocated on the stack.
    • The local variable has no default value, so the local variable is declared and must be initialized before it can be used.

2. Instance variables

    • Instance variables are declared in a class, but outside of methods, construction methods, and statement blocks;
    • When an object is instantiated, the value of each instance variable is then determined;
    • Instance variables are created when the object is created and destroyed when the object is destroyed;
    • The value of the instance variable should be referenced by at least one method, construct method, or statement block, so that the external can obtain instance variable information through these methods;
    • Instance variables can be declared before use or after use;
    • An access modifier can modify an instance variable;
    • Instance variables are visible to methods, construction methods, or block statements in a class. In general, you should set the instance variable to private. By using the access modifier, the instance variable is visible to the child class;
    • The instance variable has a default value. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method;
    • Instance variables can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.

3. Class variables (static variables)

    • Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside the method construction method and statement block.
    • No matter how many objects a class creates, the class has only one copy of the class variable.
    • Static variables are seldom used except when declared as constants. Constants are variables declared as public/private,final and static types. Constants cannot be changed after initialization.
    • Static variables are stored in a static storage area. are often declared as constants, and static declaration variables are seldom used alone.
    • Static variables are created at the beginning of the program and are destroyed at the end of the program.
    • has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types.
    • The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, static variables can be initialized in static statement blocks.
    • Static variables can be accessed by:classname.variablename .
    • Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.

Java Learning Notes (i)

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.