2.Java data types and variable definitions

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 = 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 = 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:

  1. Public class Demo {
  2. public static void main(String[] args){
  3. //character type
  4. char webName1 = ' micro ';
  5. char webName2 = ' learning ';
  6. char WebName3 = ' Garden ';
  7. System. Out. println("website name is:" + webName1 + webName2 + webName3);
  8. //integral type
  9. Short x=$; //Decimal
  10. int y=022; //Eight binary
  11. long Z=0x22l; //16 binary
  12. System. Out. println("converted to decimal: x =" + x + ", y =" + y + ", z =" + z ");
  13. //floating-point type
  14. float m = 45f;
  15. double N = ten;
  16. System. Out. println("Calculated product:" + M + "*" + N + "=" + M*n);
  17. }
  18. }

Operation Result:
The name of the website is: Micro School
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>
  2. int main(){
  3. int x = >;
  4. int y = <;
  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 class Demo {
  2. public static void main(String[] args){
  3. //character type
  4. Boolean a = >;
  5. Boolean b = <;
  6. System. Out. println("100>10 =" + a);
  7. System. Out. println("100<10 =" + b);
  8. if(a){
  9. System. Out. println("100<10 is Right");
  10. }Else{
  11. System. Out. println("100<10 is Wrong");
  12. }
  13. }
  14. }

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.

2.Java data types and variable definitions

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.