Java entry notes-basic knowledge (1)

table of Contents

Introduction 

Recommended Java development tools

helloworld example 

Common error problems

Comment

Conversion of storage units

Basic data type

Constants and variables

Common problems in defining variables

Identifier definition

Identifier command rules camel case nomenclature

java keywords

Automatic data type conversion and forced type conversion

Operator

Arithmetic Operator

Increase and decrease

Assignment operator

Relational operator

Logical Operators

Short-circuit logic operator

Ternary operator

Receive data entered by the user (data input)

Process control

Sequence structure

Branch structure

if conditional branch statement

switch statement

Loop structure

for loop statement

while loop statement

do-while loop statement

Jump control statement

operation


Introduction 

        JAVA Chinese translation name Java, meaning a cup of hot coffee. The advantage of java is that the software written can run on all computers and has platform-independent characteristics. As long as the platform provides a java environment, software written in java can be executed on its operating system. 

 

Recommended Java development tools

idea:https://www.jetbrains.com/idea/download/#section=windows

Try to download the flagship version as much as possible, you can try it for free for 30 days. After all, the functional difference between the community version and the flagship version is quite big, and the function modules are doubled.

 

helloworld example 

 Regardless of what is a class, what is a method. These we will learn later, for Xiaobai at present, only need to know that  System.out.println()  is the output.

public class test {//定义一个类
    public static void main(String[] args) {//定义一个main方法
        System.out.println("hello word");//print是输出
    }
}

 

println is an output statement that automatically adds a newline, print is no newline.

public class test {
    public static void main(String[] args) {
        System.out.print("hello ");
        System.out.print("world");
    }
}

 

Common error problems

 

Case problem, java code block has case distinction
Chinese and English symbol problems

 

Comment


The comment does not participate in the operation and only serves as an explanation.

Annotation Instructions
Single line comment //Annotation information
Multi-line comments /*Comment information*/
Documentation notes

/**Annotation Information**/

 

Conversion of storage units

 

  • 1KB=1024B;
  • 1MB=1024KB=1024×1024B。
  • 1B (byte, byte bai) = 8 bit;
  • 1KB (Kilobyte, kilobytes)=1024B= 2^10 B;
  • 1MB (Megabyte, megabyte, du megabyte, referred to as "Megabyte") = 1024KB = 2^20 B;
  • 1GB (Gigabyte, gigabyte, billion bytes, also known as "gigabyte") = 1024MB = 2^30 B;
  • 1TB (Terabyte, trillion bytes, terabytes) = 1024GB = 2^40 B;

 

Basic data type

 

type of data Keyword Memory footprint Ranges
Integer byte 1 -128·127
short 2 -32768~32767
int 4 (-2 to the 31st power to 2 to the 31st power -1)
long 8 (-2 to the 63rd power to 2 to the 63rd power-1)
Floating point float 4

Negative numbers: -3.402823E+38 to -1.401298E-45

Positive number: 1.401298E-45 with 3.402823E+38

double 8

Negative numbers: -1.797693E+308 to -4.9000000E-324

Positive numbers: 4.9000000E-324 to 1.797693E+308

character char 2 0-65535
logic boolean 1 false、true

 

Constants and variables

 

1. Constant is an unchangeable quantity, for example:

System.out.println("hello word");

hello word  is constant

 

2. A variable is the amount whose value can be changed during the running of the program, for example:

Variable name = variable value

int a = 10;

System.out.println(a);

a= 11;

System.out.println(a);

The variable a has changed. The first time it is assigned to 10, the second time it is assigned to 11

 

Common problems in defining variables

Repeated type definition variables will be wrong

int a = 1;
string a =1 ;//会报错

 

  • When calling long type, you need to add L after the constant 
  • When calling float type, you need to add F after the constant
long a =1L;
float f = 13.14f;

 

Identifier definition

 

  • Composed of numbers, letters, underscore (_) and dollar sign ($)
  • Cannot start with a number
  • Cannot be a keyword
  • Case sensitive

 

Identifier command rules camel case nomenclature

 

  • Little Camel Nomenclature: userName (used for naming methods and variables)
  • Big hump nomenclature: UserName (for naming classes)

 

java keywords

 

abstract

assert

boolean

break

byte

case

catch

char

class

const

continue

default

do

double

else

enum

extends

final

finally

float

for

goto

if

implements

import

instanceof

int

interface

long

native

new

package

private

protected

public

return

strictfp

short

static

super

switch

synchronized

this

throw

throws

transient

try

void

volatile

while

 

 

Automatic data type conversion and forced type conversion

 

