Java know how much (10) data types and variables

Source: Internet
Author: User

Java is a "strongly typed" language, and you must specify the data type when declaring a variable. Variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes.

Java has 8 basic data types, including 4 integer, 2 floating-point, 1-character, 1-Boolean, see the table below.

Java basic data Types
Data Type Description occupied memory Example Notes
Byte BYTE type 1 byte 3, 127
Short Short-integer 2 bytes 3, 32767
Int Integral type 4 bytes 3, 21474836
Long Long integer type 8 bytes 3L, 92233720368L Long finally has an L letter (case-insensitive).
Float Single-precision floating-point type 4 bytes 1.2F, 223.56F Float finally has an F letter (capitalization doesn't matter).
Double Double-precision floating-point type 8 bytes 1.2, 1.2D, 223.56, 223.56D Double finally preferably have a D letter (case does not matter).
Char Character type 2 bytes ' A ', ' a ' Character data can only be one character, surrounded by single quotes.
Boolean Boolean type 1 bit True, False


For integer data, the type of int is typically used. But if the energy emitted by the atom bomb that is served on Hiroshima Nagasaki is used, a long type is required. The byte and short types are primarily used for specific applications, such as the underlying file processing or the need to control large arrays that occupy the amount of storage space.

In Java, the length of the integer data is platform-independent, which solves many of the problems that the programmer brings to programmers when porting software from one platform to another. In contrast, the length of a/C + + Integer data is platform-dependent, and programmers need to choose the right integer for different platforms, which could result in an integer overflow on a 32-bit system for programs that run stably on a 64-bit system.

Octal has a prefix of 0, for example 010 corresponds to 8 in decimal, hexadecimal has a prefix of 0x, such as 0xCAFE; starting with Java 7, you can use prefix 0b to represent binary data, such as the 0b1001 corresponding to 9 in decimal. Also starting with Java 7, you can use underscores to separate numbers, like the English numeral notation, such as 1_000_000 1,000,000, or 1 million. The underscore is just to make the code easier to read, and the compiler will remove these underscores.

Also, unlike C/c++,java, unsigned types (unsigned) are not supported.

The valid digits of the float type are up to 7 bits long, and the valid number lengths include integer and fractional parts. For example:

1 float x = 223.56F; 2 float y = 100.00f;

Note: There is a flag "F" or "F" behind each float type, and this flag represents the float type.

A double type is a valid number with a maximum of 15 bits. As with the float type, a double is followed by a flag "D" or "D". For example:

1 double x = 23.45D; 2 double y = 422.22d; 3 double z = 562.234;

Note: floating-point data without any flags, the system defaults to the double type.

In most cases, a double type is used, and the precision of float is difficult to meet demand.

Examples of application of different data types:

 Public classDemo { Public Static voidMain (string[] args) {//Character Type    CharwebName1 = ' Bo '; CharwebName2 = ' Guest '; CharWebName3 = ' Garden '; System.out.println ("The name of the site is:" + webName1 + webName2 +WebName3); //Integral type     Shortx=22;//decimal    inty=022;//octal    Longz=0x22l;//hexadecimalSystem.out.println ("Converted to decimal: x =" + x + ", y =" + y + ", z =" +z); //floating Point Type    floatm = 22.45f; Doublen = 10; System.out.println ("Calculated product:" + M + "*" + n + "=" + m*N); }}

Operation Result:
The name of the website is: Blog Park
Convert to decimal: x = $, y =, z = 34
Calculated product: 22.45 * 10.0=224.50000762939453

As you can see from the running result, even if the floating-point data has only integers with no decimals, the system will automatically add a decimal point to the output of the console, and the scale is all set to 0.

Description of the Boolean type

If you have programming experience and understand the Boolean type, please skip the following tutorial, the following tutorials are for readers with only C language basis (C language does not have a Boolean type).

In the C language, if the condition is determined, it returns 1, otherwise 0 is returned, for example:

1 #include <stdio.h>2int  main () {3  int x = 100>10; /c6>4int y = 100<10; 5 printf ("100>10 =%d\n", x); 6 printf ("100<10 =%d\n", y); 7 return 0; 8 }         

Operation Result:

100>10 = 1
100<10 = 0

In Java, however, the condition returns True, otherwise false, which is the Boolean type. For example:

1  Public classDemo {2      Public Static voidMain (string[] args) {3 //Character Type4 BooleanA = 100>10;5 Booleanb = 100<10;6System.out.println ("100>10 =" +a);7System.out.println ("100<10 =" +b);8       if(a) {9System.out.println ("100<10 is right."));Ten}Else{ OneSystem.out.println ("100<10 is Wrong"); A       } - } -}

Operation Result:

100>10 = True
100<10 = False
100<10 was right.

In fact, true equates to 1,false equivalent to 0, except for a different name, and becomes a data type alone.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (10) data types and variables

Java know how much (10) data types and variables

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.