4-Java Basics - variables

Posted by amit.patel on Sun, 28 Nov 2021 20:58:07 +0100

Variable introduction

Why do I need variables?

  • A program is a world. What the world has will be reflected in the program
  • Variables are the basic unit of a program
    Variables have three basic elements, type + name + value
    int a = 1;
public class Var01{

	public static void main(String[] args) {
		
		// Declare variable
		int a;
		a = 100;
		System.out.println(a);

		// You can also say so
		int b = 800;
		System.out.println(b);
	}
}

Type of variable

public class Var02{
	public static void main(String[] args) {
		// Record person's information
		int age = 20;
		double score = 88.9;
		char gender = 'male';
		String name = "king";

		// Output information
		System.out.println("The person's information is as follows:");
		System.out.println(name);
		System.out.println(age);
		System.out.println(gender);
		System.out.println(score);


	}
}

Precautions for variables

  1. Variable represents the storage area in memory. Different types and occupied space are different
  2. This area has its own name and type
  3. Variables must be declared before use
public class VarDetail{

	public static void main(String[] args) {
		
		// Variables must be declared first and then used. There is an existing order
		int a = 50;
		System.out.println(a); // 50
		// The value of this area can change continuously within the same range
		// a = "jack"; //  report errors
		a = 88;
		System.out.println(a);

		// Variables cannot have the same name in the same scope
		// int a = 77; //  report errors
		// Three elements of variable: variable name + type + value
		
	}
}

class Dog {
	public static void main(String[] args) {
		
		int a = 666; // yes
	}
}

+Use of number

  1. When the left and right sides are numerical values, the addition operation is performed
  2. When one of the left and right sides is a string, the splicing operation
public class VarStr{

	public static void main(String[] args) {
		
		System.out.println(100 + 98);
		System.out.println("98" + 100);
		System.out.println(100 + 3 + "hello");
		System.out.println("hello" + 100 + 3);
	}
}

data type

  1. There are two data types in java. One is the basic data type and the other is the reference type
  2. There are 8 basic data types: byte 1, short 2, int 4, long 8, float 4, double 8, char12 and Boolean 1
  3. Reference type, class, interface, array
public class IntDetail{

	public static void main(String[] args) {
		
		// The default value of Java integer variable is int type. L or l should be added to declare long type
		// 
		int n1 = 1;
		// int n2 = 1L;  No, an error is reported because 1L is a long type and cannot be converted to int type
		// This means that if long is used, it needs to be followed by L. it can be automatically converted to long without adding
		long n3 = 1L;

		
	}
}

Precautions for floating point numbers

public class FloatDetail{

	public static void main(String[] args) {
		
		// The java floating-point type is double by default, and the declaration of float needs to add f or F
		// float num1 = 1.1 / / no, no, because the floating point number is double by default
		float num2 = 1.1F;
		double num3 = 1.1; 
		double num4 = 1.1F; // yes
		//
		// Decimal form,
		double num5 = .123; // Equivalent 0.123
		System.out.println(num5);
		// Scientific notation 5.12e2;
		System.out.println(5.12e2);
		System.out.println(5.12e-2);
		// In general, double should be used, with double type precision up to 15 bits and float type precision up to 7 bits
		double num9 = 2.1234567851;
		float num10 = 2.1234567851F;
		System.out.println(num9);
		System.out.println(num10);

		// As long as it is a floating point number, it is inaccurate
		// Look at the code below
		double num11 = 2.7;
		double num12 = 8.1 / 3;
		System.out.println(num11);
		System.out.println(num12);
		if (num11 == num12) {
			System.out.println("num11 == num12 equal");
		}

		// To judge that two floating-point numbers are equal, we should judge that the absolute value of the two differences is within a small precision
		// Right approach
		if(Math.abs(num11 - num12) < 0.000001)
			System.out.println("The difference is small and the two data are considered equal");
		
		System.out.println(Math.abs(num11 - num12));
	}
}

Organization of Java classes


Packages can be understood as folders
Package - > class - > method
Char precautions

public class Char01{
	public static void main(String[] args) {
		
		char c1 = 'a';
		char c2 = '\t';
		char c3 = 'Han';
		char c4 = 97;

		System.out.println(c1);
		System.out.println(c2);
		System.out.println(c3);
		System.out.println(c4); // When c4 is output, the character represented by 97 is output
		

	}
}

https://tool.chinaz.com/Tools/unicode.aspx

public class CharDetail{

	public static void main(String[] args) {
		// In java, char is essentially an integer. When outputting, unicode encodes the corresponding characters
		// Output the corresponding number, and (int) character is required
		char c1 = 97;
		System.out.println(c1);

		char c2 = 'a';
		System.out.println((int)c2);
		char c3 = 'Han';
		System.out.println((int)c3); // 38889
		char c4 = 38889;
		System.out.println(c4);

		// char characters are computable, equivalent to an integer, because they correspond to Unicode codes
		System.out.println('a' +  10);

		// Classroom test
		char c5 = 'b' + 1;
		System.out.println((int) c5);
		System.out.println(c5);

	}
}

code

Coding problem

ASCII code is a set of standards, and the specific code is ISO-8895

Unicode encoding is a set of standards. All Unicode characters are two bytes. The common encoding is UTF-8, which uses 1-6 bytes for encoding

