As we all know, Java is an object-oriented programming language. The best thing about it is that it is cross-platform. You can write Java source code on Windows and execute the compiled bytecode on Linux without making any changes to the source code.

01. Data type

Java has two types of data, basic data type and reference type.

Basic data types are used to store simple types of data, such as int, long, byte, and short for integers, float and double for floating-point numbers, char for characters, and Boolean for booleans.

Different basic data types, with different default values and sizes, to feel the table.

The data type The default value The size of the
boolean false 1 bit
char ‘\u0000’ 2 –
byte 0 1 byte
short 0 2 –
int 0 4 bytes
long 0L 8 bytes
float 0.0 f 4 bytes
double 0.0 8 bytes

The reference type is used to store references to objects (null means an object with no value), and String is the best representation of the reference type, such as String cmower = “Silent King two”.

02. Declare variables

To declare a variable, you must specify its name and type. Here’s a simple example:

int age;
String name;
Copy the code

Count and name get a default value when they are declared, depending on their data type — not local variables (otherwise the Java compiler will remind you to assign values first when you use variables), but class member variables.

public class SyntaxLocalVariable {
    int age;
    String name;


    public static void main(String[] args) {
        SyntaxLocalVariable syntax = new SyntaxLocalVariable();
        System.out.println(syntax.age); / / 0
        System.out.println(syntax.name);  / / output is null}}Copy the code

It is also possible to assign a variable after declaring it using the “=” operator, as follows:

int age = 18;
String name = "Silent King II.";
Copy the code

We define two variables, age of int and name of String. Age is assigned 18 and name is assigned “Silent King 2”.

Each line of code is followed by a “;” , indicating that the current statement ends.

In Java, it is best for variables to follow naming conventions to make code more readable.

  • Start with a letter, underscore (_), or dollar sign ($)
  • Do not use Java reserved words, such as int, as variable names

03, arrays,

Arrays play an important role in Java as the underlying implementation of many collection classes. An array is a reference type that stores data of a specified type.

The general syntax for declaring an array is as follows:

type[] identiier = new type[length];
Copy the code

Type can be any primitive data type or reference type. Consider the following example:

public class ArraysDemo {
    public static void main(String[] args) {
        int [] nums = new int[10];
        nums[0] = 18;
        nums[1] = 19;
        System.out.println(nums[0]); }}Copy the code

The index of the array starts at 0, with the first element having an index of 0 and the second element having an index of 1. Why is it designed this way? If you’re interested, you can explore it.

The element at the specified index is accessed by the variable name [index], and the assignment or value is the same.

04, key words

