String StringBuffer和StringBuilder的区别 线程安全 tools:context=mainactivity 作用
目录
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 属性主要针对的 上图右边那个模拟器 调试时候显示状态,编译中不起作用的,;