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.
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 } }
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);*/ //这就是多行注释用法 } }
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); }*/ //这就是文档注释用法 }
Multi-line comments cannot be nested in java, that is, /* */ cannot be nested. This code itself may also contain a /*Delimiter.
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
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)
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
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
在同一对花括号内,不能定义同名变量。
第四条实例:
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!