首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >String StringBuffer和StringBuilder的区别 线程安全 tools:context=mainactivity 作用

String StringBuffer和StringBuilder的区别 线程安全 tools:context=mainactivity 作用

作者头像
zhangjiqun
发布2024-12-16 13:09:59
发布2024-12-16 13:09:59
13900
代码可运行
举报
文章被收录于专栏:计算机工具计算机工具
运行总次数:0
代码可运行

String、StringBuffer和StringBuilder的区别

String:字符串常量,字符串长度不可变。

StringBuffer:字符串变量(Synchronized,即线程安全)。如果要频繁对字符串内容进行修改,出于效率考虑最好使用StringBuffer,如果想转成String类型,可以调用StringBuffer的toString()方法。

线程安全

是指:当你开了10个线程对StringBuffer同时进行处理10000次,那么总次数就是:1000000;要是使用StringBuilder就会少于10000000次。就是StringBuilder不是线程安全。

StringBuilder:字符串变量(非线程安全)。在内部,StringBuilder对象被当作是一个包含字符序列的变长数组。java.lang.StringBuilder是一个可变的字符序列,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。

其实无非就是在创建速度与线程安全之间不断的选择,其他的一些空间也是如此的情况.

tools:context=".mainactivity" 作用
代码语言:javascript
代码运行次数:0
运行
复制
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

tools:context="activity name" ,这个xml 布局就是渲染指定activity , manifest文件中设置了Theme 的话, 上图右边模拟器theme 样式也会跟着变化对应 theme。

在举例:

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        tools:text="Q"
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

看图显示是 Q,只是在预览使用,context是显示主题使用的; 这些tools 属性主要针对的 上图右边那个模拟器 调试时候显示状态,编译中不起作用的,;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-12-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 线程安全
  • tools:context=".mainactivity" 作用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档