Java Learning note 01--data types

Source: Internet
Author: User

Java Learning note 01--data types

Java Data type Partitioning

are divided into two main types:

1) Basic data type: similar to normal values.

2) Reference data type: The address of the memory is passed.

Floating-point types are actually representing decimals.

Java Basic data types

Overflow of data

When the data size of an integer exceeds the range that can be represented, and the value range is not checked in the program, the values output by this integer variable will be confused and not expected to run.

Example: Finding the maximum value of an integral type

[Java]View Plaincopy
    1. Public class T {
    2. public static void Main (string[] args) {
    3. int max = Integer.max_value;
    4. System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
    5. }
    6. }


Now the maximum value to be asked for the addition operation

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. int max = Integer.max_value;
  4. System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
  5. System.out.println ("Maximum value of integer + 1:" + (max+1)); maximum value of//integer + 1: -2147483648
  6. System.out.println ("Maximum value of Integer + 2:" + (max+2)); maximum value of//integer + 2: -2147483647
  7. }
  8. }


If you want to avoid data overflow now, you can expand the data type in a way. Int-->long

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. int max = Integer.max_value;
  4. System.out.println ("The maximum value of the integer type is:" +max); the maximum value for the//integral type is: 2147483647
  5. System.out.println ("Maximum value of integer + 1:" + (max+1)); maximum value of//integer + 1: -2147483648
  6. System.out.println ("Maximum value of Integer + 2:" + (max+2)); maximum value of//integer + 2: -2147483647
  7. SYSTEM.OUT.PRINTLN (maximum value of integer + 2: "+ (long) max+2)); //2147483649
  8. }
  9. }


Character type

The character type occupies 2 bytes in memory and can be used to hold characters such as English letters. When a computer handles character types, it treats these characters as different integers.

Therefore, strictly speaking, the character type is also considered as one of the integer types.

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. char ch1 = ' a '; the//character is data enclosed in '
  4. char CH2 = 97; //define character variables by numbers
  5. System.out.println ("ch1 =" +ch1);
  6. System.out.println ("CH2 =" +ch2);
  7. }
  8. }


Commonly used escape characters

Floating-point type and double-precision floating-point number type

In daily life, the number of decimal types is often used, such as height, weight and so on, the whole number can not meet the requirements of the program designer. In mathematics, these values with a decimal point

Called a real number, in Java, this data type is called a floating-point type (FLOAG) with a length of 32 bytes and a valid range of -3.4E1038 to 3.4E1038. When the range of floating-point numbers is not large enough

There's also a double-precision (double) floating-point number to use. Double-precision floating-point number type is 64 bytes long and valid from -1.7E10308 to 1.7E10308

In Java, a number or a decimal actually has a default type:

The default type for decimals (1.1,1.2) is double type

The default type of integer (All-in-one) is type int

Boolean type

Boolean (Boolean) type of variable, only true (true) and False (false) two

Default values for basic data types

Java Learning note 01--data types

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.