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

AI Hentai Generator
Generate AI Hentai for free.

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



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

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

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

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

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