Keywords are reserved words that have special meanings in Java, such as public, final, static, new, and so on. They cannot be used as variable names. For your reference, I’ve compiled a list of 48 commonly used keywords that you can take a look at.

  1. Abstract: The abstract keyword is used to declare abstract classes, which can have abstract and non-abstract methods.
  2. Boolean: The Boolean keyword is used to declare a variable as a Boolean type. It has only true and false values.
  3. Break: The break keyword is used to break a loop or switch statement.
  4. Byte: The byte keyword is used to declare a variable that can hold eight bits.
  5. Case: The case keyword is used to mark the value of the condition in the switch statement.
  6. Catch: The catch keyword is used to catch exceptions in try statements.
  7. Char: The char keyword is used to declare a variable that can hold Unicode characters with unsigned 16-bit bits.
  8. Class: The class keyword is used to declare a class.
  9. Continue: The continue keyword is used to continue the next loop. It can skip the rest of the code under specified conditions.
  10. Default: The default keyword is used to specify the default code block in the switch statement except for case conditions.
  11. Do: The do keyword is usually used with the while keyword, followed by the body of the loop.
  12. Double: The double keyword is used to declare a variable that can hold a 64-bit floating-point number.
  13. Else: The else keyword is used to indicate alternate branches in an if statement.
  14. Enum: The enum keyword is used to define a fixed set of constants.
  15. Extends: The extends keyword is used to indicate that a class inherits from another class or interface.
  16. Final: The final keyword is used to indicate that the variable is immutable.
  17. Finally:Finally keyword andtry-catchUsed in conjunction, this means that code ina finally block is always executed whether or not an exception is handled.
  18. Float: The float keyword is used to declare a variable that can hold a 32-bit floating point number.
  19. For: The for keyword is used to start a for loop, which is recommended if the number of loops is fixed.
  20. If: The if keyword is used to specify the condition, and if the condition is true, the corresponding code is executed.
  21. The implements: implements keyword is used to implement an interface.
  22. Import: The import keyword is used to import the corresponding class or interface.
  23. Instanceof: The instanceof keyword is used to determine whether an object belongs to a certain type (class).
  24. Int: The int keyword is used to declare a signed integer variable that can hold 32 bits.
  25. Interface: The interface keyword is used to declare interfaces — only abstract methods.
  26. Long: The long keyword is used to declare a variable that can hold a 64-bit integer.
  27. Native: The native keyword is used to specify that a method is implemented by calling the native interface (not Java).
  28. The new: new keyword is used to create a new object.
  29. Null: If a variable is empty (there is no reference to it), it can be assigned null.
  30. Package: The package keyword is used to declare the package in which the class resides.
  31. Private: The private keyword is an access modifier that indicates that a method or variable is visible only to the current class.
  32. The protected: protected keyword is also an access modifier, indicating that a method or variable is visible to classes and all subclasses within the same package.
  33. Public:The public keyword is another access modifier that allows you to declare classes as well as methods and variables (visible to all classes).main()Methods must be declared public.
  34. Return: The return keyword is used to return (a value) after code execution is complete.
  35. Short: The short keyword is used to declare a variable that can hold a 16-bit integer.
  36. Static: The static keyword indicates that the variable or method is static.
  37. Strictfp: The strictFP keyword is uncommon and is usually used to modify a method to ensure that the floating-point operations in the method body perform the same results on each platform.
  38. Super: The super keyword can be used to call methods or variables of the parent class.
  39. Switch: The switch keyword is usually used for three or more criteria.
  40. Synchronized: The synchronized keyword is used to specify synchronized methods, variables, or code blocks in multithreaded code.
  41. This: The this keyword can be used to reference the current object in a method or constructor.
  42. Throw: the throw keyword actively throws an exception.
  43. Throws: Throws The keyword is used to declare exceptions.
  44. Transient: The transient keyword is used in serialization. The fields it modifies will not be serialized.
  45. Try: The try keyword is used to wrap the block of code to catch the exception.
  46. Void: The void keyword is used to specify that a method has no return value.
  47. Volatile: The volatile keyword guarantees visibility to different threads operating on the variables it modifies. That is, if one thread modifies the value of a variable, the new value is immediately visible to other threads.
  48. While: If the number of loops is not fixed, a while loop is recommended.

05. Operators

In addition to the “=” assignment operator, there are many other Java operators that can be used, so let’s take a look.

① Arithmetic operators

  • Plus (plus sign)
  • — (minus sign)
  • * (multiplication sign)
  • / (divisor)
  • % (mod)

Here’s an example:

public class ArithmeticOperator {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        
        System.out.println(a + b);/ / 15
        System.out.println(a - b);/ / 5
        System.out.println(a * b);/ / 50
        System.out.println(a / b);/ / 2
        System.out.println(a % b);/ / 0}}Copy the code

The + sign is special and can also be used for string concatenation. Here’s an example:

String result = "Silent King II." + "An interesting programmer.";
Copy the code

② Logical operators

Logical operators are commonly used in Boolean expressions. Common ones are:

  • && (AND) the result is false if one of the conditions is false
  • | | (OR) multiple conditions as long as have a result to true is true
  • ! (NOT) If true, add “!” Is false, otherwise, and vice versa.

