ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA自学笔记(3)

2020-05-25 18:57:59  阅读:196  来源: 互联网

标签:JAVA String int System 笔记 static 字符串 自学 public


JAVA的心动之旅

Day1 字符串String

1.0 字符串的特点以及创建一个字符串

 

public class Practice {//构建字符串的3+1种方法
    public static void main(String[] args) {
        //第一种
        String one=new String();
        System.out.println("输出的字符串为:"+one);
        //第二种
        char str[]={'A','B','C'};
        String two=new String(str);
        System.out.println("输出的字符串为:"+two);
        //第三种
        byte []bytes={97,98,99,100};
        String three=new String(bytes);
        System.out.println("输出的字符串为:"+three);
        //+1种 直接构造 字符串常量不可改变 
        String four="HelloWorld";
        System.out.println("输出的字符串为:"+four);
        
    }
}

打印结果:

输出的字符串为:
输出的字符串为:ABC
输出的字符串为:abcd
输出的字符串为:HelloWorld

2.0 字符串的常量池

 3.0 字符串的获取方法

 

public class Practice {
    public static void main(String[] args) {
     String one="Hello";
     System.out.println("字符串的长度为:"+one.length());
     String two="say ".concat(one);
     System.out.println("字符串为:"+two);
     for(int i=0;i<two.length();i++)
     {
         char ch=two.charAt(i);
         System.out.print(ch+" ");
     }
      String three="o";
      System.out.println("\n"+one.indexOf(three));
      String four="h";
      System.out.println(one.indexOf(four));
    }
}

打印结果

字符串的长度为:5
字符串为:say Hello
s a y H e l l o
4
-1

4.0 字符串的比较 ==为地址的比较

5.0 字符串的截取

public class Practice {
    public static void main(String[] args) {
     String one="hellobts";
     String two=one.substring(5);
     System.out.println(two);
     String three=one.substring(0, 5);
     System.out.println(three);
    }
}

打印结果:

bts
hello

6.0 字符串的转化方法

 

 

 

public class Practice {
    public static void main(String[] args) {
     String one="hellobts";
     char a[]=one.toCharArray();
     for(int i=0;i<a.length;i++)
     {
         System.out.print(a[i]+" ");
     }
     System.out.println();
     byte []bytes=one.getBytes();
     for(int i=0;i<bytes.length;i++)
     {
         System.out.print(bytes[i]+" ");
     }
     System.out.println();
     String two=one.replace("hello", "love");
     System.out.println(two);
    }
}

打印结果:

h e l l o b t s
104 101 108 108 111 98 116 115
lovebts

 

Day2 static 关键字

1.0 static 概述

一旦用了static 关键字,那么这样的内容不再属于对象自己,它是属于类的,所以凡是本类的对象,都共享一份。

2.0 static 修饰成员变量 

public class Students {
     private static int idcounter=0;//每当创建一个对象(new)计数器++
      static String room;
     private String name;
     private int age;
     private int id;
     public Students() {
        this.id=++idcounter;
         }
     public Students(String name, int age) {
        this.name = name;
        this.age = age;
        this.id=++idcounter;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}
public class Practice {
    public static void main(String[] args) {
    Students one=new Students("田小娟",22);
    one.room="101教室";
    Students two=new Students("徐穗珍",22);
    System.out.println("姓名:"+one.getName()+" "+"年龄:"+one.getAge()+" 教室:"+one.room+
" 学号"+one.getId()); System.out.println("姓名:"+two.getName()+" "+"年龄:"+two.getAge()+" 教室:"+two.room+
" 学号"+two.getId()); } }

 3.0 static 修饰方法

 

 

 

 

 

4.0 静态代码块

Day3 与Arrays相识

 

 

1.0 简单的应用

 

 

import java.util.Arrays;

public class Practice {
    public static void main(String[] args) {
        int []num={3,89,45,235,43,79};
        String str=Arrays.toString(num);
        System.out.println(str);
        Arrays.sort(num);
        for(int i=0;i<num.length;i++)
        {
            System.out.print(num[i]+" ");
        }
    }
}

 

打印结果:

[3, 89, 45, 235, 43, 79]
3 43 45 79 89 235

 

 

2.0 字符串倒序

import java.util.Arrays;

public class Practice {
    public static void main(String[] args) {
        String str="aihfjsdfhuwefwnf";
        //定义一个随机的字符串,并将字符串排序后倒序输出
        //需将字符串先转化为字符数组,才能使用Arrays
        char []chars=str.toCharArray();
        Arrays.sort(chars);
        for(int i=chars.length-1;i>=0;i--)
        {
            System.out.print(chars[i]+" ");//w w u s n j i h h f f f f e d a 
        }
        
    }
}

 Math类方法(百度)

 

标签:JAVA,String,int,System,笔记,static,字符串,自学,public
来源: https://www.cnblogs.com/mzq-/p/12952229.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有