Java basics: Data Types

Source: Internet
Author: User

1. Category


Basic Data Type

4 categories and 8 types -->

Logical Boolean text char integer byte short int long floating point float double

 

Reference data type

Class 3 -->

Class interface Array

 

2. Basic Data Types


<1> data promotion: Before calculation, convert the data type to a higher level before calculation. The rules are as follows:

Byte short char ---> int ---> long ---> float ---> double
1 2 2 4 8 4 8 --> octets

Byte short char is not upgraded to each other, and Boolean is not converted to another data type.

Byte range:-128 ~ 127

Byte short char is automatically converted to int when involved in the operation.


For example, byte b1 = 1;
Byte b2 = 2;
Byte B3 = b1 + B2; Error
// Byte B3 = (byte) B1 + B2; think of short char as well


<2> data Declaration

The integer type is int by default. Therefore, l or l must be added after the long type.

Long A = 20l; long a = 20l;

For example, long l = 2000000000; // if l or L is not added, an error is returned (an excessively large integer)

 

Because the compiler considers the integer number as int, but the int value can be no less than 2000000000 characters in four bytes, an error is returned!

Floating Point Numbers are double by default. Therefore, f or F must be added after the float type.

Float F = 2.3f; float F = 2.3f;

Suggestion: when declaring the long or float data type, write f or L. Do not use lower-case f or L.


Guess how the following program prints the result?

Public class onlongdemo {// Number of microseconds per year Private Static final long micros_per_year = 365*24*60*60*1000*1000; // Number of milliseconds per year Private Static final long millis_per_year = 365*24*60*60*1000; public static void main (string [] ARGs) {system. out. println (micros_per_year/millis_per_year );}}


Modify the code.

Public class onlongdemo {// Number of microseconds per year Private Static final long micros_per_year = 365l * 24*60*60*1000*1000; // Number of milliseconds per year Private Static final long millis_per_year = 365l * 24*60*60*1000; public static void main (string [] ARGs) {system. out. println (micros_per_year/millis_per_year );}}


Let's look at another small program to see why I need to write an upper-case letter l instead of a lower-case letter L.

public class OnLongDemo {public static void main(String[] args) {long x = 1234;long y = 432l;System.out.println("x+y= " + (x+y));}}

Many people may see that the printed result is x + y = 5555, but you are wrong !!




Long y = 432l followed by a lowercase letter l instead of a number 1









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.