Basics for Java

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39063526/article/details/102728458

JAVA Starter

JAVA language features: 1. Easy to use safe and reliable 2. 3. 4. Object-oriented cross-platform 5. Support multithreading

Data type conversion:

1. automatic type conversions: ① ② two data types compatible with each type of the target range greater than the range of source types 

2. cast: top condition is not satisfied

For example: byte can be converted to type int and automatically transferred byte int error occurs

 

Scoped variables: a curly braces

Constant: in the program fixed values ​​including: integer constant, floating-point constants, Boolean constants, character constants and so on.

Special constants: true, false, null

Logical operators: && / || a short circuit or a short circuit

  • Operator precedence:

The more the greater the priority

if syntax:

if() 

{}

else if()

{}

else

{}

switch syntax:

switch (expression): {

case 1 target:

    Statement 1 is executed

    break;

case 2 target values:

    Execute the statement 2

    break;

...

default :

    Execute the statement n

    break;

}

 

Method Format: similar function

Modifier return type method name ([1 Parameter Name Parameter Type, Parameter Name Parameter Type 2, ..]) {

Execute the statement

return Return value;

}

public static int getArea(int x ,int y){

    int temp=x*y;

    return temp;

}

Overloaded method:

Suitable methods will automatically match the number of parameters depending on the type or 

Overload conditions: 1. 2. A number of parameters were the same method or different parameters Type

 

Array 

The method defined: int [] x = new int [10]

                int[]x;        x=new int [10]

                int []x={3,2,5,8,4,7,6,9}

Array name .length

An array of common operations:

1. array traversal:

       you x [] = {3,2,5,8,4,7,6,9};

       for (int i=0;i<x.length;i++)

       {

           System.out.println(x[i]);

       }

 

2. The array of most value

    you x [] = {3,2,5,8,4,7,6,9};

       . The System OUT .println ( "maximum value of the array is:" + getMax ( X ));

    }

    public static int getMax(int a[]) {

       int max=a[0];

       for (int i=1;i<a.length;i++) {

           if (max<a[i]) {

               int t=a[i];

               a[i]=max;

               max=t;

           }

    }

    return max;

    }

 

3. Sorting an array

public static void sort(int a[]) {

       for (int i=0;i<a.length;i++)

       {

           for (int j=i;j>0;j--)

           {

               if (a[j]<a[j-1]) {

                  you're not ;

                  t=a[j];

                  a[j]=a[j-1];

                  a[j-1]=t;

               }

           }

       }

    for (int i=0;i<a.length;i++)

           {

               System.out.print(a[i] +"  ");

           }

    }

Arrays tools:

Arrays class is a specialized tool for the operation of the array, such tools located java.util package .Arrays class provides a number of static methods, commonly used methods are shown in Table:

 

 

you x [] = {3,2,5,8,4,7,6,9};

       int Copy [] = Arrays,. copyOfRange ( X ,. 1,. 7); // Before closing the opening

       System.out.println(Arrays.toString(copy));

       Arrays.sort(x);

       System.out.println(Arrays.toString(x));

       Arrays.fill(x,9);

       System.out.println(Arrays.toString(x));

 

String 类和StringBuffer类

String 类 

初始化两种方法:

1.String str1="abc";

2.String str1=new String();

  String str2=new String("abc")

String类的常用方法

 

indexOf  :返回指定字符(子字符串)在此字符串中第一次出现处的索引

charAt  查看指定下标对应的字符

endsWith 判断此字符串是否以指定的字符串结尾

length 返回此字符串的长度

equals 将此字符串与指定的字符串比较,如果相等则返回true,否则返回false 

isEmpty 当且仅当字符串长度为0时返回true

startsWith 判断此字符串是否以指定的字符串开头

contains 判断此字符串是否包含指定的字符序列

toLowerCase 将字符串全部转换为小写

toUpperCase 将字符串全部转换为大写

toCharArray  将此字符串转换为一个字符数组

replace 返回一个新的字符串,它是通过利用newstr替换此字符串中出现的所有的oldstr得到的

split 根据参数字符串将原来的字符串分割为若干个子字符串

substring 返回一个新字符串 ,它包含从指定的beginIndex处开始,直到此字符串末尾的所有字符

返回一个新字符串 ,它包含从指定的beginIndex处开始,直到此字符串endIndex-1的所有字符

trim 返回一个新的字符串,它去除了原字符串首尾的空格

 

 

