JAVA Learning (III): Basic Java syntax (conversion of variables, constants, data types, operators and data types), java Operators
Basic Java syntax (conversion of variables, constants, data types, operators, and data types)
1. Variables
In Java, you can declare variables by specifying data types and identifiers. The basic syntax is:
DataType identifier;
Or
DataType identifier = value;
DataType is a variable type, such as int/string/char/double/boolean, identifier is the variable name, that is, identifier, and value is the value of the declared variable.
Note:
A. the identifier consists of digits 0-9, uppercase and lowercase letters, underscores, dollar signs, Renminbi signs, and all ASCII codes before the hexadecimal 0x0;
B. Class or Structure Variables. If Initialization is not displayed, the default initial value is 0. the variables in the method must be displayed for initialization; otherwise, an error occurs.
Variables can be divided:Class variable, local variable, method parameter variable, exception handling parameter variable.
The sample code is as follows:
/*************************************** **************************************** ** Sample code for using "class variables" */class ClassVariable {int price = 100; // define the class variable priceString name; // define the class variable nameint num; // define the class variable num} public class Grammar {public static void main (String [] args) {ClassVariable c = new ClassVariable (); System. out. println ("price =" + c. price); System. out. println ("name =" + c. name); System. out. println ("num =" + c. num );}}///// /// // ** "Local variable "sample Code for use */public class Grammar {public static void main (String [] args) {int a = 7; if (5> 3) {int s = 3; System. out. println ("s =" + s); System. out. println ("a =" + a);} // System. out. println ("s =" + s); System. out. println ("a =" + );}} //////////////////////////////////////** sample code for "method parameter variables" */public class Grammar {public static void testFun (int s) {System. out. println ("s = "+ S);} public static void main (String [] args) {testFun (100 );}} //////////////////////////////////////** sample code for "exception handling parameter variables" */public class Grammar {public static void test () {try {System. out. println ("Hello! Exception! ");} Catch (Exception e) {// Exception Handling block. The parameter is Exception type e. printStackTrace () ;}} public static void main (String [] args) {test ();}}
2. Constants
In C/C ++, a constant must be defined with the const keyword. in JAVA, const is not a key character, but a reserved character. It usesFinal keywordsTo define a constant. Its syntax is as follows:
Final dataType constantName;
Final is the keyword for defining constants, dataType is the data type indicating constants, and constantName is the constant name.
Note:
A. initialize a constant;
B. final can be used not only to modify constants of basic data types, but also to modify object references or methods.
C. in JAVA, constants generally use uppercase characters to distinguish them from variables.
The sample code is as follows:
/*************************************** **************************************** ** Example code of "constant" usage */public class Grammar {public static void main (String [] args) {final double PI = 3.1415926; int r = 10; double c = 2 * PI * r; System. out. println ("circle perimeter =" + c); // PI = 10 ;}}
3. Data Type
JAVA data types are mainly divided into two categories:Basic Data Type(Byte, character char, boolean, float, double, int, short, and long) andReference data type(Array, class, interface ).
For example:
Note:
A. The size of all basic data types has been clearly defined and remains unchanged on different platforms. This feature helps improve JAVA program portability;
B. The referenced data type is customized by the user and is used to restrict other data types,The pointer type, structure type, union type, and enumeration type in C ++ are not supported in JAVA.;
C. For floating point data, the default value is double. If you want to be treated as float, you need to add f or F to the data;
D. The character types in Java are represented by two bytes of Unicode encoding. The characters are usually expressed in hexadecimal notation and range from \ u0000 to \ uFFFF, that is, 0 to 65535.
The sample code is as follows:
/*************************************** **************************************** ** Sample code for "Integer type" */public class Grammar {public static void main (String [] args) {byte a = 10; short B = 20; int c = 30; long d = 40; long sum = a + B + c + d; System. out. println ("10 + 20 + 30 + 40 =" + sum );}} /** sample code for "floating point type" */public class Grammar {public static void main (String [] args) {double x = 10.23; int a = 2; float sum = (float) x * a; System. out. println ("sum =" + sum) ;}/ ** example code for using boolean type */public class Grammar {public static void main (String [] args) {boolean a = true; System. out. println ("a =" + a) ;}/ ** sample code for using the character type */public class Grammar {public static void main (String [] args) {char a = 'a'; char B = 'B'; System. out. println ("a + B =" + (a + B ));}}
4. Operators
Operators include Arithmetic Operators, comparison operators, logical operators, value assignment operators, conditional operators, auto-increment and auto-increment operators, and bitwise operators.
Note:
A. Self-incrementing auto-subtraction operators, such as I ++ and ++ I, whose rule is who is counted first;
B. In bitwise operators, the bitwise XOR operator is ^. Pay special attention to this;
The sample code is as follows:
/*************************************** **************************************** ** Example code of how to use the "Arithmetic Operator" */import java. util. future; // After the code is entered into the future, the System will automatically add this code public class Grammar {public static void main (String [] args) {custom input = new partition (System. in); // System input System. out. println ("Enter the first number:"); int num1 = input. nextInt (); System. out. println ("enter the second number:"); int num2 = input. nextInt (); float add = num1 + num2; float sub = num1-num2; float multi = num1 * num2; int div = num1/num2; float rem = num1 % num2; System. out. println ("add =" + add + ", sub =" + sub + ", multi =" + multi + ", div =" + div + ", rem = "+ rem) ;}}/**" Priority "example code */public class Grammar {public static void main (String [] args) {int a = 10; int B = 5; int c = 12; int result = a + B-(a ++) * (B --) % c; System. out. println ("result =" + result );}}
5. Data Type Conversion
Data type conversion can be classified into implicit conversion (automatic type conversion) and explicit conversion (forced type conversion.
Automatic type conversionTwo conditions must be met: one is that the two data types are compatible with each other, and the other is that the value range of the target type must be greater than that of the source data type, that is, the low-level data type is converted to the high-level data type.
The conversion rules are as follows:
(1) convert numeric data: byte-> short-> int-> long-> float-> double;
(2) convert string type to integer type: char-> int.
The conversion of the above data types follows the conversion order from left to right, and is finally converted to the Data Type of the variable with the largest range in the expression.
Display Conversion: If the two data types are incompatible or their values are smaller than the source type, automatic conversion cannot be performed. In this case, forced type conversion is required.
6. Example -- convert an integer to a binary number
The reference code is as follows:
/*************************************** **************************************** ** "Practice -- convert Integers to binary" -- difficulty coefficient:★★★*/Public class Grammar {// Step 1: Define a class static String ConvertIntToBinary (int n) // Step 2: Define a method {String binary = ""; // Save the binary string int I = n; // declare the I variable and assign the value of parameter n to int m = 0; // declare the initialization variable mwhile (I> 1) {I = n/2; m = n % 2; binary = Integer. toString (m) + binary; // Add to binary string n = I;} if (I> 0) binary = "1" + binary; return binary ;} public static void main (String [] args) {System. out. println ("14 to binary:" + ConvertIntToBinary (14); System. out. println ("15 converted to binary:" + ConvertIntToBinary (15 ));}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.