Forced type conversion

  • Format: data type variable name = (data type) variable name
  • Example: int a = (int)88.88;

Note: forced type conversion will lose data , 88.88 will eventually become 88, and the decimal point will be lost.

 

Automatic data type conversion

The automatic conversion is in order from low to high. The priority relationship between different types of data is as follows:
Low ---------------------------------------- ------------->high
byte,short,char-> int -> long -> float -> double

 

  • Example: perform operations on char and int type data, and the final data will be converted to int type

 

Operator

Arithmetic Operator

symbol effect use

Additional information

+ plus a+b The plus sign can be used for string splicing: System.out.println("ja"+"va");
- Less a-b  
* Multiply a*b  
/ except a/b Division "/" When the two sides are integers, take the integer part and round off the remainder. When one of the sides is floating point, divide according to normal rules.
% Take the remainder a%b  

Increase and decrease

symbol Description Instructions Attachment description  
++ Increment, the value of the variable increases by 1.

++i/i++

Put the plus sign in front of the variable, ++ and then participate in the operation / Put the plus sign behind the variable, and participate in the operation first, then ++ When used alone, ++ and - whether placed before or after the variable, the result is the same
Decrement, the value of the variable is subtracted by 1. --i/i-- Put the minus sign in front of the variable, and then take part in the operation/Minus sign put the variable after the variable, take part in the operation before proceeding--

Assignment operator

 

symbol Description Instructions Equivalent to
= Assignment a=1 a=1
+=  Plus equals a+=1 a = a+1
-= Minus equal a-=1 a = a-1
*= Multiply equals a*=1 a = a*1
/= Divide equal a/=1 a = a/1
%= More than equal to a%=1 a = a%1

注意:

 

关系运算符

符号 说明
== a==b,判断a和b的值是否相等,成立为true,不成立为false
!+ a!=b,判断a和b的等值是否不相等,成立为true,不成立为false
> a>b,判断a是否大于b,成立为true,不成立为false
>= a>=b,判断as是否大于等于b,成立为true,不成立为false
< a<b,判断a是否小于b,成立为true,不成立为false
<= a<=b,判断a是否小于等于b,成立为true,不成立为false

关系运算符的结果都是布尔类型,要么是true,要么是false。

 

逻辑运算符

 

符号 作用 说明
& 逻辑与 a&b, a和b都是true,结果为true,否则为false
| 逻辑或 a|b,a和b都是false,结果为false,否则为true
^ 逻辑异或 a^b,a和b结果不同为true,相同为false
逻辑非 !a,结果和a的结果正好相反

 

短路逻辑运算符

 

符号 作用 说明 附加说明
&& 短路与 作用和&相同,但是有短路效果 如果左边为假,右边不执行
|| 短路或 作用和|相同,但是又短路效果 如果左边为真,右边不执行

 

三元运算符

 

关系表达式?表达式1:表达式2;

规则:

计算关系表达式的值

如果值为true,则表达式1的值作为运算结果

如果值为false,表达式2的值作为运算结果

 

示范:

 

接收用户输入的数据(数据输入)

  1. 导包
    import java.util.Scanner;

  2. 创建对象
    Scanner sc = new Scanner(System.in);

  3. 接受数据
    int i = sc.nextInt();

 

流程控制

流程控制语句分类

  • 顺序结构
  • 分支结构(if,switch)
  • 循环结构(for,while,do-while)

 

顺序结构

顺序结构式程序中最简单的基本流程结构,按照代码现后顺序,从上而下执行。 

分支结构

if条件分支语句

//格式1
if(关系表达式){
    语句块1;
}

//格式2
if(关系表达式){
    语句块1;
}else{
    语句块2;
}

//格式3
if(关系表达式){
    语句块1;
}else if(关系表达式){
    语句块2;;
}else if(关系表达式){
    语句块3;
}
...
else{
    语句块4;
}

执行流程:

  1. 计算代码块的值
  2. 如果关系表达式的值为true,则执行语句块中的语句
  3. 如果关系表达式的值为false,则执行else语句块中的语句。如果没有else,则跳过此次if判断

流程图:

                 格式1                                     格式2                                                                格式3

 

switch开关语句

switch语句是单条件多分支的开关语句,它的一般格式定义如下:

//格式

switch(表达式){

case 常用值 1:
    语句块
    break;
case 常用值 2:
    语句块
    break;


case 常用值 n:
    语句块
    break;
default:
    语句块
}

格式说明:

  •  表达式:取值可以为byte、short、int、char类型
  • case 常用值:取值可以为byte、short、int、char类型,且要互不相同
  • break:结束代码执行
  • default:若switch语句中的表达式的值不与任何case的常用值相等,则执行default中的语句块

 