String str1="  ababcabcde dcba    ";

       System.out.println(str1.length());

       System.out.println(str1.charAt(1));

       System.out.println(Arrays.toString(str1.toCharArray()));

       System.out.println(str1.toUpperCase());

       System.out.println(str1.toLowerCase());

       System.out.println(str1.endsWith("ddd"));

       System.out.println(str1.trim());

       System.out.println(Arrays.toString(str1.split("b")));

       System.out.println(str1.substring(5));

       System.out.println(str1.contains("cabc"));

       System.out.println(str1.replace("ab", "oo"));

21

 

[ ,  , a, b, a, b, c, a, b, c, d, e,  , d, c, b, a,  ,  ,  ,  ]

  ABABCABCDE DCBA   

  ababcabcde dcba   

false

ababcabcde dcba

[  a, a, ca, cde dc, a    ]

bcabcde dcba   

true

  oooocoocde dcba   

 

StringBuffer类:

由于String 字符串是常量,一经创建,其内容和长度不可改变.如果需要对一个字符串进行修改,则只能创建新的字符串.为此JDK中提供了一个StringBuffer类(也称作字符串缓冲区),与String类不同的是,它的内容和长度是可变的.StringBuffer类如同一个字符串容器,当在其中添加或删除字符时,并不会产生新的StringBuffer对象.

针对添加和删除字符的操作,StringBuffer类提供了一系列的方法,具体如下

StringBuffer append(char c):添加参数到StringBuffer对象中

StringBuffer insert(int offset,String str):在字符串中的offset位置插入字符串str

StringBuffer delete(int start,int end) :删除StringBuffer对象中指定范围的字符或字符串序列

StringBuffer deleteCharAt(int index) :移除此序列指定位置的字符

StringBuffer replace(int start,int end,String s):    

void setCharAt(int index,char ch) :修改指定位置Index处的字符序列

StringBuffer reverse():将此字符序列用其反转形式取代

String toString() :返回StringBuffer缓冲区中的字符串

    add();

    remove();

    alter();

 

public static void add() {

       StringBuffer sb=new StringBuffer();

       sb.append("abcd");

       System.out.println(sb);

       sb.insert(1, "shshshs");

       System.out.println(sb);

    }

    public static void remove()

    {

       StringBuffer sb=new StringBuffer("abcdefg");

       sb.delete(2, 4);

       System.out.println(sb);

       sb.deleteCharAt(2);

       System.out.println(sb);

       sb.delete(0, sb.length());

       System.out.println(sb);

    }

    public static void alter()

    {

       StringBuffer sb=new StringBuffer("abcdefg");

       sb.setCharAt(1, 'd');

       System.out.println(sb);

       sb.replace(0, 3, "123");

       System.out.println(sb);

       System.out.println(sb.reverse());

       

    }

abcd

ashshshsbcd

abefg

abfg

 

adcdefg

123defg

gfed321

 

 

包装类

 在JAVA中,很多类的方法都需要接收引用类型的对象,每个基本数据类型都有其对应的包装类。

char --Character      long --Long

byte -- Byte             float --Float

int --Integer            double--Double

short--Short            boolean--Boolean

除了Integer    和    Charcter 之外,其他包装类的名称就是第一个字母大写

包装类和基本数据类型在进行转换时,引入了装箱和拆箱的概念,其中装箱是指将基本数据类型的值转为引用数据类型;反之,拆箱是指将引用数据类型对象转换为基本数据类型。

拆箱和装箱的过程:

Integer a=3;//自动装箱

int b=a+4;//自动拆箱

syso{b};

 

 

java 获得键盘输入值:

public static void main(String [] args) throws IOException{

  System.out.print(“Enter a Char:”);

  char i = (char) System.in.read();

  System.out.println(“your char is :”+i);

}

 

虽然此方式实现了从键盘获取输入的字符,但是System.out.read()只能针对一个字符的获取,同时,获取进来的变量的类型只能是char,当我们输入一个数字,希望得到的也是一个整型变量的时候,我们还得修改其中的变量类型,这样就显得比较麻烦。

 

2.我们需要用到BufferedReader类和InputStreamReader类

public static void main(String [] args) throws IOException{

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String str = null;

  System.out.println(“Enter your value:”);

  str = br.readLine();

  System.out.println(“your value is :”+str);

}

 

方法三:这种方法我认为是最简单,最强大的,就是用Scanner类

public static void main(String [] args) {

  Scanner sc = new Scanner(System.in);

  System.out.println(“请输入你的姓名:”);

  String name = sc.nextLine();

  System.out.println(“请输入你的年龄:”);

  int age = sc.nextInt();

  System.out.println(“请输入你的工资:”);

  float salary = sc.nextFloat();

  System.out.println(“你的信息如下:”);

  System.out.println(“姓名:”+name+“\n”+“年龄:”+age+“\n”+“工资:”+salary);

}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_39063526/article/details/102728458