Coding refers not only to the coding of variable types, but also to the coding of files, that is, the files saved by our coding are saved according to 0-1 at the bottom, and different coding methods correspond to different parsing
This is why utf-8 can't work under windows, because dos parses the file according to gbk, but the encoding of the file is utf-8, that is, the donkey's head is not the horse's mouth, which explains why utf-8 is used in memory or transmission, because the letter is one byte, which saves space, but it is saved locally according to Unicode encoding on the disk, because The information saved is complete enough.

boolean type

  • Use details
    false and true cannot be replaced by 0 or non-0 integers, which is different from C language
public class Boolean01{
	public static void main(String[] args) {
		//Demonstrate the case of judging whether the result is passed
		// Define a boolean variable
		boolean isPass = false;

		if(isPass == true){
			System.out.println("Pass the exam, Congratulations");
		} else{
			System.out.println("If you fail in the exam, try hard next time");
		}

	}
}

Data type conversion

  • Automatic type conversion

    Small precision can be automatically converted to high-precision data types, but the reverse is not possible
  • Attention and details of automatic conversion

    java is a strongly typed language, so it maintains high robustness
public class AutoConvertDetail{

	public static void main(String[] args) {
		
		//Detail 1 mixed operation of various types of data, which is automatically converted to large capacity in calculation
		int n1 = 10;
		double d1 = n1 + 1.1; //correct
		// Detail 2: if you assign a value with high precision to a value with low precision, an error will be reported
		// float d1 = n1 + 1.1; / / an error is reported, because n1 + 1.1 is a double type, and the double type cannot be automatically converted to float type
		float d2 = n1 + 1.1F; // correct


		System.out.println(d1);

		// Detail 3: byte, short and char cannot be automatically converted
		// When assigning data to byte, first judge whether it is within the byte range
		byte b1 = 10;
		int n2 = 1; // n2 is of type int
		// byte b2 = n2; / / wrong. If it is a variable assignment, judge the type
		// char c1 = b1; / / error, because byte and char cannot be automatically converted

		// Detail 4: byte, short and char can be calculated with each other and converted to int first
		byte b3 = 1;
		byte b4 = 1;
		short s1 = 1;
		// short s2 = b3 + s1; / / an error is reported because the result is int
		int s2 = b3 + s1;

		// byte b5 = b3 + b4; / / an error is reported. As long as byte participates in the operation, the result is of type int

		// Detail 5: boolean does not participate in conversion
		boolean pass = true;
		// int num100 = pass; // boolean does not participate in automatic type conversion
		
		// Detail 6: automatically promote the expression result to the maximum type
		// example
		byte b6 = 1;
		short s3 = 10;
		int num200 = 1;
		double num300 = 1.1;

		float num500 = b4 + s3 + num200 + num300;// report errors
		double num500 = b4 + s3 + num200 + num300;


	}
}

Cast type

  • Cast may result in precision reduction or overflow
public class ForceConvert{

	public static void main(String[] args) {
		
		// Demonstrate cast
		int n1 = (int) 1.9;
		System.out.println("n1=" + n1); // Resulting in reduced accuracy

		int n2 = 2000;
		byte b1 = (byte) n2;

		System.out.println(b1); // Cause data overflow

	}
}
  • Details of cast
public class ForceConvertDetail{
	public static void main(String[] args) {
		
		// Demonstrate cast
		// Detail 1: strong conversion is only valid for the most recent operands, and parentheses are often used to improve the priority
		// int x = (int) 10 * 3.5 + 6 * 1.5; / / compilation error
		int x = (int)(10 * 3.5 + 6 * 1.5);
		System.out.println(x);
		// char type can save the constant value of int, but cannot save the variable value of int
		char c1 = 100;
		int m = 100;
		// char c3 = m; / / an error is reported
		char c3 = (char)m;
		System.out.println(c3);

		// byte and short types are calculated and treated as int types


	}
}

Conversion of basic data type and String type

  • Introduction and use
public class StringToBasic{

	public static void main(String[] args) {
		
		int n1 = 100;
		float f1 = 1.1F;
		double d1 = 4.5;
		boolean b1 = true;

		String s1 = n1 + "";
		String s2 = f1 + "";
		String s3 = d1 + "";
		String s4 = b1 + "";

		System.out.println(s1 + " " + s2 + "  " + s3 + " " + s4);

		// Convert String to basic data type
		// Call parse through the wrapper class of the basic type
		String s5 = "123";
		// It will be explained in detail on OOP
		int num1 = Integer.parseInt(s5);
		double num2 = Double.parseDouble(s5);
		float num3 = Float.parseFloat(s5);
		long num4 = Long.parseLong(s5);
		byte num5 = Byte.parseByte(s5);
		boolean num6 = Boolean.parseBoolean("true");
		short num7 = Short.parseShort(s5);
		// char num8 = Char.parseChar(s5); / / no
		System.out.println(num1);
		System.out.println(num2);
		System.out.println(num3);
		System.out.println(num4);
		System.out.println(num5);
		System.out.println(num6);
		System.out.println(num7);
		// How to convert a string into characters? Take the first character of the string
		System.out.println(s5.charAt(0));


	}
}
  • matters needing attention
  • When converting a string to a basic data type, ensure that the data is valid, such as converting hello to a number
  • If the format is incorrect, an exception will be thrown, but there is no problem with compilation
public class StringToBasicDetail{

	public static void main(String[] args) {
		
		String str = "hello";
		// Convert to int
		int n1 = Integer.parseInt(str);
		System.out.println(n1);
	}
}

Topics: Java