JAVA start (Basics)

type of data

Boolean 1 bit
Byte 1 byte (8 bits)
Short 2 bytes
Char 2 bytes
Int 4 bytes
Long 8 bytes

The default type
is used by default and double int, float to be used if necessary to add long or behind which the digital L or F

float ff=3.28F;
long lo=2000000000000000L;

Why Char is two bytes
because in C char is ascll character set, up to 255, while the java char is Unicode character set, up to 65535, which houses the character countries.
So C can not be used in Chinese characters as a variable name, you can java

char ch1=1000;
char ch2='A';
char ch3='东';
char ch4='\r';

Boolean type
Boolean type only have one, it is not like the C language can achieve zero that is true, the value must be true or flase

boolean b1=100;//报错
boolean b2=true;

Auto Switch
small type is automatically converted to large type
Byte-> short-> int-> long-> float-> double

Amount and calculation

Variables and Constants

Class variables: variables are automatically endowed with zero initial value attributable to the class object must be used by the class object
class static variables: variables are automatically endowed with zero initial value attributable to the class, it is best to use the class name to call
local variables: local variables without early value must be manually assigned before you can use

Constant: immutable amount value (final int II = 10;)

Scope
The scope of local variables is {}

Three head operations

int II. 3 =>. 8. 1: 2;? // Output 2

Switch () ... case
its value can be numbers, characters, strings, the string is not possible before, but even then the string can be used JDK1.7

Bit arithmetic or logic operation

Bitwise: [&], [|], [-], [] ^ (bitwise exclusive-or, the same return 0 return 1 different)
logic operations: [&], [&&], [|], [|| ], [!]

                                          Photos from the Shangxue Tang

Example:
"!" "~" And the difference between:
1 "~" binary bit to the contrary, is (0000 0001) 2, 1 = 1111 1110 ~
"!" 2 logically inverted, non-zero is true , 0 is false

computation

random number

D = Math.random double (); // returns the interval [0,1)

Exponentiation
public static double pow (double a, double b)

System.out.println (Math.pow (2,. 3)); // Output: 8.0

平方根
public static double sqrt(double a)

System.out.println (the Math.sqrt (. 9)); // Output: 3.0

Console input

next () takes a string, space or carriage return end
nextLine () takes a string, enter the end
the nextInt () receiving a data type int
nextDouble () receiving a double data type
nextBoolean () receives a boolean data type

Import java.util.Scanner;
 public  class the Main {
     public  static  void main (String [] args) {     
        Scanner SC = new new Scanner (the System.in); // get the input object 
        String name = sc.nextLine (); // Get his string 
        int I = sc.nextInt (); // Get digital 
        System.out.println ( "name =" + name + "= I" + I); // output 
    } 
}

Output escape character

【\】、【”】、【”】、【\n】

System.out.println ( "n-\\"); // Output: \ n

abnormal detection

Abnormal statement block

the try {
     // be detected abnormality statement 
    
} the catch (Exception E) {
     // process the statement after the abnormal 
    
} the finally {
     // statements must be executed 
}

Throw an exception

public  class the Main {
     public  static  void main (String [] args) {     
    
    // must call the function abnormality detecting 
      the try { 
        TT (); 
      } the catch (Exception E) { 
        System.out.println ( "function call is ---- throwing an exception ---- " ); 
        e.printStackTrace (); 
      } 
    } 
    // thrown 
    public  static  void TT () throws exception {
         int X =. 6/0 ; 
    } 
}

JAVA environment

1, seen from the drawing, comprising three relations, comprising the JDK JRE, JRE comprising the JVM;
2, the development of a Java program must be installed the JDK
. 3, JRE is like running a Java program
4, JVM virtual machine, corresponding to the JVM each virtual machine system provided by the government, we can not consider the differences in each platform when developing Java programs, you can achieve the effect of a program used everywhere.
5, JDK Download: https: //www.oracle.com/technetwork/java/javase/downloads/index.html

The first command line java program

Code:

Free to create a new directory Myjava, which creates a new file Dong.java

public class Dong{
    public static void main(String[] args){
        System.out.println("DongXiaoDong");//输出
    }
}

Enter the directory

cd / d E: \ Myjava

Compiler
compiler will be the same name .class file, the file for the virtual machine interpreter file

javac Dong.java

carried out

java Dong

operation result

 When an error occurs:

 

Editing tools to download and install

download

官网下载
https://www.eclipse.org/downloads/
本文使用:
链接:https://pan.baidu.com/s/10ZDqTZbhnQy6sGnYmw0_tw
提取码:sxz2
安装

 

选择工作空间
工作空间是存放工程的地方,可以勾选下面的记住此次选择

新建工程

新建类

 

Guess you like

Origin www.cnblogs.com/dongxiaodong/p/11274657.html