White wrote to the entry-level look at the basic syntax of Java, it is strongly recommended

As we all know, Java is an object-oriented programming language. Best of place it is that it is cross-platform, you can write Java source code on Windows operating system, and then execute the compiled byte code on the Linux operating system, without having to make any changes to the source code.

01, data types
Java has two kinds of data types, one is the basic data types, one is a reference type.

The basic data types for storing the simple data type, for example, int, long, byte, short for storing integer, float, double float for storing, char for character, boolean a boolean value is stored.

Different types of basic data, there are different defaults and size to the next table felt.

The default size of data type bit boolean false 1 char '\ u0000' 2 bytes byte 0 1 byte short 0 2 byte int 0 4 bytes long 0L 8 bytes float 0.0f 4-byte double 0.0 8 bytes

Reference type is used to store objects (null means no object value) references, String is the best representative reference types, such as String cmower = "silent king."

02, declare variables
to declare a variable, you must specify its name and type, look at a simple example:

int age; String name; Copy the code

count and name after the declaration will be a default value, according to their data type - can not be a local variable (otherwise the Java compiler will remind first assignment when you use variables), you must be a class member variable.

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); // 输出 null } } 复制代码

It may also be used after a variable declared "=" assignment operator, as follows:

int age = 18; String name = "silent king"; duplicated code

We define two variables, int age and type of type String name, age assignment 18, name assigned to the "silent king."

Each subsequent line of code related to a ";" represents the end of the current statement.

In Java, the best variable to comply with naming conventions, this will improve the readability of the code.

Letter, underscore (_) or dollar sign ($) at the beginning
can not use Java reserved words, such as int not be used as a variable name
03, an array of
arrays occupies an important position in Java, it is a lot of underlying collection class implementation. It belongs to the type of array reference, which is used to store data of a given type.

The general syntax for declaring an array is as follows:

type [] identiier = new type [length]; duplicated code

It may be any type of elementary data type or a 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]); } } 复制代码

Array index starts from 0, the index of the first element is 0, the index 1 of the second element. Why this design? Interested, you can go to explore it.

By variable name [index] of ways to access the array element at the specified index, or the value of the assignment is the same.

04, keyword
Keywords are reserved words have special meaning in Java, such as public, final, static, new, and so on, they can not be used as a variable name. In order to facilitate you as a reference, I have listed 48 popular keywords, you can look a look.

abstract: abstract keyword is used to declare an abstract class - may have abstract and non-abstract methods.
boolean: boolean keyword used to declare a variable of type boolean, it has only two true and false values.
break: break keyword is used to break the cycle or switch statement.
byte: byte is used to declare a keyword can accommodate 8 bits variable.
case: case keyword is used to mark the value of the condition in a switch statement.
catch: catch keyword is used to catch the exception try statement.
char: char can receive keyword is used to declare a variable unsigned 16-bit Unicode character bits.
class: class keyword is used to declare a class.
continue: continue keyword is used to continue the next cycle. It can skip the rest of the code under the specified conditions.
default: default key switch statement specifies the default case block removed outside conditions.
do: do keywords typically used in conjunction with and while keyword, followed by the do loop.
double: double keyword is used to declare a variable that can accommodate 64-bit floating-point number.
else: else if statement is used to indicate keywords spare branch.
enum: enum (enumeration) keyword is used to define a fixed set of constants.
extends: extends keyword is used to indicate a class inherits from another class or interface.
final: final keyword is used to indicate that the variable is not changed.
finally: try-catch and finally with keywords, refers to whether abnormal process is always executing code in the finally block.
float: float for declaring a keyword can accommodate 32-bit floating-point variables.
for: for keyword is used to start a for loop, if the number of cycles is fixed, it is recommended to use a for loop.
if: if the keyword is used to specify the condition, if the condition is true, then the corresponding code is executed.
implements: implements keyword is used to implement the interface.
import: import a keyword for introducing a corresponding class or interface.
instanceof: instanceof keyword is used to determine whether an object belongs to a certain type (class).
int: int keyword is used to declare a variable that can accommodate 32-bit integer with a sign.
interface: interface keyword is used to declare an interface - can have only abstract methods.
long: long keyword is used to declare a variable that can accommodate 64-bit integer.
native: native keyword is used to specify a method by calling Native Interface (non-Java) implementation.
new: new keyword is used to create a new object.
null: if a variable is empty (no reference to what point), you can assign it to null.
package: package keyword is used to declare the class package is located.
private: private keyword is an access modifier, representation or variables are only visible to the current class.
protected: protected access modifier is a keyword, or variables visible representation of all classes and subclasses in the same package.
public: public key is another access modifier, in addition to declare methods and variables (all classes visible), you can also declare class. main () method must be declared as public.
return: return key for returning to (a value) after the code is completed.
short: short keyword used to declare a variable that can accommodate 16-bit integers.
static: static keyword indicates that the variable or method is a static variable or static method.
strictfp: strictfp keyword is not common, usually used to modify a method to ensure the same results in vivo method of floating-point operations performed on each platform.
super: super keyword can be used in a method or variable call the parent class.
switch: switch keyword commonly used in three (or more) conditions for determination.
synchronized: synchronized keyword is used to specify multithreaded code synchronization method, variable, or a code block.
this: this key can be used to refer to the current object constructor or method.
throw: throw keyword active thrown.
throws: throws keyword is used to declare an exception.
transient: transient keywords used in the sequence of use, it modified field will not be serialized.
try: try to capture keywords for wrapping exception block.
void: void keyword is used to specify the method has no return value.
volatile: volatile keyword to ensure the visibility of the different threads when it modified the operating variables, namely a thread modifies the value of a variable, this new value to other thread is immediately visible.
while: If the number of cycles is not fixed, it is recommended to use a while loop.

