Java SE Quick Start--1

I won't talk about environment variables and installation here, just follow the tutorial above on Baidu.

hello word

public class HelloWord{
    public static void main(String args[]){
        System.out.println("Hello Word");
    }
}


It is worth noting that args is a command line parameter. We first run the java program through javac HelloWord.java and then java Helloword under dos . At the same time, we can also, java Helloword 123
At this time, we can get this parameter in args .
System.out.println() is output with newline, and System.out.print() is output without linefeed.

java basic data types

byte occupies 1 byte, -128-127 range
short occupies 2 bytes, -2^15-2^15-1
int occupies 4 bytes, -2^31-2^31-1
long occupies 8 bytes, 2^63-2^63-1
double occupies 8 bytes,
char occupies 1 byte ,
float occupies 2 bytes, usually used to represent the prime of 1 decimal,
boolean occupies 1 position, It is used to judge true
and false . Compared with the basic types in C, there is one more type byte that directly manipulates bytes.
It is worth noting that the difference between float and double, they can both represent decimals, and double can represent a larger range, but in In terms of processing speed, float is more dominant. We use float for known numbers with few decimal places and the number is not large enough, while the number can be larger and requires more decimal places. In this case, double should be used. , the number of significant digits that a float can represent is 8, while a double can have 16.

        float as = 123412341f;
        float as2 = as + 1;
        System.out.println(as == as2);

Q: The above prints true and false (the meaning of true and false here means == is true, if true, it is true, if not, false), the answer is, true, the reason is that flare can only represent an 8-bit valid array, And here, 123412341 exceeds the specification length, so +1 is invalid, and float only stores the content of 12341234, of course, it will remain like this, 1.23412344E8

Declaration of Identifier

What is an identifier?

int a = 1;

This a is an identifier
and there are rules for defining an identifier. We usually open it with letters or underscores, and then we can connect numbers, or then letters and underscores. But, remember, don't start with a number. Someone asked, can we use Chinese characters? Haha, the same is possible, but no one will do this for two reasons. First, in the process of coding, it is very troublesome to switch the input method, which greatly reduces the development efficiency. Second, it seriously affects the readability of the program. Think about it, half of your code is in Chinese and half in English. Is it the effect?
​​For example

int _a = 1;
int _ = 2;

bits, bytes, characters

What is a bit?
We all know that a computer cannot recognize real language, it only recognizes binary, that is, 0 and 1. We call such 0 and 1 bits
. What is a byte?
8 bits represent 1 byte, i.e. if you split a byte it could be 00101010
What is a character?
Characters are defined with single quotes in java, for example char c = 'A', of course, you can also use to represent a Chinese character, for example char c = '哈', but it is worth noting that an English character occupies 1 byte, while a Chinese character occupies 2 bytes .

Constants and Naming Conventions

A constant, an immutable quantity, in memory, you cannot change its value through an assignment statement.
Use final in java to declare a constant, for example

final int A = 1;

Different from the const in the C language, the const in the C language can be written before the int, or after the int, before the identifier, while in java, final must be written before the int, in java, in order to It is good programming practice to usually use uppercase to denote a constant.
In addition, let's talk about the naming conventions other than constants in java. For the readability of the program, we usually define an identifier as camel case. As for what is camel case? For example, our helloword, if we want to write it in camel case, helloWord, notice that this W is capitalized, that is to say, the letter of each word is capitalized, and the letter of the first word is lowercase, only in the class name, the first letter Letters of words are capitalized.

keyboard input

It is impossible for any program to have no input and output, so if we want to output a sentence on the console and then print it, what should we do? Here, we will use a string type of data, String type.
and Scanner scanners.

import java.util.Scanner;
public class ScanTest{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        String s2 = scan.nextLine();
        System.out.println(s);      
        System.out.println(s2); 
    }
}

We note that the new keyword is used here. We will come back when we talk about objects later. What we need to know now is that we construct a scanner by Scanner scan = new Scanner(System.in); here is scan.next( ) and scan.nextLine(), then, what is the difference between them? scan.next() does not count spaces, tabs, or carriage returns, and nextLine() ends only when it encounters carriage returns.
At the same time, you should know that scan.next() assigns the input content to s, which is the same as cin>>s in C++. Similarly, if you want to input an integer, you can use scan .nextInt(), input a double type data, scan.nextDouble(), input a float type data, scan.nextFloat(), etc. For details, you can query the java API

random number

Here are the
ways 1, construct Random to generate random numbers. Generate a random number by even random, this random number is very large, but

Random ran = new Randow();
int a = ran.nextInt()%100;//产生的是0-99的随机

It is worth noting here that nextInt will generate a large random number, and if the remainder is taken here, it just happens that the remainder is the random number within.
If we want to generate a character of az

Random ran = new Random();
char c = (char)('a' + ran.nextInt()%('z'-'a'));

Generate a random character via Math class

        int x = (int)( Math.random()*20);
        System.out.println(x);

Math.random() generates a random number 0-1. At this time, use Math.random()*N to generate a random number within 0-N. not including N

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325464043&siteId=291194637