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。4.char typechar is used to represent charactersChar literals are represented by single quotes, for example: ‘A’. Note: Do not use double quotes. char represents the range: \u0000 to \uFFFF5.boolean typeThe boolean (Boolean) type has two values: false and true, used to determine logical conditions . Integer values cannot be converted to Boolean values. 3. Constants and Variables1. ConstantsA constant is a quantity that cannot be changed. It is a constant and remains unchanged forever. Classification of constants: integer constants, decimal constants, character constants, string constants, Boolean constants, and empty constants.
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);//空常量不能放在输出语句的里面 } }In Java, you can use the final keyword to indicate constants:
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 } }
- 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor