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

Case Analysis of Java variables and data types

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

Share

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

This article introduces the relevant knowledge of "instance Analysis of Java variables and data types". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Variables and basic data types in Java:

After writing the first java program, we also have a preliminary understanding of Java, and now let's move on to the basic knowledge points in Java-variables and data types.

So how do we understand variables and data types? here, let's first take a look at memory. Memory in the computer system in our daily life is mainly divided into internal memory and external memory. For example, when we usually look at our computer configuration, we will see signs such as 8G+256G, in which 8G is the so-called internal memory (RAM), and 256g is external memory, such as disk and USB disk. CD, etc., we will not study it in detail here. The variable we define takes up internal memory, which is essentially a period of actual continuous storage space, and the data type represents the amount of storage space that the data needs to occupy. The function of the data type is to tell the compiler how much memory the variable currently needs. The data types in Java are divided into basic data types and reference data types. There are eight basic data types. Now let's take a look at these eight basic data types.

The integer type public class Test {public static void main (String [] int) {int a = 10 + a represents the integer int b = 5; System.out.println (the value of "an is:" + a) System.out.println (the value of "b is" + b);}}

Note: the integer occupies four bytes in memory, whether it is 32 or 64 bits, which has nothing to do with the operating system. One byte = eight bit bits, so the integer occupies 32 bit bits in memory, one bit bit represents a binary number, the first bit bit represents the symbol bit, and the latter 31 bit bits represent the numeric bit. There are a total of 2 ^ 31 states, and positive numbers have 2 ^ 31-1 possibilities. The range of negative numbers is-2 ^ 31-1, so we can find the range of shaping values:-231-231-1.

Public class TestB {public static void main (String [] args) {int a = 10; System.out.println (Integer.MAX_VALUE); System.out.println (Integer.MIN_VALUE);}}

Here, we can use int's wrapper class Integer to check the value range result.

Note: the maximum value of the ① integer + 1 becomes the minimum value, which is equivalent to a circle in a continuous cycle.

Long integer public class TestC {public static void main (String [] args) {long a = 10L; long b = 5L; System.out.println ("a's value is:" + a); System.out.println ("b's value is:" + b) }}

Note: the long integer occupies eight bytes, 64 bit bits, one symbol bit and 63 numerical bits in Java. By the same calculation method, its value range is-263263-1.

Public class TestD {public static void main (String [] args) {long a = 10L; System.out.println ("maximum:" + Long.MAX_VALUE); / / "+" means System.out.println ("minimum:" + Long.MIN_VALUE);}}

Here we can also use long's wrapper class Long to verify

Short integer public class TestH {public static void main (String [] args) {short a = 2; short b = 4; System.out.println (a); System.out.println (b);}}

The short integer takes up two bytes, and the range of values is-2 ^ 16 mi-2 ^ 15-1.

Similarly, we can also use short's wrapper class Short to verify:

Public class TestI {public static void main (String [] args) {short a = 10; System.out.println ("maximum:" + Short.MAX_VALUE); System.out.println ("minimum:" + Short.MIN_VALUE);}}

Single-precision floating-point public class TestJ {public static void main (String [] args) {float f = 11.5f; / / the decimal defaults to double precision, so add a f float F1 = 6.3f; System.out.println (f); System.out.println (F1);}}

Note: the single-precision floating-point type takes up four bytes, and we can also calculate that its value range is-231-231-1. The decimal defaults to double precision, so you need to add an "f" after the definition, which also reflects the security of java.

Public class TestK {public static void main (String [] args) {float f = 1.6f; System.out.println (Float.MAX_VALUE); System.out.println (Float.MIN_VALUE);}}

We can also use float's wrapper class Float to verify

Double precision floating point public class TestO {public static void main (String [] args) {double d = 11.5; double D1 = 14.73; System.out.println (d); System.out.println (D1);}}

Note: double occupies eight bytes, and you can also use double's wrapper class Double to calculate the range of values. As long as decimals are double-precision by default, it is recommended to use double when writing programs with decimals. When dividing two integers and the result is a decimal, you need to define one of the integers as double type, such as double d = 1.0.

Public class TestP {public static void main (String [] args) {double d = 1.6; System.out.println (Double.MAX_VALUE); System.out.println (Double.MIN_VALUE);}

Character data type: character data type public class TestQ {public static void main (String [] args) {char ch ='A' / / it can be a Chinese character, which also accounts for two bytes, or a number, and the printed symbol will be the corresponding symbol System.out.println (ch) in Unicode code;}}

The character data type takes up two bytes, except that the wrapper class Character of char cannot be used to calculate its value range, because its value corresponds to the symbol in Unicode code.

Byte type public class TestR {public static void main (String [] args) {byte a = 102; byte b = 1; byte c = (byte) (aqb) / / A type less than four bytes will be promoted to integer calculation, so it needs to be cast to byte type System.out.println (c);}}

The byte type accounts for 1 byte, and the range of initial values is-128. It should be noted that types less than 4 bytes will increase the integer type during calculation, so you need to force the conversion to byte type.

Public class TestS {public static void main (String [] args) {byte a = 12; System.out.println (Byte.MAX_VALUE); System.out.println (Byte.MIN_VALUE);}}

You can also use byte's wrapper class Byte to verify

8. Boolean type public class TestT {public static void main (String [] args) {boolean flg = false; System.out.println (flg);}}

The Boolean type does not have a definite size in java, and it has only two values. True / false in true/false,java is only true/false, no 0 is false, and non-0 is true. The wrapper class corresponding to the Boolean type is Boolean

In addition to the above eight basic data types, there is also a reference data type in Java, such as String-like array, abstract class interface enumeration and so on. Let's take a look at String.

String data type:

Public class TestU {public static void main (String [] args) {String str = "hello"; / / string with double quotation marks String str1 = "hello" + "world"; System.out.println (str) System.out.println (str1); / / two strings can be concatenated with "+". Any data type and string can be concatenated as a string}}

After understanding the above data types, we also have a general understanding of variables, in the naming of variables, there are several rules:

①: can only contain numeric underscores

②: numbers cannot begin

③: it is recommended to use small hump, numMax, that is, the first letter of the first word, the rest should be capitalized.

In java, in addition to variables, there is also a kind of constant, which, as the name implies, can only be initialized once, and remember to initialize it when you use it. Constants are divided into two types:

①: literal constant: like 10, "abc"

②: variable modified by final: final int MAXNUM = 10; in this case maxNum is constant and the letters are all capitalized.

This is the end of "instance Analysis of Java variables and data types". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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