Here’s an example:

public class LogicalOperator {
    public static void main(String[] args) {
        int a=10;
        int b=5;
        int c=20;
        System.out.println(a<b&&a<c);//false
        System.out.println(a>b||a<c);//trueSystem.out.println(! (a<b));// true}}Copy the code

③ Comparison operator

  • <(less than)
  • < =(less than or equal to)
  • >(greater than)
  • > =(Greater than or equal to)
  • = =(equal)
  • ! =(range)

06. Program structure

The smallest unit of program in Java is called a class, and a class can have one or more fields (also known as member variables), one or more methods, and even inner classes.

If a class wants to execute, it must have a main method — the entrance through which the program runs, like a human mouth.

public class StructureProgram {
    public static void main(String[] args) {
        System.out.println("No member variables, only one main method."); }}Copy the code
  • It’s called StructureProgram, and inside it, there’s only one main method.
  • {}The code between is called a code block.
  • The above source code will be stored in a file with the suffix Java.

07. Compile and execute the code

In general, some tutorials cover this by recommending that you compile the source code into a bytecode file by executing javac commands from the command line, and then specifying the code by executing Java commands.

However, I don’t want this situation to continue – it takes courage and patience for a beginner to install and configure the JDK, and if he or she makes a mistake, he or she will give up before getting started. Besides, compiling source code at the command line leads to a lot of unaccountable errors, which can be deadly for beginners — if you ever run into one of these old-fashioned tutorials again, spit it out.

A good way is to download IntelliJ IDEA, referred to as IDEA, which is recognized as the best Java integrated development tool in the industry, especially in intelligent code assistant, code automatic prompt, code reconstruction, code version management (Git, SVN, Maven), unit testing, code analysis and other aspects have a bright play. IDEA is made in the Czech Republic (located in Eastern Europe) and its developers are known for their rigor. IDEA is divided into community version and paid version of two versions, novice directly download the community version is enough to use.

Once the installation is complete, you can start typing code, then right-click to Run it (without even saving), and the results will be displayed in the Run panel, as shown below.

To see the decompiled bytecode, you can find a structureProgram.class file in the SRC sibling directory target/classes (if you can’t find it, right-click on the directory and select Reload from Disk).

You can double-click to open it.

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


package com.cmower.baeldung.basic;


public class StructureProgram {
    public StructureProgram(a) {}public static void main(String[] args) {
        System.out.println("No member variables, only one main method."); }}Copy the code

IDEA by default decompiles the class bytecode into Java code we can read using Fernflower. In fact, the class bytecode (please install show Bytecode) looks like this:

/ / class version 57.65535 (65479) / / access flags 0 x21 public class com/cmower baeldung/basic/StructureProgram {/ / compiled from: StructureProgram.java // access flags 0x1 public <init>()V L0 LINENUMBER 3 L0 ALOAD 0 INVOKESPECIAL java/lang/Object.<init> ()V RETURN L1 LOCALVARIABLE this Lcom/cmower/baeldung/basic/StructureProgram; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 // access flags 0x9 public static main([Ljava/lang/String;)V L0 LINENUMBER 5 L0 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "\u6ca1\u6709\u6210\u5458\u53d8\u91cf\uff0c\u53ea\u6709\u4e00\u4e2a main \u65b9\u6cd5" INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L1 LINENUMBER 6 L1 RETURN L2 LOCALVARIABLE args [Ljava/lang/String; L0 L2 0 MAXSTACK = 2 MAXLOCALS = 1 }Copy the code

Novice looks still some meng force, suggested to have an eye addiction on the line.

Machine Learning Online Manual Deep Learning online Manual AI Basics download (PDF updated to25Set) site QQ group1003271085To join the wechat group, please reply to "add group" to get a discount station knowledge planet coupon, please reply to "knowledge planet" like the article, click on itCopy the code