Java Learning notes----data types, variables, constants

Source: Internet
Author: User

First, the data type

1, the basic type (8, also known as the built-in data type). 6 numeric types (byte,short,int,long,float,double), a character type (char), a Boolean type (Boolean).

  

Byte

    • The byte data type is a 8-bit, signed, integer represented by a binary complement; 1 bytes;
    • 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, and 2 bytes;
    • 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, and a 4 byte;
    • 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, and 8 bytes;
    • 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 and must be followed by "L"
    • Example: Long a = 100000l,long B = -200000l.

Float

    • The float data type is a single-precision, 32-bit, IEEE 754-compliant floating-point number, accounting for 4 bytes;
    • Float saves memory space when storing large floating-point groups;
    • The default value is 0.0f; you must add "F" or "F" after the number
    • 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, accounting for 8 bytes;
    • 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, which occupies 2 bytes; The first 256 represent special characters;
    • 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 '.

Note: Shaping the default value: int

Floating-point default value: Double

Instance:

Note: For the value ranges of the base types of numeric types, we do not need to force memory, because their values are already defined in the corresponding wrapper class in the form of constants.

 Public classPrimitivetypetest { Public Static voidMain (string[] args) {//byteSystem.out.println ("base type: Byte bits number:" +byte.size); System.out.println ("Packing class: Java.lang.Byte"); System.out.println ("min: byte.min_value=" +byte.min_value); System.out.println ("Maximum value: byte.max_value=" +byte.max_value);            System.out.println (); // ShortSystem.out.println ("Basic type: Short bits number:" +short.size); System.out.println ("Packing class: Java.lang.Short"); System.out.println ("min: short.min_value=" +short.min_value); System.out.println ("Maximum value: short.max_value=" +short.max_value);            System.out.println (); //intSystem.out.println ("base type: int bits number:" +integer.size); System.out.println ("Packing class: Java.lang.Integer"); System.out.println ("min: integer.min_value=" +integer.min_value); System.out.println ("Maximum value: integer.max_value=" +integer.max_value);            System.out.println (); //LongSystem.out.println ("base type: Long bits number:" +long.size); System.out.println ("Packing class: Java.lang.Long"); System.out.println ("min: long.min_value=" +long.min_value); System.out.println ("Maximum value: long.max_value=" +long.max_value);            System.out.println (); //floatSystem.out.println ("Basic type: float bits number:" +float.size); System.out.println ("Packing class: Java.lang.Float"); System.out.println ("min: float.min_value=" +float.min_value); System.out.println ("Maximum value: float.max_value=" +float.max_value);            System.out.println (); //DoubleSystem.out.println ("Basic type: Double bits number:" +double.size); System.out.println ("Packing class: Java.lang.Double"); System.out.println ("min: double.min_value=" +double.min_value); System.out.println ("Maximum value: double.max_value=" +double.max_value);            System.out.println (); //CharSystem.out.println ("base type: char bits number:" +character.size); System.out.println ("Packing class: Java.lang.Character"); //output Character.min_value to the console in numerical form rather than as a characterSystem.out.println ("min: character.min_value=" + (int) character.min_value); //output Character.max_value to the console in numerical form rather than as a characterSYSTEM.OUT.PRINTLN ("Maximum value: character.max_value=" + (int) character.max_value); }  }

Example results:

Basic type:byteNumber of binary digits: 8Packing class: java.lang.Byte min: Byte.min_value=-128Maximum value: Byte.max_value=127Basic Type: ShortNumber of binary digits: 16Packing class: java.lang.Short min: Short.min_value=-32768Maximum value: Short.max_value=32767Basic Type:intNumber of binary digits: 32Packing class: Java.lang.Integer min: Integer.min_value=-2147483648Maximum value: Integer.max_value=2147483647Basic Type:LongNumber of binary digits: 64Packing class: Java.lang.Long min: Long.min_value=-9223372036854775808Maximum value: Long.max_value=9223372036854775807Basic Type:floatNumber of binary digits: 32Packing class: java.lang.Float min: Float.min_value=1.4e-45Maximum value: Float.max_value=3.4028235e38Basic Type:DoubleNumber of binary digits: 64Packing class: java.lang.Double min: Double.min_value=4.9e-324Maximum value: Double.max_value=1.7976931348623157e308Basic Type:CharNumber of binary digits: 16Packing class: java.lang.Character min: Character.min_value=0Maximum value: Character.max_value=65535

Note: "E+ (-) Number" means that the number before E is multiplied by 10 for the "number" of the second square

II. Reference data types

A reference type points to an object, and a variable that points to an object is a reference variable. These variables are specified as a specific type at the time of Declaration, point to an address value, and once declared, the type cannot be changed.

    • objects, arrays, and interfaces are reference data types.
    • The default value for all reference types is null.
    • A reference variable can be used to refer to any type that is compatible with it.
    • Example: Site site = new site ("Runoob").

Third, variable

Data type variable name = initialization value

Eg;int a = 0;

Iv. Constants

Final data type constant name = initialization value

Five, Four representations of the Java language integer constants:

A binary integer starting with 0b or 0 B, such as: int a = 0b110;

A decimal integer, such as: int a = 17;

An octal integer that requires starting with 0, such as int a = 012;

A hexadecimal integer that requires a 0X or 0x start, such as int a = 0x12;

Vi. Conversion of data types

Display conversions: Types with small capacity are automatically converted to large-capacity types without the need for strong-turn symbols

Byte,short,char--->int--->long--->float--->double

Eg: Large capacity = small capacity

   

 int a = ' C ';

Privacy conversion (CAST): large-capacity cast to small capacity, need to use mandatory symbols, cast loss of precision

Eg: small volume = (forced conversion) Large capacity

    

int a = (int) 35.3;

Java Learning notes----data types, variables, constants

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.