Java basics 2: java Basics
1 keyword
- Definition: a word with special meanings given by the java language.
- Feature: All letters in the keyword are in lower case.
- Keywords used to define data types
- Class, interface, byte, short, int, long, float, double, char, boolean, void
- Keywords used to define data type values
- Keywords used to define Process Control
- If, else, switch, case, default, while, do, for, break, continue, return
- Keyword used to modify the access modifier
- Private, protected, public
- Keywords used to define classes, functions, and variable Modifiers
- Abstract, final, static, synchronized
- Keywords used to define the relationship between classes
- Defines instance creation and instance reference, and determines the instance's keywords
- New, this, super, instanceof
- Keyword used for Exception Handling
- Try, catch, finally, throw, throws
- Keyword used for the package
- Other modifier keywords
- Native, strictfp, transient, volatile, assert
2 Identifier
- Some custom names in the program.
- It consists of 26 uppercase/lowercase letters, numbers 0-9, _, and $.
- Define valid identifier rules:
- ① Numbers cannot start
- ② Keywords cannot be used
- Java is case sensitive.
- Note: When you start a name, try to be considerate.
3. Notes
- The text used for annotation to indicate the interpreter is a annotation.
- This improves the readability of the Code.
- Annotation format in java:
// Single line comment
/* Multi-line comment */
/** Document comment */
- Annotation Version of HelloWorld
Package java002; /*** this is the java program used to demonstrate HelloWorld */public class HelloWorld {/*** main function * @ param args */public static void main (String [] args) {/* output the Hello World statement */System. out. print ("Hello World ");}}
4. constants and variables
- A constant represents a value that cannot be changed.
- Classification of constants in java:
- ① Integer constant, All integers.
- ② Decimal constant, all decimal places.
- ③ Boolean constant. Only two values are true or false.
- ④ Character constant, which identifies a number, letter, or character with single quotation marks.
- ⑤ A String constant that identifies one or more characters with double quotation marks.
- ⑥ Null constant. Only one value is null.
- There are four manifestations of integers:
- Binary
- Octal
- Decimal
- Hexadecimal
- Variable concept:
- A storage area in the memory
- This region has its own name (variable name) and type (data type)
- Data in this region can change continuously within the same type.
- Why define variables?
- It is used to store constants of the same type continuously and can be reused.
- Notes on using variables
- Scope of the Variable
- Initialization value
- Define the variable format:
Data type variable name = initialization value;
- Java is a strongly typed language. It defines specific data types for each type of data and allocates memory space of different sizes in the memory.
- Example: Use of Variables
Package java002. * Description: Variable */public class VariableDemo {public static void main (String [] args) {// data type variable name = initialization value; int num = 100; System. out. print ("num value:" + num );}}
Package java002. * Description: Variable */public class VariableDemo {public static void main (String [] args) {// data type variable name = initialization value; byte B = 127; short s = 128; int num = 100; long l = 12345678910L; float f = 2.3f; double d = 4.5; System. out. print ("the value of B is:" + B); System. out. print ("s value:" + s); System. out. print ("num value:" + num); System. out. print ("l value:" + l); System. out. print ("f value:" + f); System. out. print ("the value of d is:" + d );}}
- Automatic type conversion and forced type conversion
- Small-range type --> large-range type, automatic type conversion.
- Otherwise, type conversion is forced.
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {int x = 3; x = x + 5; System. out. print ("x =" + x );}}
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {byte x = 3; int y = x + 5; System. out. print ("y =" + y );}}
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {byte x = 3; byte y = (byte) (x + 5 ); system. out. print ("y =" + y );}}
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {byte x = 3; byte y = (byte) (x + 200 ); system. out. print ("y =" + y );}}
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {System. out. print ('A'); System. out. print ('A' + 1 );}}
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {byte B = 4; B = 4 + 3; System. out. print (B );}}
5. Operator 5.1: Arithmetic Operator
Package java002. * Description: type conversion */public class TypeConverse {public static void main (String [] args) {int result = 0; int a = 10; int B = 3; System. out. print ("addition:" + (a + B); System. out. print ("subtraction:" + (a-B); System. out. print ("multiplication:" + (a * B); System. out. print ("Division:" + (a/B); System. out. print ("remainder:" + (a % B ));}}
Addition: 13 subtraction: 7 multiplication: 30 Division: 3 remainder: 1
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int I = 1; System. out. println (I ++); System. out. println (I );}}
1 2
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int I = 1; System. out. println (++ I); System. out. println (I );}}
2 2
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3, B; B = a ++; System. out. print ("a =" + a + ", B =" + B );}}
A = 4, B = 3
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3, B; B = ++ a; System. out. print ("a =" + a + ", B =" + B );}}
A = 4, B = 4
5.2 value assignment operator
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 4; int c = 5; System. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; a + = 2; int B = 4; int c = 5; system. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; a-= 2; int B = 4; int c = 5; system. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; a * = 2; int B = 4; int c = 5; system. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; a/= 2; int B = 4; int c = 5; system. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; a % = 2; int B = 4; int c = 5; system. out. print ("a =" + a + ", B =" + B + ", c =" + c );}}
Package java002;/*** packages /8/30. * Note: */public class OperateDemo {public static void main (String [] args) {byte B = 1; B + = 3; System. out. print ("B =" + B );}}