Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of data types and operators in java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the use of data types and operators in java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Data types and operators of java

Variables and types of java

Variable refers to the variable amount that the program runs, which needs to open up memory space to store some data.

The type divides the types of variables, and different types have different attributes.

Basic data type-numerical integer type

Byte

Byte variable name = initial value

The byte type represents a shaping that takes up only 1 byte

Its data range is [- 128127].

Public static void main (String [] args) {System.out.println (Byte.MAX_VALUE); System.out.println (Byte.MIN_VALUE);}

Short

* short variable name = initial value * *

Short indicates that the type of the variable is an integer

The value range of short [- 32768 and 32767]

Public static void main (String [] args) {System.out.println (Short.MIN_VALUE); System.out.println (Short.MAX_VALUE);}

Int

Syntax:

* int variable name = initial value * *

Int indicates that the type of the variable is an integer

In Java, the int type occupies 4 bytes (8 bits per byte)

The data range is-2 ^ 31-> 2 ^ 31-1, that is, from-2.1 billion to + 2.1 billion.

Public static void main (String [] args) {System.out.println (Integer.MAX_VALUE); System.out.println (Integer.MIN_VALUE);}

If the result of the operation exceeds its data range, an overflow will occur.

Public static void main (String [] args) {System.out.println (Integer.MAX_VALUE+1); System.out.println (Integer.MIN_VALUE-1);}

The figure of 2.1 billion is easy to exceed in the current big data era. In this case, we need to use a wider range of data types to represent it, so the long type is provided in Java.

Long

Syntax:

* long variable name = initial value * *

Long occupies 8 bytes and its data range [- 2 ^ 63, 2 ^ 63-1]

Public static void main (String [] args) {long astat10l; System.out.println (a);} public static void main (String [] args) {System.out.println (Long.MAX_VALUE); System.out.println (Long.MIN_VALUE);}

Note:

The basic syntax format is basically the same as creating int variables, except that the type is modified to long initialization value of 10L, representing a long integer number. 10l is fine. Initialization is also possible with 10. The type of 10 is int, and the type of 10L is long. 10L or 10l is better.

Floating point type

Double

Syntax:

Double variable name = initial value

Public static void main (String [] args) {double num = 1.0; System.out.println (num);}

Note:

In Java, the value of int divided by int is still int (the decimal part is discarded directly).

Public static void main (String [] args) {int a = 1; int b = 2; System.out.println (a / b);}

Although the double in Java is also 8 bytes, the memory layout of floating point numbers is very different from that of integers, and the data norm can not be expressed simply in the form of 2 ^ n.

Encirclement.

The memory layout of Java's double type conforms to the IEEE 754 standard (like the C language). Try to use limited memory space to represent potentially infinite decimals.

There is bound to be a certain accuracy error.

Public static void main (String [] args) {double num = 1.1; System.out.println (num * num);}

Float

Syntax:

Float variable name = initial value

Public static void main (String [] args) {float num = 1.0F; System.out.println (num);}

The float type occupies four bytes in Java and also conforms to the IEEE 754 standard. Because the precision range of the data represented is small, floating-point numbers are generally used in engineering.

Double is preferred and float is not recommended.

Character type

Char

Char variable name = initial value

Public static void main (String [] args) {char ch=''; System.out.println (ch);}

Note:

Java uses single quotation marks + a single letter to represent the literal value of the character. A character in a computer is essentially an integer. ASCII is used to represent characters in C language, while Unicode is used to represent characters in Java. Therefore, a character occupies two bytes, representing more types of characters, including Chinese.

Boolean type

Boolean

Boolean variable name = initial value

Public static void main (String [] args) {boolean bool=false; System.out.println (bool);}

Note:

Variables of type boolean have only two values, true for true and false for false.

The boolean type and int of Java cannot be converted to each other. There is no such use as 1 for true and 0 for false.

Some JVM implementations of boolean types occupy 1 byte and some occupy 1 bit, which is not specified.

Operator operator: |

* * if both binary bits are 0, the result is 0, otherwise the result is 1 binary *

Public static void main (String [] args) {int astat1; int bread2; System.out.println (a | b);}

Operator: &

If both binary bits are 1, the result is 1, otherwise the result is 0. 0.

Public static void main (String [] args) {int astat1; int bread2; System.out.println (ahumb);}

Operator: ^

* * if the binary bits of two numbers are the same, the result is 0, and if they are different, the result is 1.

Public static void main (String [] args) {int axi1; int bliss 2; System.out.println (a ^ b);}

Operator: ~

If the bit is 0, it becomes 1, and if the bit is 1, it becomes 0.

Public static void main (String [] args) {int axi1; int bacheloreta; System.out.println (b);}

Operator: > >

Not the rightmost position, the leftmost complement symbol bit (positive complement 0, negative complement 1)

Public static void main (String [] args) {int aq8; System.out.println (a > > 1); / / shift 1} to the right

Operator: > > 1); int baked Murray 1; System.out.println (b > 1);}

This is the end of this article on "what is the use of data types and operators in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report