Home > Java > javaTutorial > How to use annotations, data types, constants and variables in Java

How to use annotations, data types, constants and variables in Java

WBOY
Release: 2023-04-25 15:55:08
forward
895 people have browsed it

    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
    }
    }
    Copy after login
    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);*/   //这就是多行注释用法
    }
    }
    Copy after login
    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);
    }*/                    //这就是文档注释用法
    }
    Copy after login

    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.

    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

    # 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 type

    Floating point type represents a value with a decimal point. There are two floating point types in Java.

    Type Storage requirements Value range

    float 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.

    Warning: Floating point values ​​are not suitable for financial calculations where rounding errors cannot be tolerated

    System.out.println(2.0-1.1);  //系统将打印出0.8999999999999999而不是0.9。
    Copy after login

    4.char type

    char is used to represent characters

    Char literals are represented by single quotes, for example: ‘A’. Note: Do not use double quotes.

    char represents the range: \u0000 to \uFFFF

    5.boolean type

    The 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 Variables

    1. Constants

    A 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);//空常量不能放在输出语句的里面
    	}
    }
    Copy after login

    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);
    	}
    
    }
    Copy after login

    Output results: 5.0 and 7.5

    2. 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;

    package 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
    Notes

    • 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);
    	}
    
    }
    Copy after login

    输出结果为:-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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template