In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Java comments, data types, constants and variables how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Java comments, data types, constants and variables how to use the article will have a harvest, let's take a look at it.
one。 Note 1. Brief introduction
Comments are a kind of text that interprets the program, improves the readability of the code, and helps us find errors. It doesn't affect the running of the program, and you don't have to worry about getting more executable code.
two。 Type 1. Single-line comment
Single-line comments are the most commonly used, in the format: / / the contents of the comments.
The usage is as follows:
Package day1;public class Demo01 {public static void main (String [] args) {System.out.println ("hello word"); / / output hello word System.out.println (3-1); / / output result is 2}} 2. Multiline comment
When there are too many comments, you can use multiple lines of comments in the format: / contents of comments /
The usage is as follows:
Package day1;public class Demo01 {public static void main (String [] args) {System.out.println ("yuema"); System.out.println (3-1); / * System.out.println ("yuema"); System.out.println (3-1); * / / this is the usage of multiline comments} 3. Document comment
Automatically generate comments for the document. Format: / * contents of comments /
The usage is as follows:
Package day1;public class Demo01 {/ * * public static void main (String [] args) {System.out.println ("yuema"); System.out.println (3-1); System.out.println ("yuema"); System.out.println (3-1);} * / / this is the usage of document comments} 3. Matters needing attention
Multiline comments cannot be nested in java, that is, / * / cannot be nested, and the code itself may contain a / * delimiter.
two。 Data type 1. Brief introduction
Java is a strongly typed language, which means that a type must be declared for each variable.
Java has eight basic types: four integers, two floating-point types, one char type, and one boolean type.
two。 Integer type
An integer is used to indicate a value that has no decimal and is allowed to be negative.
Java provides four integers: int, short, long, and byte.
Value range of type storage requirements
Int 4 bytes-2147483648 ~ 2147483647
Short 2 bytes-32768 ~ 32768
Long 8 bytes-9223372036854775808 ~ 9223372036854775808
Byte 1 byte-128127
Int is most commonly used, using long if a value is large, and short and byte are used for specific applications, such as underlying file processing or arrays with valuable storage space.
There are no unsigned (unsigned) int, short, long, byte types in Java.
3. Floating point type
A floating point represents a value with a decimal point. There are two floating-point types in Java.
Numerical range of type storage requirements
Float 4 bytes approximately +-3.40282347E+38F (significant digits are 6-7 digits)
Double 8 bytes approximately +-1.79769313486231570E+308 (significant digits are 15 digits)
The numerical accuracy of the double type is twice that of the float type, and most commonly used double types.
The float type is used in specific places, such as single-precision libraries or when storing large amounts of data.
Values of type float are followed by a suffix F or f, for example: 3.14F/f. If the suffix f is not followed, the system defaults to the double type.
Warning: floating-point values are not suitable for financial calculations that cannot accept rounding errors
System.out.println (2.0-1.1); / / the system will print 0.899999999999999999 instead of 0.9. 4.char Typ
Char is used to represent characters
The literal amount of char is indicated in single quotation marks, for example:'A'. Note: it is not indicated by double quotation marks.
Char indicates the range:\ u0000 to\ uFFFF
5.boolean Typ
The boolean (Boolean) type has two values: false and true, which are used to determine logical conditions.
Integer values cannot be converted to Boolean values.
three。 Constant and variable 1. Constant
A constant is an unchangeable quantity, which is constant and immutable.
Classification of constants: integer constant, decimal constant, character constant, string constant, Boolean constant, empty constant.
Package com;public class Demo05 {public static void main (String [] args) {/ / System.out.println (); / / output statements that can output content on the console / / output integer constant System.out.println (1); System.out.println (12); System.out.println (- 12) on the console / / output decimal constant System.out.println (3.14); System.out.println (12.5); System.out.println (1.0) on console; / / output character constant System.out.println ('a') on console System.out.println ('in'); System.out.println ('$'); / / there is only one character within the constant single quotation mark and cannot be an empty character. The following is an example of / / System.out.println (''); / / incorrect / / System.out.println ('abc') / / incorrect / / output string constant System.out.println ("a") on the console; / / string constant must be enclosed in double quotes, which can be one, a string, or empty data. System.out.println ("abc"); System.out.println ("123"); System.out.println ("); / / output the Boolean constant System.out.println (true) on the console; / / has only two values. System.out.println (false); / / output empty constants on the console / / System.out.println (null); / / empty constants cannot be put in the output statement}}
In Java, you can use the final keyword to indicate constants:
Package decom1;public class changliang {public static void main (String [] args) {final double aversion 2.50; / final once a variable is defined, the value of the variable cannot be changed. Double bread2.0; double cantilever 3.0; System.out.println ("output results:" + astatb + "and" + astatc);}}
Output: 5.0 and 7.5
two。 Variable
Variables that change over a period of time are called variables.
Variable names must be a sequence that begins with a letter and consists of letters or numbers, and is case-sensitive.
Keywords in Java cannot be used as variables.
After you declare a variable, you must initialize the declared variable.
Variable format:
Direct variables:
Data type variable name = data; (directly defined) int I = 0
Indirect variable:
Data type variable name; variable name = data; (indirect definition) int I; iDefinition1
Package decom1;public class bianliang {public static void main (String [] args) {/ / define a byte variable byte a = 12; System.out.println (a); / / define a short variable short b; bread13; System.out.println (b) / / define an int variable int censor 14; System.out.println (c); / / define a long variable long dong2; System.out.println (d); / / define a float variable float eBay 12.04F System.out.println (e); / / define a double variable double fallow 1.0; System.out.println (f); / / define a char variable char g = 'Aids; System.out.println (g) / / define a variable of type boolean boolean h = true; System.out.println (h); boolean I = false; System.out.println (I); / / define two variables of type int aline b / / int a = 12, b = 13 / * int a, b; a = 12; b = 13; define a variable of type int with an initial value of 12 int k = 12; System.out.println (a); / / 12 / / change the value of variable a to 13 k = 13 System.out.println (a); / / 13}}
Matters needing attention
When we assign a value to a variable of type float, we add F or f after the data.
When we assign a value to a variable of type long, it is recommended to add L or l after it.
When we want to use a variable, we must assign a value before using it, or we will report an error.
When we want to assign a value to a variable, we must consider the scope of the variable, otherwise we will report an error.
A variable with the same name cannot be defined within the same pair of curly braces.
Article 4 examples:
Package decom1;public class cuowu {public static void main (String [] args) {byte I = (byte) 130; System.out.println (I);}}
The output is-126
This is the end of the article on "how to use Java comments, data types, constants and variables". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Java annotations, data types, constants and variables". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.