White wrote to the entry-level look at the basic syntax of Java, it is strongly recommended

05, the operator
remove the "=" assignment operator, Java, there are many other roles of operators, we look briefly.

①, arithmetic operators

+ (Plus sign)
- (minus sign)
* (multiplication)
/ (division
number) % (remainder)
look at 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 } } 复制代码

"+" Special number can also be used in string concatenation, look at an example:

String result = "silence the king," + "an interesting programmers"; Copy the code

②, logical operators

Logical operators Boolean expressions commonly used, common are:

&& multiple conditions (AND) as long as there is a false result is false
|| (OR) as long as there is a plurality of conditions is true result is true
! (The NOT) If the condition is true, with "!" Is on false, or vice versa.
Look at 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);//true System.out.println(!(a<b)); // true } } 复制代码

③, comparison operators

<(Less than)
<= (less than or equal to)

(Greater than)
= (greater than or equal to)
== (equal to)
! = (Range)
06, program structure
Java program in a minimum unit called a class, a class can have one or more fields (also called variable member), there may also be one or more methods, you can even have some internal classes.

, There must be a main method of a class if you want to perform - running entrance, just like the human mouth, like, ah, can be so far-fetched to understand it.

public class StructureProgram {public static void main (String [] args) {System.out.println ( "no member variables and only a main method");}} And

Class is named StructureProgram, in it, there is only one main method.
Called code blocks of code between the {}.
Over the source code will be saved in a file called java in the suffix.
07, compile and then execute the code
usually some tutorials in the introduction of this piece of content when, I suggest you first execute javac command from the command line to compile the source code into bytecode files, and then execute the java command to specify the code.

But I do not want this bad situation be allowed to continue - novice really quite JDK installation configuration requires courage and patience, a little careless, did not give up on the first entry. Moreover, the command line to compile the source code will encounter many inexplicable error, which the novice is extremely deadly - if you encounter this old-fashioned course, you can spit up.

Good way is to download IntelliJ IDEA, referred to as IDEA, it is widely recognized as the best Java integrated development tools, especially in the intelligent code assist, code tips, code refactoring, code version management (Git, SVN, Maven) aspects, unit testing, code analysis etc. has a dazzling play. IDEA produced in the Czech Republic (in Eastern Europe), a developer known for rigorous. IDEA community is divided into two versions and paid versions, novice Direct download Community Edition will be enough.

After a successful installation, you can start knocking the code, and then run just right (even saved are saved), the results will be displayed in the Run panel, as shown below.

White wrote to the entry-level look at the basic syntax of Java, it is strongly recommended

To get byte code decompilation, you can find a file StructureProgram.class at the same level src directory target / classes package path of (if not, then right click on the directory "Reload from Disk") .

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 () {} public static void main (String [] args) {System.out.println ( "no member variables and only a main method");}} And

IDEA default with Fernflower the class bytecode decompile Java code as we can see understand. In fact, class byte code (please install show bytecode plug-ins) long look like this:

// class version 57.65535 (-65479) // access flags 0x21 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 } 复制代码

Novice still looks somewhat ignorant force recommended worked on the eye addiction on the line.

White wrote to the entry-level look at the basic syntax of Java, it is strongly recommended

Well, my dear readers, that's the entire contents of the article. Learn any programming language, in my opinion, the methods are the same, and that is kept to practice, do not feel easy to understand, refused to try hands - your left hand and the right hand is your best teacher .

I feel a little point to remember with praise Oh!

Guess you like

Origin blog.51cto.com/14783151/2485518