Basic concepts of variables

  • When you need to record a single data content in the program, you can declare a variable, and the essence of declaring a variable is to apply for a storage unit in memory, because the data content in the storage unit can be changed, so the name “variable”.

  • The size of the required storage unit varies with the size of the stored data. Data types are used to describe the variable in the Java language. To facilitate the next access, you need to specify a name for the variable to record its corresponding storage unit.

I sorted out some information, and friends in need can click to get it directly.

Java Basics

22 Java Architect Core books

Learning routes and materials from 0 to 1Java

1000+ questions from 2021

Declaration and use of variables

Datatype variable name = initial value; Where the initial value of = can be omitted, but; Don’t omit

package com.lagou.Day02; Public class VarTest {public static void main(String[] args) {// 1. Declare a variable and initialize the data type variable name = initial int age = 18; Println ("age -" + age); system.out.println ("age -"); //age -18 } }Copy the code

Considerations for using variables

Java is a strongly typed language, and variables must be declared to indicate their data type before they can be used. Variables must be initialized before being used. Variables cannot be declared twice

package com.lagou.Day02; Public class VarTest {public static void main(String[] args) {// 1. Declare a variable and initialize the data type variable name = initial int age = 18; Println ("age -" + age); system.out.println ("age -"); //age -18 System.out.println("------------------------"); // system.out. println("name -" + name); Error: symbol not found // need to initialize before using variable //String name; //System.out.println("name -" + name); Error: May not initialize String name = "kirby "; System.out.println("name -" + name); //int age = 19; }}Copy the code

Naming rules for identifiers

  • The value consists of digits, letters, underscores (_), and $(.). The value cannot start with a digit
  • You cannot use Java language keywords, which are words that the Java language uses to express specific meanings
  • Case sensitive, unlimited length but not too long.
  • Try to see the name know the meaning, support Chinese but not recommended.
  • Identifiers can give names to classes/variables/properties/methods/packages.

Variable input and output case implementation

package com.lagou.Day02; import java.util.Scanner; Public class VarIOTest {public static void main(String[] args) {//1. Declare two variables to record name and age information String name; int age; System.out.println(" Please enter your name and age :"); //2. Scanner sc = new Scanner(system.in); Scanner sc = new Scanner(system.in); // The scanner reads a string of data into the variable name = sc.next(); Age = sc.nextint (); age = sc.nextint (); System.out.println("name = "+ name); //3. System.out.println("age = " + age); * kobe * 20 * name = kobe * age = 20 */Copy the code

Classification of data types

Data types in the Java language fall into two main categories:

(1) Basic data types (remember)

Byte, short, int, long, float, double, Boolean, char

(2) Reference data types (understand)

Array, class, interface, enumeration, annotation

A common base

In daily life, the use of decimal data description, every ten to one, decimal weight is: 100, 101, 10^2…

At the bottom of the computer, the binary sequence of 0 and 1 is used to describe data. Every two into one, the weight of binary is 20, 21, 2^2… The highest (leftmost) bit in binary is used to represent the sign bit, which is non-negative if it is 0 and negative if it is 1. Octal and hexadecimal are shorthand for binary

One way to convert positive decimal to binary

The method of converting the positive decimal to binary a. Mod by 2, the decimal integer is continuously divided by 2 to get the remainder until the quotient is 0 and the remainder is sorted in reverse order. 0B / 0b can be prefixed as binary 45- binary 101101: is a 64-bit binary.

Two ways to convert positive decimal to binary

Splitting a decimal integer into the sum of several binary weights, with 1 written below and 0 written otherwise

The conversion of positive binary to decimal

Weighting method, which uses each digit in the binary number multiplied by the weight of the current bit and added up.

The conversion of negative decimal to binary

First convert the absolute value of decimal to binary, then negate the bit and add 1. Negative numbers require complement: negate the bit and add 1

The conversion of negative binary to decimal

Subtract 1 and then invert the decimal number. Add a negative sign to the decimal number

The range of integers that a single character can represent

In computers, a single byte represents eight binary bits, where the highest (leftmost) represents the sign bit, 0 represents a non-negative number, and 1 represents a negative number. The range of integers represented is as follows:

The concept of integer types

In Java, the types of integer data are byte, short, int, and long. The int type is recommended.

Programming use of integer types (PART 1)

package com.lagou.Day02; Public class IntTest {public static void main(String[] args) {//1. Declare a byte variable and initialize byte b1 = 25; System.out.println(b1); //25 //byte b2 = 250; Short s1 = 250; short s1 = 250; short s1 = 250; System.out.println(s1); Int i1 = 250250; System.out.println(i1); // over 2.1 billion}}Copy the code

Programming use of integer types (part 2)

Integer data written directly in Java programs is called a literal/literal/constant and is of type int by default. If you want to express a larger direct quantity, add L or L to the end of the direct quantity, l is recommended.

//int i1 = 2502505006; Int i1 = 2502505006L; //int i1 = 2502505006L; // Error: incompatible type: converting from long to int may cause loss // Declare a long type to the variable and initialize long g1 = 2502505006L; System.out.println(g1);Copy the code

If the description calls more data than long, use the java.math.bigintger type

Integer type of written test site

package com.lagou.Day02; Public class IntTest01 {public static void main(String[] args) {int I = 25; //byte b = i; Error: Incompatible type: conversion from int to byte may result in loss of accuracy}}Copy the code

The concept of floating point types

  • The types used to describe decimal data in the Java language: float and double. Double is recommended
  • A single-precision floating-point number can represent 7 significant digits in the range of -3.403E38 to 3.403E38
  • The double type occupies 8 bytes in the memory space. It is called a double precision floating point number and can represent 15 significant digits ranging from -1.798E308 to 1.798E308.

Programming use of floating point types

package com.lagou.Day02; public class DoubleTest { public static void main(String[] args) { //1. Declare a variable of type float and initialize it // Error: incompatible types: Converting from double to float may cause loss; The default is double //float f1 = 3.1415926; }}Copy the code

Boolean type concept and programming use

In the Java language, types used to describe true and false information: Boolean, values are true and false. The size of a Boolean type in memory is not specified. package com.lagou.Day02;

package com.lagou.Day02; Public class BooleanTest {public static void main(String[] args) {//1. Declare a Boolean variable and initialize Boolean b1 = true; System.out.println(b1); //2. }}Copy the code

The concept of character types

A data type used in the Java language to describe a single character: char. For example: ‘A’, ‘middle’ and so on. Where char type in the memory space accounted for 2 bytes and no sign bit, the range is: 0-65535, because the implementation of life rarely data can be described by a single character, so the future development of the use of more than a String composed of strings, using the String type to describe, such as: The bottom layer of the computer such as’ Hello ‘and’ Kirby ‘only recognizes the binary sequence composed of 0 and 1, which does not meet this rule for groups such as character’ A ‘, so the data cannot be stored directly in the computer. However, in real life, such pattern data need to be stored by the computer. In order for the data to be stored, you can assign a number to the data, and then store the number, which is called ASCII.

The use of character types and numbers

package com.lagou.Day02; public class CharTest { public static void main(String[] args) { char c1 = 'a'; System.out.println(c1); //a System.out.println((int)c1); / / 97}}Copy the code

Remember: ‘0’ = 48; ‘A’ = 65

The concept and use of Unicode character sets

Java character types are encoded in Unicode, the universal fixed-length character set, where all characters are 16 bits.

The concept and use of escape characters

Use of special characters

The concept and use of automatic type conversion

Conversions between basic data types in the Java language: automatic and cast. Automatic type conversion mainly refers to conversion between small types and large types

The concept and use of casts

Target type variable name = (target type) source type variable name;

package com.lagou.Day02; public class Demo04 { public static void main(String[] args) { byte b1 = 10; short s1 = 20; b1 = (byte) s1; System.out.println(b1); / / 20}}Copy the code

The last

Thanks for reading this, feel helpful to you remember to like!