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

本文详细解析了String、StringBuffer和StringBuilder三种字符串类型的特性及应用场景,并解释了它们在线程安全方面的区别。此外,还介绍了tools:context属性的作用及其在Android布局文件中的应用。

 

目录

 

 

String、StringBuffer和StringBuilder的区别

线程安全

tools:context=".mainactivity" 作用 


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" 作用 

<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。

在举例:

<?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 属性主要针对的 上图右边那个模拟器 调试时候显示状态,编译中不起作用的,;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等到80岁回首依旧年轻

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值