栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一周工作总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一周工作总结

1、Idea使用时无故自动关闭或者被迫强制结束进程,导致已开启的服务器未能断开连接,占用端口号,导致再启动服务时显示端口号被占用。

解:打开cmd窗口执行 netstat -ano | findstr 被占用的端口号,得到Pid;

然后执行taskkill /f /t /im可杀死该程序,使被占用端口解放。

2.String.format的使用。(仅列举常用的)

%s,%S占位一个字符串
%c,%C占位一个字符

%s 占位一个字符串格式,若后者为对象类型则会调用后者的toString()方法,若本身为基本类型如int,则会自动装箱之后再执行toString方法。 

%S 会在%s的基础上将字符串所有字母大写

public static void main(String[] args) {
        String res = String.format("%s", "Hello World");
        System.out.println(res);
        String res2 = String.format("%S", "Hello World");
        System.out.println(res2);
    }


输出结果:
Hello World
HELLO WORLD

//---------------------------华丽分割线----------------------------------

 public static void main(String[] args) {
        int r=12;
        String res = String.format("%s", new Person(12,"张Xx"));
        System.out.println(res);
        String res2 = String.format("%S", new Person(12,"张Xx"));
        System.out.println(res2);
    }

输出结果:
Person{id=12, name='张Xx'}
PERSON{ID=12, NAME='张XX'}

//---------------------------华丽分割线----------------------------------

 %s可结合$符号使用,表示使用参数列表的第几个参数,示例如下图:

注:若使用了比如%3$s,但是没有对应的第三个参数,会报错。

public static void main(String[] args) {
        String res = String.format("%1$s", 12);
        System.out.println(res);
        String res2 = String.format("%2$S,%2$S,%1$s", new Person(12,"张Xx"),12);
        System.out.println(res2);
    }

输出结果:
12
12,12,Person{id=12, name='张Xx'}

还可在s前加上数字和-格式化输出,如:

private static void TestSs() {
        int r=12;
        String res = String.format("%8S,HelloWorld", 12);
        System.out.println(res);
        String res2 = String.format("%-8S,HelloWorld", 12);
        System.out.println(res2);
    }

输出结果:
      12,HelloWorld
12      ,HelloWorld

%c 占位一个字符可使用一般的char类型,也会自动将数字转为对应的字符,可占位中文字符。不可占位小数,但是(char)97.99F强制类型转换可以正常显示a。。。

public static void main(String[] args) {
        String res = String.format("%c", '你');
        System.out.println(res);
        String res2 = String.format("%c", 97);
        System.out.println(res2);
        String res3 = String.format("%C", 98);
        System.out.println(res3);
    }

输出结果:
你
a
B

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571821.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号