How to use annotations, data types, constants and variables in Java
1. Comments
1. Introduction
A comment is a text that explains the program and improves the code The readability helps us find errors without affecting the running of the program. We don’t have to worry about too much executable code.
2. Type
1. Single-line comment
Single-line comment is the most commonly used, format: //Comment content
.
Usage is as follows:
package day1; public class Demo01 { public static void main(String[] args) { System.out.println("hello word"); //输出hello word System.out.println(3-1); //输出结果为2 } }
2. Multi-line comments
When there are too many comments, you can use multi-line comments. The format is: /content of the comment/
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);*/ //这就是多行注释用法 } }
3. Document comments
Automatically generate document comments, format: /*Comment content/
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); }*/ //这就是文档注释用法 }
3. Notes
Multi-line comments cannot be nested in java, that is, /* */ cannot be nested. This code itself may also contain a /*Delimiter.
2. Data types
1. Introduction
Java is a strongly typed language, which means that a type must be declared for each variable.
Java has eight basic types: four integer types, two floating point types, one char type, and one boolean type
2. Integer type
Integer type is used To represent a value without decimals, negative numbers are allowed
Java provides four integer types: int, short, long, and byte.
# This is the most commonly used. If a value is relatively large, use Long and BYTE for specific application occasions, such as file processing at the bottom layer Or arrays where storage space is at a premium. There are no unsigned (unsigned) forms of int, short, long, or byte types in Java. 3. Floating point typeFloating point type represents a value with a decimal point. There are two floating point types in Java.Type Storage requirements Value range
int 4 bytes -2147483648 ~ 2147483647
short 2 bytes -32768 ~ 32768
long 8 bytes -9223372036854775808 ~ 9223372036854775808
# Byte 1 byte-128 ~ 127
Type Storage requirements Value rangefloat 4 bytes About -3.40282347E 38F (valid digits are 6~7)
double 8 bytes About -1.79 769313486231570E 308( The number of significant digits is 15)
- The numerical precision of the double type is twice that of the float type, and most commonly use the double type.
- The float type is used in specific situations, such as single-precision libraries or when storing large amounts of data.
- The float type value is followed by a suffix F or f, for example: 3.14F/f. If there is no F/f suffix, the system will default to double type.
System.out.println(2.0-1.1); //系统将打印出0.8999999999999999而不是0.9。
package com; public class Demo05 { public static void main(String[] args) { //System.out.println();//输出语句,能够在控制台上输出内容 //在控制台上输出整数常量 System.out.println(1); System.out.println(12); System.out.println(-12); //在控制台上输出小数常量 System.out.println(3.14); System.out.println(12.5); System.out.println(1.0); //在控制台上输出字符常量 System.out.println('a'); System.out.println('在'); System.out.println('$'); //字符常量单引号之内有且仅有一个字符,不能是空字符,以下为例 //System.out.println('');//错误的 //System.out.println('abc');//错误的 //在控制台上输出字符串常量 System.out.println("a"); //字符串常量必须用双引号括起来,里面可以是一个、一串、空数据。 System.out.println("abc"); System.out.println("123"); System.out.println(""); //在控制台上输出布尔常量 System.out.println(true); //只有两个值。 System.out.println(false); //在控制台上输出空常量 //System.out.println(null);//空常量不能放在输出语句的里面 } }
package decom1; public class changliang { public static void main(String[] args) { final double a=2.50; //final一旦被定义变量,该变量的值就不能改变。 double b=2.0; double c=3.0; System.out.println("输出结果:"+a*b+"与"+a*c); } }
Output results: 5.0 and 7.52. Variables
Variables that change over a period of time are called variables. Variable names must start with a letter and consist of a sequence of letters or numbers, and are case-sensitive. You cannot use keywords in Java as variables. After declaring a variable, the declared variable must be initialized. Variable format: Direct variable:
Data type variable name = data; (direct definition) int i = 0;Indirect variables:
Data type variable name; variable name = data; (indirect definition) int i; i=1;Notespackage decom1; public class bianliang { public static void main(String[] args) { //定义一个byte变量 byte a =12; System.out.println(a); //定义一个short变量 short b; b=13; System.out.println(b); //定义一个int变量 int c=14; System.out.println(c); //定义一个long变量 long d=2; System.out.println(d); //定义一个float变量 float e=12.04F; System.out.println(e); //定义一个double变量 double f=1.0; System.out.println(f); //定义一个char变量 char g = 'A'; System.out.println(g); //定义一个boolean类型的变量 boolean h = true; System.out.println(h); boolean i = false; System.out.println(i); //定义2个int类型变量a,b //int a = 12, b = 13; /*int a, b; a = 12; b = 13;*/ //定义一个int类型的变量,初始值为12 int k = 12; System.out.println(a);//12 //将变量a的值修改为13 k = 13; System.out.println(a);//13 } }Copy after login
- When we assign a value to a float type variable, we must add F or f after the data.
- When we assign a value to a long type variable, it is recommended to add L or l after it.
- When we want to use a variable, we must first assign a value and then use it, otherwise an error will be reported.
- When we want to assign a value to a variable, we must consider the scope of the variable, otherwise an error will be reported.
在同一对花括号内,不能定义同名变量。
第四条实例:
package decom1; public class cuowu { public static void main(String[] args) { byte i = (byte)130; System.out.println(i); } }
输出结果为:-126
The above is the detailed content of How to use annotations, data types, constants and variables in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