注意:若switch中,匹配到常用值时,语句块中并没有break,会出现穿越功能。

示例:

 

循环结构

for循环语句

//格式

for(初始化语句;条件判断语句;条件控制语句){
    循环体语句;
}
  • 初始化语句:负责完成变量的初始化
  • 条件判断语句:条件判断语句的值必须时boolean类型的表达式,也称为循环条件
  • 条件控制语句:条件控制语句用于修改变量的值,改变循环条件。

 

执行流程:

  1. 执行初始化语句
  2. 执行条件判断语句,看奇结果时true还是false
    1. 如果时false,循环结束
    2. 如果时true,继续执行
  3. 执行循环体语句
  4. 执行条件控制语句
  5. 回到 2 继续

 

流程图:

示例:

打印5次hello world

public class test {
    public static void main(String[] args) {
        int i;
        for(i=0;i<5;i++){
            System.out.println("hello world");
        }

    }
}

 

循环体语句只有一条时,大括号可以省略。

示范:

public class test {
    public static void main(String[] args) {
        int i;
        for(i=0;i<5;i++)System.out.println("hello world");

    }
}

for的死循环用法:

public class test {
    public static void main(String[] args) {
        for(;;)System.out.println("hello world");

    }
}

 

while循环语句

//格式
while(表达式){
    循环体;
}

执行流程:

  1. 执行表达式的值
  2. 如果该值式true,则执行循环体。如果该值是false,则跳出循环。
  3. 执行循环体。
  4. 回到1

 流程图:

示例:

打印 5次 hello world ,记住:一定要写个条件控制语句(i++),不然会while会进入死循环。

public class test {
    public static void main(String[] args) {
        int i=0;
        while(i<5){
            System.out.println("hello world");
            i++;
        }
    }
}

while的死循环用法:

public class test {
    public static void main(String[] args) {
        while(true){
            System.out.println("hello world");
        }
    }
}

 

do-while循环语句

do-while循环和while循环的区别是do-while的循环至少被执行一次。

//格式
do{
    循环体语句
}while(表达式)

示例:

public class test {
    public static void main(String[] args) {
        int i =3;
        do{
            System.out.println("hello world");
        }while(i<3);
    }
}

跳转控制语句

 

  1. break:break是用于永久终止循环。即不执行本次循环中break后面的语句,直接跳出循环。
  2. continue:continue是用于终止本次循环。即本次循环中continue后面的代码不执行,进行下一次循环的入口判断。

  

作业

1.判断一个整数是奇数还是偶数。(if语句)

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();

        if(number%2==0){
            System.out.println("偶数");
        }else{
            System.out.println("奇数");
        }
    }
}

 

2.一年有12个月,分属于春夏秋冬4个季节,键盘录入一个月份,请用程序实现判断该月份数据哪个季节,并输出。(switch语句)

import java.util.Scanner;

public class test {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();

        switch(number){
            case 1:
            case 2:
            case 3:
                System.out.println("春季");
                break;
            case 4:
            case 5:
            case 6:
                System.out.println("夏季");
                break;
            case 7:
            case 8:
            case 9:
                System.out.println("秋季");
                break;
            case 10:
            case 11:
            case 12:
                System.out.println("冬季");
                break;
            default:
                System.out.println("输入错误");
        }
    }
}

3.求出数字1到100的和。(for循环)

public class test {
    public static void main(String[] args) {
        int i;
        int sum=0;
        for(i=1;i<101;i++){

            sum+=i;
        }
        System.out.println(sum);
    }
}

4.求出数字1-100的偶数和。(for循环)

public class test {
    public static void main(String[] args) {
        int i;
        int sum=0;
        for(i=1;i<101;i++){
            if(i%2==0){
                sum+=i;
            }
        }
        System.out.println(sum);
    }
}

5.水仙花数 (for)

  • 水仙花数是一个三位数
  • 水仙花的个数、十位、百位的数字立方和等于原数
  • 例如 371:3^{3}+7^{3}+1^{3}=371 
public class test {
    public static void main(String[] args) {
        int i;
        int g,s,b;
        for(i=100;i<=999;i++){
            //将个位数取出
            g = i%10;

            //将十位数取出
            s = i/10%10;

            //将百位数取出
            b = i/100;

            if(g*g*g+s*s*s+b*b*b == i){
                System.out.println(i);
            }
        }
    }
}

 

Guess you like

Origin blog.csdn.net/weixin_41924764/article/details/109737012
Recommended